今天寫了一個實時畫曲線的功能,主要是想用於顯示車輛的100公里加速,這里指寫了一個測試還沒能和主程序關聯上
using UnityEngine; using System.Collections; using System; using System.Collections.Generic; public class CurveTest : MonoBehaviour { public Material material; private float _speed=0; private float _timeA=0; private float _timeB=0; private bool _isPlay=false; private LineRenderer lineRenderer; //定義一個Vector3,用來存儲鼠標點擊的位置 private Vector3 position; //用來索引端點 private int index = 0; //端點數 private int LengthOfLineRenderer=0; //private float[] _arrSpeed; private ArrayList _arrSpeed=new ArrayList(); private int _speedIndex; // Use this for initialization void Start(){ InvokeRepeating("OnTimer", 1, 0.1f); //添加LineRenderer組件 lineRenderer = gameObject.AddComponent<LineRenderer>(); //設置材質 lineRenderer.material = material; //設置顏色 lineRenderer.SetColors(Color.red, Color.yellow); //設置寬度 lineRenderer.SetWidth(0.5f, 0.5f); } // Update is called once per frame void Update () { lineRenderer = GetComponent<LineRenderer>(); if(Time.timeScale==1){ if(Input.GetKey(KeyCode.W)){ _speed+=(110-_speed)/200; _isPlay=true; }else{ _speed-=0.1f; } if(_speed<0){ _speed=0; }else if(_speed>100){ _speed=100; _isPlay=false; Time.timeScale=0; OnEnd(); } if(_isPlay){ _timeA = (_timeA+Time.deltaTime); _timeB=(Mathf.CeilToInt (_timeA)); } } } void OnGUI(){ GUILayout.Label(""+_speed); GUILayout.Label(""+_timeB); } void OnTimer(){ if(Mathf.CeilToInt(_speed)>0){ _arrSpeed.Add(Mathf.CeilToInt(_speed)); _speedIndex+=1; position=new Vector3(_speedIndex,_speed/2,0); //端點數+1 LengthOfLineRenderer++; //設置線段的端點數 lineRenderer.SetVertexCount(LengthOfLineRenderer); while (index < LengthOfLineRenderer) { //兩點確定一條直線,所以我們依次繪制點就可以形成線段了 lineRenderer.SetPosition(index, position); index++; } } } void OnEnd(){ foreach(object o in _arrSpeed) { Debug.Log(o); gameObject.AddComponent ("SimpleCurve"); } } }