[返回首页 ] 版权声明
加入收藏 网站地图
关于我们 联系我们
   最新文章
   推荐文章
   点击排行
Unity3D笔记
Unity3D 代码创建模型
来源:霜叶工作站 点击次数: 发布时间:2016-05-21 18:15:46

Unity支持代码创建模型Mesh物体,具体代码如下:

Star.cs

using System; 
using UnityEngine; 
 
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] 
public class Star : MonoBehaviour { 
 
    [Serializable] 
    public class Point { 
        public Color color; 
        public Vector3 offset; 
    } 
 
    public Point[] points; 
    public int frequency = 1; 
    public Color centerColor; 
 
    private Mesh mesh; 
    private Vector3[] vertices; 
    private Color[] colors; 
    private int[] triangles; 
 
    void Start () { 
        GetComponent<MeshFilter>().mesh = mesh = new Mesh(); 
        mesh.name = "Star Mesh"; 
 
        if(frequency < 1){ 
            frequency = 1; 
        } 
        if(points == null || points.Length == 0){ 
            points = new Point[]{ new Point()}; 
        } 
 
        int numberOfPoints = frequency * points.Length; 
        vertices = new Vector3[numberOfPoints + 1]; 
        colors = new Color[numberOfPoints + 1]; 
        triangles = new int[numberOfPoints * 3]; 
        float angle = -360f / numberOfPoints; 
        colors[0] = centerColor; 
        for(int iF = 0, v = 1, t = 1; iF < frequency; iF++){ 
            for(int iP = 0; iP < points.Length; iP += 1, v += 1, t += 3){ 
                vertices[v] = Quaternion.Euler(0f, 0f, angle * (v - 1)) * points[iP].offset; 
                colors[v] = points[iP].color; 
                triangles[t] = v; 
                triangles[t + 1] = v + 1; 
            } 
        } 
        triangles[triangles.Length - 1] = 1; 
 
        mesh.vertices = vertices; 
        mesh.colors = colors; 
        mesh.triangles = triangles; 
    } 
}


StarInspector.cs
using UnityEditor; 
using UnityEngine; 
 
[CustomEditor(typeof(Star))] 
public class StarInspector : Editor { 
 
    private static GUIContent 
        insertContent = new GUIContent("+", "duplicate this point"), 
        deleteContent = new GUIContent("-", "delete this point"), 
        pointContent = GUIContent.none; 
 
    private static GUILayoutOption 
        buttonWidth = GUILayout.MaxWidth(20f), 
        colorWidth = GUILayout.MaxWidth(50f); 
 
    private SerializedObject star; 
    private SerializedProperty 
        points, 
        frequency, 
        centerColor; 
 
    void OnEnable () { … } 
 
    public override void OnInspectorGUI () { 
        star.Update(); 
 
        GUILayout.Label("Points"); 
        for(int i = 0; i < points.arraySize; i++){ 
            EditorGUILayout.BeginHorizontal(); 
            SerializedProperty point = points.GetArrayElementAtIndex(i); 
            EditorGUILayout.PropertyField(point.FindPropertyRelative("offset"), pointContent); 
            EditorGUILayout.PropertyField(point.FindPropertyRelative("color"), pointContent, colorWidth); 
 
            if(GUILayout.Button(insertContent, EditorStyles.miniButtonLeft, buttonWidth)){ 
                points.InsertArrayElementAtIndex(i); 
            } 
            if(GUILayout.Button(deleteContent, EditorStyles.miniButtonRight, buttonWidth)){ 
                points.DeleteArrayElementAtIndex(i); 
            } 
 
            EditorGUILayout.EndHorizontal(); 
        } 
 
        EditorGUILayout.PropertyField(frequency); 
        EditorGUILayout.PropertyField(centerColor); 
 
        star.ApplyModifiedProperties(); 
    } 
}