HideInspectorDerivedAttribute.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Linq;
  8. namespace KairoEngine.Core
  9. {
  10. /// <summary>
  11. /// Custom attributes
  12. /// </summary>
  13. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
  14. public class HideInspectorDerived : Attribute
  15. {
  16. public readonly Type m_derivedtype;
  17. public readonly string m_fieldname;
  18. public HideInspectorDerived(Type derivedtype) // Hide from specific derived type
  19. {
  20. m_derivedtype = derivedtype;
  21. m_fieldname = "";
  22. }
  23. public HideInspectorDerived() // Hide from all descendants
  24. {
  25. m_derivedtype = null;
  26. m_fieldname = "";
  27. }
  28. public HideInspectorDerived(string fieldName) // Hide field from class
  29. {
  30. m_derivedtype = null;
  31. m_fieldname = fieldName;
  32. }
  33. }
  34. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
  35. public class InspectorLabel : Attribute
  36. {
  37. public readonly string m_label;
  38. public readonly Type m_derivedtype;
  39. public readonly string m_fieldname;
  40. public InspectorLabel(string label) // relabel field
  41. {
  42. m_label = label;
  43. m_derivedtype = null;
  44. m_fieldname = "";
  45. }
  46. public InspectorLabel(string label, Type derivedtype) // relabel field for specific derived type
  47. {
  48. m_label = label;
  49. m_derivedtype = derivedtype;
  50. m_fieldname = "";
  51. }
  52. public InspectorLabel(string fieldName, string label) // relabel field for class
  53. {
  54. m_label = label;
  55. m_derivedtype = null;
  56. m_fieldname = fieldName;
  57. }
  58. }
  59. #if UNITY_EDITOR
  60. [UnityEditor.CustomEditor(typeof(MonoBehaviour), true)]
  61. [CanEditMultipleObjects()]
  62. public class CustomInspectorAttribEditor : UnityEditor.Editor
  63. {
  64. struct CustomFieldInfo
  65. {
  66. public bool m_displayField;
  67. public string m_label;
  68. };
  69. Dictionary<string, CustomFieldInfo> m_customField = new Dictionary<string, CustomFieldInfo>();
  70. protected virtual void OnEnable()
  71. {
  72. Type monoType = this.target.GetType();
  73. FieldInfo[] componentFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public);
  74. FieldInfo[] declaredtFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
  75. CustomFieldInfo custominfo;
  76. foreach(FieldInfo field in componentFields)
  77. {
  78. bool customField = false;
  79. custominfo.m_displayField = false;
  80. custominfo.m_label = "";
  81. InspectorLabel labelattrib = Attribute.GetCustomAttribute(field, typeof(InspectorLabel)) as InspectorLabel;
  82. if ((labelattrib != null) && ((labelattrib.m_derivedtype == null) || labelattrib.m_derivedtype.IsAssignableFrom(monoType)))
  83. {
  84. custominfo.m_label = labelattrib.m_label;
  85. custominfo.m_displayField = true;
  86. customField = true;
  87. }
  88. HideInspectorDerived hidederived = Attribute.GetCustomAttribute(field, typeof(HideInspectorDerived)) as HideInspectorDerived;
  89. if(hidederived != null)
  90. {
  91. if((hidederived.m_derivedtype != null) && hidederived.m_derivedtype.IsAssignableFrom(monoType))
  92. {
  93. custominfo.m_displayField = false;
  94. customField = true;
  95. }
  96. else if((hidederived.m_derivedtype == null) && (!declaredtFields.Contains(field)))
  97. {
  98. custominfo.m_displayField = false;
  99. customField = true;
  100. }
  101. }
  102. if(customField && !m_customField.ContainsKey(field.Name))
  103. m_customField.Add (field.Name, custominfo);
  104. }
  105. HideInspectorDerived [] hidelist = monoType.GetCustomAttributes(typeof(HideInspectorDerived), false) as HideInspectorDerived[];
  106. if((hidelist != null) && (hidelist.Length > 0))
  107. {
  108. custominfo.m_displayField = false;
  109. custominfo.m_label = "";
  110. foreach(HideInspectorDerived hide in hidelist)
  111. {
  112. if(hide.m_fieldname != "")
  113. {
  114. if(m_customField.ContainsKey(hide.m_fieldname))
  115. {
  116. m_customField[hide.m_fieldname] = custominfo; // overwrite existing info...
  117. }
  118. else
  119. {
  120. m_customField.Add (hide.m_fieldname, custominfo);
  121. }
  122. }
  123. }
  124. }
  125. InspectorLabel [] labellist = monoType.GetCustomAttributes(typeof(InspectorLabel), false) as InspectorLabel[];
  126. if((labellist != null) && (labellist.Length > 0))
  127. {
  128. custominfo.m_displayField = true;
  129. custominfo.m_label = "";
  130. foreach(InspectorLabel label in labellist)
  131. {
  132. if((label.m_fieldname != "") && (label.m_label != ""))
  133. {
  134. custominfo.m_label = label.m_label;
  135. if(m_customField.ContainsKey(label.m_fieldname))
  136. {
  137. m_customField[label.m_fieldname] = custominfo; // overwrite existing info...
  138. }
  139. else
  140. {
  141. m_customField.Add (label.m_fieldname, custominfo);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. ////////////////////////////////
  148. public override void OnInspectorGUI()
  149. {
  150. DrawDefaultInspector();
  151. }
  152. ////////////////////////////////
  153. public new bool DrawDefaultInspector()
  154. {
  155. this.serializedObject.Update();
  156. if (this.serializedObject == null)
  157. throw new System.ArgumentNullException("serializedObject");
  158. MonoBehaviour mono = serializedObject.targetObject as MonoBehaviour;
  159. Type monoType = mono.GetType();
  160. EditorGUI.BeginChangeCheck();
  161. SerializedProperty iter = serializedObject.GetIterator();
  162. GUIContent label = new GUIContent();
  163. CustomFieldInfo info;
  164. bool enterChildren = true;
  165. while(iter.NextVisible(enterChildren))
  166. {
  167. if(m_customField != null && m_customField.TryGetValue(iter.name, out info))
  168. {
  169. if(info.m_displayField)
  170. {
  171. if(info.m_label != "")
  172. {
  173. label.text = info.m_label;
  174. EditorGUILayout.PropertyField(iter, label, true);
  175. }
  176. else
  177. {
  178. EditorGUILayout.PropertyField(iter, true);
  179. }
  180. }
  181. }
  182. else
  183. {
  184. EditorGUILayout.PropertyField(iter, true);
  185. }
  186. enterChildren = false;
  187. }
  188. bool result = EditorGUI.EndChangeCheck();
  189. this.serializedObject.ApplyModifiedProperties();
  190. return result;
  191. }
  192. }
  193. #endif
  194. }