123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Linq;
-
- namespace KairoEngine.Core
- {
- /// <summary>
- /// Custom attributes
- /// </summary>
-
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
- public class HideInspectorDerived : Attribute
- {
- public readonly Type m_derivedtype;
- public readonly string m_fieldname;
-
- public HideInspectorDerived(Type derivedtype) // Hide from specific derived type
- {
- m_derivedtype = derivedtype;
- m_fieldname = "";
- }
-
- public HideInspectorDerived() // Hide from all descendants
- {
- m_derivedtype = null;
- m_fieldname = "";
- }
-
- public HideInspectorDerived(string fieldName) // Hide field from class
- {
- m_derivedtype = null;
- m_fieldname = fieldName;
- }
- }
-
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
- public class InspectorLabel : Attribute
- {
- public readonly string m_label;
- public readonly Type m_derivedtype;
- public readonly string m_fieldname;
-
- public InspectorLabel(string label) // relabel field
- {
- m_label = label;
- m_derivedtype = null;
- m_fieldname = "";
- }
-
- public InspectorLabel(string label, Type derivedtype) // relabel field for specific derived type
- {
- m_label = label;
- m_derivedtype = derivedtype;
- m_fieldname = "";
- }
-
- public InspectorLabel(string fieldName, string label) // relabel field for class
- {
- m_label = label;
- m_derivedtype = null;
- m_fieldname = fieldName;
- }
- }
-
-
- #if UNITY_EDITOR
-
- [UnityEditor.CustomEditor(typeof(MonoBehaviour), true)]
- [CanEditMultipleObjects()]
- public class CustomInspectorAttribEditor : UnityEditor.Editor
- {
- struct CustomFieldInfo
- {
- public bool m_displayField;
- public string m_label;
- };
-
- Dictionary<string, CustomFieldInfo> m_customField = new Dictionary<string, CustomFieldInfo>();
-
- protected virtual void OnEnable()
- {
- Type monoType = this.target.GetType();
- FieldInfo[] componentFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public);
- FieldInfo[] declaredtFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
- CustomFieldInfo custominfo;
-
- foreach(FieldInfo field in componentFields)
- {
- bool customField = false;
-
- custominfo.m_displayField = false;
- custominfo.m_label = "";
-
- InspectorLabel labelattrib = Attribute.GetCustomAttribute(field, typeof(InspectorLabel)) as InspectorLabel;
-
- if ((labelattrib != null) && ((labelattrib.m_derivedtype == null) || labelattrib.m_derivedtype.IsAssignableFrom(monoType)))
- {
- custominfo.m_label = labelattrib.m_label;
- custominfo.m_displayField = true;
- customField = true;
- }
-
- HideInspectorDerived hidederived = Attribute.GetCustomAttribute(field, typeof(HideInspectorDerived)) as HideInspectorDerived;
-
- if(hidederived != null)
- {
-
- if((hidederived.m_derivedtype != null) && hidederived.m_derivedtype.IsAssignableFrom(monoType))
- {
- custominfo.m_displayField = false;
- customField = true;
- }
- else if((hidederived.m_derivedtype == null) && (!declaredtFields.Contains(field)))
- {
- custominfo.m_displayField = false;
- customField = true;
- }
- }
-
- if(customField && !m_customField.ContainsKey(field.Name))
- m_customField.Add (field.Name, custominfo);
- }
-
- HideInspectorDerived [] hidelist = monoType.GetCustomAttributes(typeof(HideInspectorDerived), false) as HideInspectorDerived[];
- if((hidelist != null) && (hidelist.Length > 0))
- {
- custominfo.m_displayField = false;
- custominfo.m_label = "";
-
- foreach(HideInspectorDerived hide in hidelist)
- {
- if(hide.m_fieldname != "")
- {
-
- if(m_customField.ContainsKey(hide.m_fieldname))
- {
- m_customField[hide.m_fieldname] = custominfo; // overwrite existing info...
- }
- else
- {
- m_customField.Add (hide.m_fieldname, custominfo);
- }
- }
- }
- }
-
- InspectorLabel [] labellist = monoType.GetCustomAttributes(typeof(InspectorLabel), false) as InspectorLabel[];
- if((labellist != null) && (labellist.Length > 0))
- {
- custominfo.m_displayField = true;
- custominfo.m_label = "";
-
- foreach(InspectorLabel label in labellist)
- {
- if((label.m_fieldname != "") && (label.m_label != ""))
- {
- custominfo.m_label = label.m_label;
- if(m_customField.ContainsKey(label.m_fieldname))
- {
- m_customField[label.m_fieldname] = custominfo; // overwrite existing info...
- }
- else
- {
- m_customField.Add (label.m_fieldname, custominfo);
- }
- }
- }
- }
- }
- ////////////////////////////////
-
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- }
- ////////////////////////////////
-
- public new bool DrawDefaultInspector()
- {
- this.serializedObject.Update();
-
- if (this.serializedObject == null)
- throw new System.ArgumentNullException("serializedObject");
-
- MonoBehaviour mono = serializedObject.targetObject as MonoBehaviour;
- Type monoType = mono.GetType();
-
- EditorGUI.BeginChangeCheck();
-
- SerializedProperty iter = serializedObject.GetIterator();
- GUIContent label = new GUIContent();
- CustomFieldInfo info;
- bool enterChildren = true;
-
- while(iter.NextVisible(enterChildren))
- {
- if(m_customField != null && m_customField.TryGetValue(iter.name, out info))
- {
- if(info.m_displayField)
- {
- if(info.m_label != "")
- {
- label.text = info.m_label;
- EditorGUILayout.PropertyField(iter, label, true);
- }
- else
- {
- EditorGUILayout.PropertyField(iter, true);
- }
- }
- }
- else
- {
- EditorGUILayout.PropertyField(iter, true);
- }
-
- enterChildren = false;
- }
-
- bool result = EditorGUI.EndChangeCheck();
- this.serializedObject.ApplyModifiedProperties();
-
- return result;
- }
- }
- #endif
-
- }
|