unity3d 想做动态折线,这个效果不好,姑且记录一下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Material material;
public GameObject S0;
private List<Vector3> lineInfo;
private bool startDraw = false;
Vector3 carSpeedVector;
// Use this for initialization
void Start()
{
lineInfo = new List<Vector3>();
carSpeedVector = new Vector3(0f, 0f, 0f);
}
// Update is called once per frame
void Update()
{
S0.transform.position=new Vector3(S0.transform.position.x, S0.transform.position.y, S0.transform.position.z+Time.deltaTime*1f);
carSpeedVector = new Vector3(Time.time, S0.transform.position.z, 0f);
lineInfo.Add(carSpeedVector);
}
void OnRenderObject()
{
if (!material)
{
Debug.LogError("Material is null!");
return;
}
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
int size = lineInfo.Count;
for (int i = 0; i < size - 1; i++)
{
Vector3 start = lineInfo[i];
Vector3 end = lineInfo[i + 1];
DrawLineFunc(start.x, start.y, end.x, end.y);
}
GL.End();
}
void DrawLineFunc(float x1, float y1, float x2, float y2)
{
GL.Vertex(new Vector3((float)(x1 * 10) / Screen.width, (float)(y1 * 2 + 0.5 * Screen.height) / Screen.height, 0));
GL.Vertex(new Vector3((float)(x2 * 10) / Screen.width, (float)(y2 * 2 + 0.5 * Screen.height) / Screen.height, 0));
}
}