using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; [System.Serializable] public class People { public GameObject person; public string name; public int age; public float height; public Vector3 location; public int[] itemarray = new int[0]; } public class PeopleList : MonoBehaviour { public List<People> people_list = new List<People>(); // Use this for initialization void AddNew() { people_list.Add(new People()); } // Update is called once per frame void Remove(int index) { people_list.RemoveAt(index); } } [CustomEditor(typeof(PeopleList))] public class PeopleListEditor : Editor { PeopleList N; SerializedObject GetTarget; SerializedProperty TheNewList; int ListSize; void OnEnable() { N = (PeopleList)target; GetTarget = new SerializedObject(N); TheNewList = GetTarget.FindProperty("people_list");// Find the List in our script and create a refrence of it } public override void OnInspectorGUI() { GetTarget.Update(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Define the list size with a number"); ListSize = TheNewList.arraySize; ListSize = EditorGUILayout.IntField("List Size", ListSize); if (ListSize != TheNewList.arraySize) { while (ListSize > TheNewList.arraySize) { TheNewList.InsertArrayElementAtIndex(TheNewList.arraySize); } while (ListSize < TheNewList.arraySize) { TheNewList.DeleteArrayElementAtIndex(TheNewList.arraySize - 1); } } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); //Or add a new item to the List<> with a button EditorGUILayout.LabelField("Add a new item with a button"); if (GUILayout.Button("Add New")) { N.people_list.Add(new People()); } EditorGUILayout.Space(); EditorGUILayout.Space(); for (int i = 0; i < TheNewList.arraySize; i++) { SerializedProperty TheNewListRef = TheNewList.GetArrayElementAtIndex(i); SerializedProperty Person = TheNewListRef.FindPropertyRelative("person"); SerializedProperty Name = TheNewListRef.FindPropertyRelative("name"); SerializedProperty Age = TheNewListRef.FindPropertyRelative("age"); SerializedProperty Height = TheNewListRef.FindPropertyRelative("height"); SerializedProperty Location = TheNewListRef.FindPropertyRelative("location"); SerializedProperty Itemarray = TheNewListRef.FindPropertyRelative("itemarray"); EditorGUILayout.LabelField("List (" + i.ToString() + ")"); Person.objectReferenceValue = EditorGUILayout.ObjectField("Person Object", Person.objectReferenceValue, typeof(GameObject), true); Name.stringValue = EditorGUILayout.TextField("Person Name", Name.stringValue); Age.intValue = EditorGUILayout.IntField("Person Age", Age.intValue); Height.floatValue = EditorGUILayout.FloatField("Person Height", Height.floatValue); Location.vector3Value = EditorGUILayout.Vector3Field("Person Location", Location.vector3Value); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Item Array"); if (GUILayout.Button("Add New Item", GUILayout.MaxWidth(130), GUILayout.MaxHeight(20))) { Itemarray.InsertArrayElementAtIndex(Itemarray.arraySize); Itemarray.GetArrayElementAtIndex(Itemarray.arraySize - 1).intValue = 0; } for (int a = 0; a < Itemarray.arraySize; a++) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Item Index (" + a.ToString() + ")", GUILayout.MaxWidth(120)); Itemarray.GetArrayElementAtIndex(a).intValue = EditorGUILayout.IntField("", Itemarray.GetArrayElementAtIndex(a).intValue, GUILayout.MaxWidth(100)); if (GUILayout.Button("-", GUILayout.MaxWidth(15), GUILayout.MaxHeight(15))) { Itemarray.DeleteArrayElementAtIndex(a); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); //Remove this index from the List EditorGUILayout.LabelField("Remove an index from the List<> with a button"); if (GUILayout.Button("Remove This Index (" + i.ToString() + ")")) { TheNewList.DeleteArrayElementAtIndex(i); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); } GetTarget.ApplyModifiedProperties(); } }
月份: 五月 2016
C#-屬性Property(get與set的另一種形式)
本篇將提到的屬性是一種get與set的便利方式。透過這種方式能簡化原本較複雜的程式碼,使用上更為方便。閱讀更多»
C#-Set與Get基本範例(2)
本篇是將Set、Get方法與enum做搭配的範例。閱讀更多»
C#-MoneyUpdate(金幣跳動效果)
本篇主要說明金幣跳動效果的程式碼架構,我們先來看完成的效果。
閱讀更多»