在 Unity 中,你可以使用 Unity 的绘图 API(Graphics.DrawTexture)来在 RawImage 上绘制折线图。这是一个完整的脚本示例:
``` using UnityEngine; using UnityEngine.UI;
public class LineGraph : MonoBehaviour { // 要在其上绘制折线图的 RawImage public RawImage image;
public Vector2[] points;
public Color lineColor;void Start()
{Texture2D texture = new Texture2D((int)image.rectTransform.rect.width, (int)image.rectTransform.rect.height);DrawLineGraph(texture, points, lineColor);image.texture = texture;
}void DrawLineGraph(Texture2D texture, Vector2[] points, Color color)
{for (int i = 0; i 1; i++){DrawLine(texture, points[i], points[i + 1], color);}
}void DrawLine(Texture2D texture, Vector2 p1, Vector2 p2, Color color)
{float slope &#61; (p2.y - p1.y) / (p2.x - p1.x);if (float.IsInfinity(slope)){int x1 &#61; (int)p1.x;int x2 &#61; (int)p2.x;int y &#61; (int)p1.y;if (x1 > x2){int temp &#61; x1;x1 &#61; x2;x2 &#61; temp;}for (int x &#61; x1; x <&#61; x2; x&#43;&#43;){texture.SetPixel(x, y, color);}}else{float intercept &#61; p1.y - slope * p1.x;