[英]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.