参考

对于自定义mesh,有三点很重要:

1.顶点个数=三角形数+2;三角形顶点数=3*三角形数

2.顶点创建的顺序最好是顺时针或者逆时针创建的,这样可以大大地减少算法的复杂度

3.顶点绘制的顺序必须是顺时针或者逆时针绘制的,这样才能正确地绘制以顶点为边界点的图形,顶点绘制的顺序参考mesh.triangles



[csharp]  view plain  copy


1. using UnityEngine;  
2. using System.Collections;  
3.   
4. [RequireComponent(typeof(MeshFilter))]  
5. [RequireComponent(typeof(MeshRenderer))]  
6. public class CustomMesh : MonoBehaviour {  
7.   
8. }


[csharp]  view plain  copy



    1. using UnityEngine;  
    2. using System.Collections;  
    3. using System.Collections.Generic;  
    4. using UnityEditor;  
    5.   
    6. //使用前最好先锁定Inspector面板!  
    7. [CustomEditor(typeof(CustomMesh))]    
    8. public class CustomMeshEditor : Editor {  
    9.   
    10. private List<Vector3> vertices = new List<Vector3>();  
    11. private int[] triangles;  
    12.   
    13. void OnSceneGUI()  
    14.     {  
    15.         CustomMesh customMesh = (CustomMesh)target;  
    16.   
    17. for (int i = 0; i < vertices.Count; i++)  
    18.         {  
    19.             Handles.PositionHandle(vertices[i], Quaternion.identity);  
    20.             Handles.Label(vertices[i], i.ToString());  
    21.         }  
    22. if (vertices.Count > 1)  
    23.         {  
    24.             Handles.DrawPolyLine(vertices.ToArray());  
    25.             Handles.DrawLine(vertices[vertices.Count - 1], vertices[0]);  
    26.         }       
    27.   
    28. //在OnSceneGUI()中只能通过Handles来绘制新视图  
    29. //如果你想引入GUI的元素哪么就需要使用BeginGUI()和EndGUI()组合的使用。    
    30.         Handles.BeginGUI();  
    31. new Rect(0, 0, 100, 100));  
    32.    
    33. if (GUILayout.Button("创建点"))  
    34.         {  
    35.             vertices.Add(customMesh.transform.position);            
    36.         }  
    37. if (GUILayout.Button("撤销点"))  
    38.         {  
    39.             vertices.RemoveAt(vertices.Count - 1);  
    40.         }  
    41. if (GUILayout.Button("导出mesh"))  
    42.         {  
    43. new Mesh();  
    44.               
    45. int triangleAmount = vertices.Count - 2;  
    46. new int[3 * triangleAmount];  
    47.   
    48. //根据三角形的个数,来计算绘制三角形的顶点顺序(索引)  
    49. //顺序必须为顺时针或者逆时针  
    50. for (int i = 0; i < triangleAmount; i++)  
    51.             {  
    52. //固定第一个点  
    53.                 triangles[3 * i + 1] = i + 1;  
    54.                 triangles[3 * i + 2] = i + 2;  
    55.             }  
    56.   
    57.             mesh.vertices = vertices.ToArray();  
    58.             mesh.triangles = triangles;  
    59.   
    60. "Assets/CustomMesh.asset");  
    61.         }  
    62.         GUILayout.EndArea();  
    63.         Handles.EndGUI();     
    64.     }  
    65. }



    unity2d能用mesh吗_List


    unity2d能用mesh吗_mesh_02

    ///

    实例(属性图的制作):


    unity2d能用mesh吗_顺时针_03


    unity2d能用mesh吗_顺时针_04



    [csharp]  view plain  copy



      1. using UnityEngine;  
      2. using System.Collections.Generic;  
      3.   
      4. [RequireComponent(typeof(MeshFilter))]  
      5. [RequireComponent(typeof(MeshRenderer))]    
      6. public class CreatePentagon : MonoBehaviour {  
      7.   
      8. public Transform[] verticesTra;//赋值顺序为顺时针或者逆时针  
      9. public float[] proportion;//比例  
      10.   
      11. void Start()  
      12.     {  
      13. new List<Vector3>();  
      14. for (int i = 0; i < verticesTra.Length; i++)  
      15.         {  
      16.             Vector3 pos = (verticesTra[i].position - transform.position) * proportion[i];  
      17.             vertices.Add(pos);  
      18.         }  
      19.   
      20.         DrawPentagon(vertices);  
      21.     }  
      22.   
      23. void DrawPentagon(List<Vector3> vertices)  
      24.     {  
      25. new Mesh();  
      26. int triangleAmount = vertices.Count - 2;  
      27. int[] triangles = new int[3 * triangleAmount];  
      28.   
      29. //根据三角形的个数,来计算绘制三角形的顶点顺序(索引)    
      30. //顺序必须为顺时针或者逆时针    
      31. for (int i = 0; i < triangleAmount; i++)  
      32.         {  
      33. //固定第一个点    
      34.             triangles[3 * i + 1] = i + 1;  
      35.             triangles[3 * i + 2] = i + 2;  
      36.         }  
      37.   
      38.         mesh.vertices = vertices.ToArray();  
      39.         mesh.triangles = triangles;  
      40.   
      41.         GetComponent<MeshFilter>().mesh = mesh;  
      42.     }  
      43. }









      参考链接:

      对于自定义mesh,有三点很重要:

      1.顶点个数=三角形数+2;三角形顶点数=3*三角形数

      2.顶点创建的顺序最好是顺时针或者逆时针创建的,这样可以大大地减少算法的复杂度

      3.顶点绘制的顺序必须是顺时针或者逆时针绘制的,这样才能正确地绘制以顶点为边界点的图形,顶点绘制的顺序参考mesh.triangles



      [csharp]  view plain  copy


      1. using UnityEngine;  
      2. using System.Collections;  
      3.   
      4. [RequireComponent(typeof(MeshFilter))]  
      5. [RequireComponent(typeof(MeshRenderer))]  
      6. public class CustomMesh : MonoBehaviour {  
      7.   
      8. }


      [csharp]  view plain  copy



        1. using UnityEngine;  
        2. using System.Collections;  
        3. using System.Collections.Generic;  
        4. using UnityEditor;  
        5.   
        6. //使用前最好先锁定Inspector面板!  
        7. [CustomEditor(typeof(CustomMesh))]    
        8. public class CustomMeshEditor : Editor {  
        9.   
        10. private List<Vector3> vertices = new List<Vector3>();  
        11. private int[] triangles;  
        12.   
        13. void OnSceneGUI()  
        14.     {  
        15.         CustomMesh customMesh = (CustomMesh)target;  
        16.   
        17. for (int i = 0; i < vertices.Count; i++)  
        18.         {  
        19.             Handles.PositionHandle(vertices[i], Quaternion.identity);  
        20.             Handles.Label(vertices[i], i.ToString());  
        21.         }  
        22. if (vertices.Count > 1)  
        23.         {  
        24.             Handles.DrawPolyLine(vertices.ToArray());  
        25.             Handles.DrawLine(vertices[vertices.Count - 1], vertices[0]);  
        26.         }       
        27.   
        28. //在OnSceneGUI()中只能通过Handles来绘制新视图  
        29. //如果你想引入GUI的元素哪么就需要使用BeginGUI()和EndGUI()组合的使用。    
        30.         Handles.BeginGUI();  
        31. new Rect(0, 0, 100, 100));  
        32.    
        33. if (GUILayout.Button("创建点"))  
        34.         {  
        35.             vertices.Add(customMesh.transform.position);            
        36.         }  
        37. if (GUILayout.Button("撤销点"))  
        38.         {  
        39.             vertices.RemoveAt(vertices.Count - 1);  
        40.         }  
        41. if (GUILayout.Button("导出mesh"))  
        42.         {  
        43. new Mesh();  
        44.               
        45. int triangleAmount = vertices.Count - 2;  
        46. new int[3 * triangleAmount];  
        47.   
        48. //根据三角形的个数,来计算绘制三角形的顶点顺序(索引)  
        49. //顺序必须为顺时针或者逆时针  
        50. for (int i = 0; i < triangleAmount; i++)  
        51.             {  
        52. //固定第一个点  
        53.                 triangles[3 * i + 1] = i + 1;  
        54.                 triangles[3 * i + 2] = i + 2;  
        55.             }  
        56.   
        57.             mesh.vertices = vertices.ToArray();  
        58.             mesh.triangles = triangles;  
        59.   
        60. "Assets/CustomMesh.asset");  
        61.         }  
        62.         GUILayout.EndArea();  
        63.         Handles.EndGUI();     
        64.     }  
        65. }



        unity2d能用mesh吗_List


        unity2d能用mesh吗_mesh_02

        ///

        实例(属性图的制作):


        unity2d能用mesh吗_顺时针_03


        unity2d能用mesh吗_顺时针_04



        [csharp]  view plain  copy


        1. using UnityEngine;  
        2. using System.Collections.Generic;  
        3.   
        4. [RequireComponent(typeof(MeshFilter))]  
        5. [RequireComponent(typeof(MeshRenderer))]    
        6. public class CreatePentagon : MonoBehaviour {  
        7.   
        8. public Transform[] verticesTra;//赋值顺序为顺时针或者逆时针  
        9. public float[] proportion;//比例  
        10.   
        11. void Start()  
        12.     {  
        13. new List<Vector3>();  
        14. for (int i = 0; i < verticesTra.Length; i++)  
        15.         {  
        16.             Vector3 pos = (verticesTra[i].position - transform.position) * proportion[i];  
        17.             vertices.Add(pos);  
        18.         }  
        19.   
        20.         DrawPentagon(vertices);  
        21.     }  
        22.   
        23. void DrawPentagon(List<Vector3> vertices)  
        24.     {  
        25. new Mesh();  
        26. int triangleAmount = vertices.Count - 2;  
        27. int[] triangles = new int[3 * triangleAmount];  
        28.   
        29. //根据三角形的个数,来计算绘制三角形的顶点顺序(索引)    
        30. //顺序必须为顺时针或者逆时针    
        31. for (int i = 0; i < triangleAmount; i++)  
        32.         {  
        33. //固定第一个点    
        34.             triangles[3 * i + 1] = i + 1;  
        35.             triangles[3 * i + 2] = i + 2;  
        36.         }  
        37.   
        38.         mesh.vertices = vertices.ToArray();  
        39.         mesh.triangles = triangles;  
        40.   
        41.         GetComponent<MeshFilter>().mesh = mesh;  
        42.     }  
        43. }