簡體   English   中英

如何在Unity 5中的Canvas上繪制2D圖形

[英]How to draw a 2D graph on Canvas in Unity 5

我需要為使用Unity制作的分貝應用程序繪制圖形。 以下解決方案有效,但不如我的雇主所希望的那樣精確。 我可以讓這張圖看起來更專業,更精確嗎? 基本上,我希望線條的寬度相等,並防止線條變得幾乎不可見,如下圖所示: http : //puu.sh/qjSvO/a51c11cef5.png

我當時正在考慮制作2D紋理並使用SetPixel,但是我不確定這是否正確。

該圖形作為可擴展UI的一部分繪制在畫布上。

 public class Graph : MonoBehaviour {

 public float graphWidth;
 public float graphHeight;
 LineRenderer newLineRenderer;
 List<int> decibels;
 int vertexAmount = 50;
 float xInterval;

     GameObject parentCanvas;

     // Use this for initialization
     void Start ()
     {
         parentCanvas = GameObject.Find("Canvas");
         graphWidth = transform.Find("Linerenderer").GetComponent<RectTransform>().rect.width;
         graphHeight = transform.Find("Linerenderer").GetComponent<RectTransform>().rect.height;
         newLineRenderer = GetComponentInChildren<LineRenderer>();
         newLineRenderer.SetVertexCount(vertexAmount);

         xInterval = graphWidth / vertexAmount;
     }

     //Display 1 minute of data or as much as there is.
     public void Draw(List<int> decibels)
     {
         if (decibels.Count == 0)
             return;

         float x = 0;

         for (int i = 0; i < vertexAmount && i < decibels.Count; i++)
         {
             int _index = decibels.Count - i - 1;

             float y = decibels[_index] * (graphHeight/130); //(Divide grapheight with the maximum value of decibels.
             x = i * xInterval;

             newLineRenderer.SetPosition(i, new Vector3(x - graphWidth / 2 , y - graphHeight / 2 , 0));
         }
     }
 }

使用編輯器,您可以將精靈“切割”成九塊,以便每個側面都有一個紋理,每個角都有一個紋理,並且要填充它。我建議您使用它使它看起來更好是這樣。 自從我自己使用它已有一段時間了,但是也許您可以在Unity手冊上找到信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM