2016-07-30 101 views
0

我需要为我使用Unity制作的分贝应用绘制图形。下面的解决方案可行,但并不像我的雇主所愿。我可以让这张图看起来更专业和准确吗?基本上我想要的行是相等的宽度,并防止线几乎看不见像在下面的图片:http://puu.sh/qjSvO/a51c11cef5.png如何在Unity中的Canvas上绘制2D图形

我想制作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)); 
     } 
    } 
} 
+0

它看起来像你可能意外地设置'widthCurve'或'colorGradiant' - 可能在检查器而不是在你的代码中。 – ArtOfWarfare

回答

0

使用编辑器,你可以“切割”精灵成九块,以便有每边的纹理,每个角落和纹理来填补它的质地。我会建议使用,要如果你想要的话,让它看起来不错。自从我自己使用它以来已经有一段时间了,但也许您可以在Unity手册中找到相关信息。

+0

我尝试过使用sprite编辑器,但无法从中获得太多帮助。基本上我想防止线宽变化如下图所示:http://puu.sh/qjSvO/a51c11cef5.png – Waltari

+0

然后我误解了你。如果你明白,我认为这是一个支柱图。 –