如何在Unity 5中绘制2D三角形?

时间:2015-03-20 19:33:19

标签: unity3d 2d draw

我尝试使用thisthis(脚本)绘制2D三角形但未成功(我没有看到任何三角形)。我只有相机,画布和面板,我也尝试将它应用到空白画布的子对象上,我该如何应用这些脚本?我试过不同的变化。这是绘制三角形的好方法还是有更好的东西?我还读过GL Class在iOS和/或Mac上无法工作的地方,这是真的吗?

2 个答案:

答案 0 :(得分:3)

private GameObject m_goTriangle;
m_goTriangle.AddComponent<MeshFilter>();
        m_goTriangle.AddComponent<MeshRenderer>();
        m_meshTriangle = m_goTriangle.GetComponent<MeshFilter>().mesh;
        m_meshTriangle.Clear();
        m_meshTriangle.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 0.25f), new Vector2(0.25f, 0.25f) };
        m_meshTriangle.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0.25f, 0), new Vector3(0.25f, 0.25f, 0) };
        m_meshTriangle.triangles = new int[] { 0, 1, 2 };

希望这有帮助! =)

答案 1 :(得分:2)

只是提醒一下,在Unity 5.6(或更高/更低版本?)中,uv不允许有不同大小的顶点&#39;。 所以......如果您在使用Junamai代码时遇到问题,那么Unity会出现如下错误: Mesh.uv is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array.

只需更改修改uv和顶点的顺序:

    private GameObject m_goTriangle;
    m_goTriangle.AddComponent<MeshFilter>();
    m_goTriangle.AddComponent<MeshRenderer>();
    m_meshTriangle = m_goTriangle.GetComponent<MeshFilter>().mesh;
    m_meshTriangle.Clear();
    m_meshTriangle.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0.25f, 0), new Vector3(0.25f, 0.25f, 0) };
    m_meshTriangle.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 0.25f), new Vector2(0.25f, 0.25f) };
    m_meshTriangle.triangles = new int[] { 0, 1, 2 };

错误将被解决。