Unity中使用自定义mesh和UImesh

自定义Mesh:

   public float width = 1;
   public float height = 1;

   public void CreateMesh()
   {
       MeshRenderer meshRenderer = gameObject.GetComponent();
       meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));

       MeshFilter meshFilter = gameObject.GetComponent();

       Mesh mesh = new Mesh();



       Vector3[] vertices = new Vector3[4]
       {
           new Vector3(0, 0, 0),
           new Vector3(width, 0, 0),
           //new Vector3(0, height, 0),
           Quaternion.Euler(0,0,60)* Vector3.up *2,
           new Vector3(width, height, 0)
       };
       mesh.vertices = vertices;

       int[] tris = new int[6]
       {
           // 左下方三角形
           0, 2, 1,
           // 右上方三角形
           2, 3, 1
       };
       mesh.triangles = tris;

       Vector3[] normals = new Vector3[4]
       {
           -Vector3.forward,
           -Vector3.forward,
           -Vector3.forward,
           -Vector3.forward
       };
       mesh.normals = normals;

       Vector2[] uv = new Vector2[4]
       {
           new Vector2(0, 0),
           new Vector2(1, 0),
           new Vector2(0, 1),
           new Vector2(1, 1)
       };
       mesh.uv = uv;

       meshFilter.mesh = mesh;
   }

自定义六边形UIMesh:

[SerializeField] private Material radarMaterial;
[SerializeField] private Texture2D radarTexture2D;

[SerializeField]
private CanvasRenderer radarMeshCanvasRenderer;

private void UpdateStatsVisual()
  {
      Mesh mesh = new Mesh();

      Vector3[] vertices = new Vector3[6];
      Vector2[] uv = new Vector2[6];
      int[] triangles = new int[3 * 5];

      float angleIncrement = 360f / 5;
      float radarChartSize = 145f;

      Vector3 attackVertex = Quaternion.Euler(0, 0, -angleIncrement * 0) * Vector3.up * radarChartSize ;
      int attackVertexIndex = 1;
      Vector3 defenceVertex = Quaternion.Euler(0, 0, -angleIncrement * 1) * Vector3.up * radarChartSize;
      int defenceVertexIndex = 2;
      Vector3 speedVertex = Quaternion.Euler(0, 0, -angleIncrement * 2) * Vector3.up * radarChartSize;
      int speedVertexIndex = 3;
      Vector3 manaVertex = Quaternion.Euler(0, 0, -angleIncrement * 3) * Vector3.up * radarChartSize;
      int manaVertexIndex = 4;
      Vector3 healthVertex = Quaternion.Euler(0, 0, -angleIncrement * 4) * Vector3.up * radarChartSize;
      int healthVertexIndex = 5;

      vertices[0] = Vector3.zero;
      vertices[attackVertexIndex] = attackVertex;
      vertices[defenceVertexIndex] = defenceVertex;
      vertices[speedVertexIndex] = speedVertex;
      vertices[manaVertexIndex] = manaVertex;
      vertices[healthVertexIndex] = healthVertex;

      uv[0] = Vector2.zero;
      uv[attackVertexIndex] = Vector2.one;
      uv[defenceVertexIndex] = Vector2.one;
      uv[speedVertexIndex] = Vector2.one;
      uv[manaVertexIndex] = Vector2.one;
      uv[healthVertexIndex] = Vector2.one;

      triangles[0] = 0;
      triangles[1] = attackVertexIndex;
      triangles[2] = defenceVertexIndex;

      triangles[3] = 0;
      triangles[4] = defenceVertexIndex;
      triangles[5] = speedVertexIndex;

      triangles[6] = 0;
      triangles[7] = speedVertexIndex;
      triangles[8] = manaVertexIndex;

      triangles[9] = 0;
      triangles[10] = manaVertexIndex;
      triangles[11] = healthVertexIndex;

      triangles[12] = 0;
      triangles[13] = healthVertexIndex;
      triangles[14] = attackVertexIndex;


      mesh.vertices = vertices;
      mesh.uv = uv;
      mesh.triangles = triangles;

      radarMeshCanvasRenderer.SetMesh(mesh);
      radarMeshCanvasRenderer.SetMaterial(radarMaterial, radarTexture2D);
     
  }

参考链接:

Example: creating a quad - Unity 手册

https://www.youtube.com/watch?v=twjMW7CxIKk&list=RDCMUCFK6NCbuCIVzA6Yj1G_ZqCg&index=2

你可能感兴趣的:(知识点记录,U3d,unity)