[Unity3D]编辑器扩展之数组或List显示
效果如下:
源码如下:
1 using System.Collections.Generic; 2 using UnityEditor; 3 using UnityEngine; 4 5 namespace XM.Editor 6 { 7 public class AssetBundleCreator : EditorWindow 8 { 9 [MenuItem("Tools/Build Asset Bundle")] 10 public static void BuildAssetBundle() 11 { 12 var win = GetWindow<AssetBundleCreator>("Build Asset Bundle"); 13 win.Show(); 14 } 15 16 [SerializeField]//必须要加 17 protected List<UnityEngine.Object> _assetLst = new List<UnityEngine.Object>(); 18 19 //序列化对象 20 protected SerializedObject _serializedObject; 21 22 //序列化属性 23 protected SerializedProperty _assetLstProperty; 24 25 26 protected void OnEnable() 27 { 28 //使用当前类初始化 29 _serializedObject = new SerializedObject(this); 30 //获取当前类中可序列话的属性 31 _assetLstProperty = _serializedObject.FindProperty("_assetLst"); 32 } 33 34 protected void OnGUI() 35 { 36 //更新 37 _serializedObject.Update(); 38 39 //开始检查是否有修改 40 EditorGUI.BeginChangeCheck(); 41 42 //显示属性 43 //第二个参数必须为true,否则无法显示子节点即List内容 44 EditorGUILayout.PropertyField(_assetLstProperty, true); 45 46 //结束检查是否有修改 47 if (EditorGUI.EndChangeCheck()) 48 {//提交修改 49 _serializedObject.ApplyModifiedProperties(); 50 } 51 } 52 } 53 }
上述原文地址http://blog.csdn.net/u012741077/article/details/54884623
------------------------------------修改-------------------------------------------------
需要在你修改面板的脚本里面定义变量(目前只实验成功了List),并要序列化,然后在对应的编辑器文件里面序列化这个脚本
1 SerializedObject serializedObject = new UnityEditor.SerializedObject(itemCollider); 2 SerializedProperty serializedPropertyMyInt = serializedObject.FindProperty("HideObjForOnEnable"); 3 if (itemCollider.IsOnEnableHideObj = EditorGUILayout.Toggle(new GUIContent("Is On Disable Hide Obj", "是否添加激活隐藏物体事件"), itemCollider.IsOnEnableHideObj)) 4 { 5 //开始检查是否有修改 6 EditorGUI.BeginChangeCheck(); 7 8 //显示属性 9 //第二个参数必须为true,否则无法显示子节点即List内容 10 EditorGUILayout.PropertyField(serializedPropertyMyInt, new GUIContent("Hide Obj For On Disable", "隐藏物体"), true); 11 12 //结束检查是否有修改 13 if (EditorGUI.EndChangeCheck()) 14 {//提交修改 15 serializedObject.ApplyModifiedProperties(); 16 }
效果如下:
经实验,直接写在OnInspectorGUI()方法内也是没问题的