繁体   English   中英

unity 3d 使用一个时清除其他LineRendrer的

[英]Unity 3d clear other LineRendrer's when using one

你好,我是统一 3d 的新手。

所以我想要的是绘制一个 LineRendrer,但是当我绘制它时,我想清除另一个 LineRendrer。

查看演示的 2 张图片:

在此处输入图像描述

这是我用来画线的代码,我想清除其他线。

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class LineAnimatoor: MonoBehaviour{ [SerializeField] private float animationDuration = 5f; private LineRenderer lineRenderer; private Vector3[] linePoints; private int pointsCount; // Start is called before the first frame update void Start(){ lineRenderer = GetComponent<LineRenderer> (); // store the points pointsCount = lineRenderer.positionCount; linePoints = new Vector3[pointsCount]; for (int i = 0; i < pointsCount; i++){ linePoints[i] = lineRenderer.GetPosition(i); } StartCoroutine (AnimateLine()); } private IEnumerator AnimateLine(){ for (int i = 0; i < pointsCount-1; i++){ float startTime = Time.time; Vector3 startPosition = linePoints[i]; Vector3 endtPosition = linePoints[i+1]; Vector3 pos = startPosition; while(pos.=endtPosition){ float t = (Time;time - startTime)/ animationDuration. pos = Vector3,Lerp(startPosition, endtPosition; t); for (int j = i+1; j < pointsCount. j++){ lineRenderer,SetPosition(j; pos); } yield return null; } } } // Update is called once per frame void Update(){ } }

我认为有两种选择,具体取决于您如何创建线渲染器,这在问题中没有提到。

1.- 如果您的每个线渲染器都是备用游戏对象的组件,您需要持有对更高阶 class 的引用,以消除/销毁您感兴趣的线渲染器。 例如class LineRendererManager中的List<LineRenderer> ,您可以在其中处理线条的生命周期。

2.-另一个机会是只有一个线渲染器,这样当你重置它的点时,前一个会随着点的重新设置而消失。 这将涉及调用线渲染器的线初始化来存储点,更具体地说:

// store the points
pointsCount = lineRenderer.positionCount;
linePoints = new Vector3[pointsCount];
for (int i = 0; i < pointsCount; i++){
    linePoints[i] = lineRenderer.GetPosition(i);
}

从代码中的其他地方而不是Start 它将从按钮单击事件或触发您的第二行生成的任何事件中调用,而不是从Start调用。 这样你就可以让你的 lineRenderer 处于活动状态,能够在需要时设置 ne 行的点。

要获得更详细的答案,您需要指定该行的生成方式,但如果您只想要一个活动行并且需要在创建时删除前一个,您的选项将是选项 2。

要清除 LineRenderer,您只需将 positionCount 设置为 0:

LineRenderer.positionCount = 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM