我怎樣才能使我正在渲染的線條在產生新的線條時不會消失? 我正在創建從一個點到另一個點的線條,在正確的位置上沿著螢屏以正確的間隔移動,但這些線條一個接一個地消失?我正試圖創建一個股票圖表的游戲化圖形模擬。謝謝任何幫助我的人。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class line_rend : MonoBehaviour
{
private LineRenderer lineRend;
float candle_y;
public float stop_running;
public float candle_speed;
public float candle_delay;
public float candle_rate;
public float candle_volat;
float candle_x;
//在第一幀更新之前呼叫Start。
void Awake()
{
candle_y = Random.Range(-4.5f, 4.5f) 。
lineRend = GetComponent<LineRenderer>()。
candle_x = -11.0f;
InvokeRepeating("Spawn_Candle", candle_speed, candle_delay) 。
}
public void Spawn_Candle()。
{
lineRend.startWidth = 0.1f。
lineRend.endWidth = 0.1f;
lineRend.SetPosition(0, new Vector3(candle_x, candle_y, 0f) 。)
candle_y = Random.Range((0f - candle_volat), candle_volat)。
candle_x = candle_rate;
lineRend.SetPosition(1, new Vector3(candle_x, candle_y, 0f) )。)
}
uj5u.com熱心網友回復:
我認為你在重復使用同一行。
相反,將該行保存為預制件,當你需要它們時,instantiate一個新的預制件。
每一個簡單的例子:
public Transform linePrefab; <--配置prefab in Inspector
游戲物件lineObj;
LineRenderer lineRend。
public void Spawn_Candle()。
{
lineObj = Instantiate(linePrefab);
lineRend = lineObj.GetComponent<LineRenderer>()。
lineRend.startWidth = 0.1f;
lineRend.endWidth = 0.1f;
lineRend.SetPosition(0, new Vector3(candle_x, candle_y, 0f) 。)
candle_y = Random.Range((0f - candle_volat), candle_volat)。
candle_x = candle_rate;
lineRend.SetPosition(1, new Vector3(candle_x, candle_y, 0f) )。)
}
uj5u.com熱心網友回復:
正如在這個答案中提到的,你目前總是在用新的坐標覆寫你現有的行。
然而,股票圖表是一條連續的線,你絕對不不想為每一個筆畫生成一個新的linerenderer。
你只是想添加更多的點。
public class line_rend : MonoBehaviour
{
private LineRenderer lineRend;
//個人選擇,但如果我有坐標,我寧愿將它們存盤在一個欄位中。
Vector2 candle;
public float stop_running;
public float candle_speed;
public float candle_delay;
public float candle_rate;
public float candle_volat;
//Start在第一幀更新前被呼叫。
void Awake()
{
candle.y = Random.Range(-4.5f, 4.5f) 。
lineRend = GetComponent<LineRenderer>()。
lineRend.startWidth = 0.1f。
lineRend.endWidth = 0.1f;
candle.x = -11.0f;
//使用nameof可以防止以后重命名方法時的麻煩。
InvokeRepeating(nameof(Spawn_Candle), candle_speed, candle_delay) 。
}
public void Spawn_Candle()
{
//span>添加一個新的位置。
lineRend.positionCount = 1;
candle.y = Random.Range(-candle_volat, candle_volat);
candle.x = candle_rate;
//只設定新的位置。
//它已經與前一個位置相連。
lineRend.SetPosition(lineRend.positionCount - 1, candle) 。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/309163.html
標籤:
