如何在 Unity 5 中绘制二维三角形?

How can I draw 2D triangles in Unity 5?

我正在尝试使用 this or this(脚本)绘制二维三角形,但没有成功(我没有看到任何三角形)。我只有摄像头、canvas 和面板,我也尝试将它应用到空的 canvas 的子对象上,我必须如何应用这些脚本?我尝试了不同的变体。这是绘制三角形的好方法还是有更好的方法?另外我在某处读到 GL Class 不适用于 iOS and/or Mac,是真的吗?

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 };

希望对您有所帮助! =)

提醒一下,在 Unity 5.6(或 higher/lower 版本?)中,uv 不允许具有不同大小的顶点'。 所以...如果您在使用 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 };

然后错误就解决了。