Unity属性面板扩展

InspectorAttribute.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using UnityEngine;
  6. namespace TModule
  7. {
  8. public enum DrawMode
  9. {
  10. EditModeAndRunTime,
  11. OnlyEditMode,
  12. OnlyRuntime
  13. }
  14. [Conditional("UNITY_EDITOR")]
  15. [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
  16. public class InspectorAttribute : UnityEngine.PropertyAttribute
  17. {
  18. /// <summary>
  19. /// 显示模式
  20. /// </summary>
  21. public DrawMode DrawMode = DrawMode.EditModeAndRunTime;
  22. /// <summary>
  23. /// 是否启用复制按钮
  24. /// </summary>
  25. public bool IsEnableCopy = false;
  26. /// <summary>
  27. /// 是否启用粘贴
  28. /// </summary>
  29. public bool IsEnadlePaste = false;
  30. /// <summary>
  31. /// 是否显示
  32. /// </summary>
  33. public bool IsShow = true;
  34. }
  35. /// <summary>
  36. /// 标签
  37. /// </summary>
  38. public class LabelAttribute : InspectorAttribute
  39. {
  40. public string Text;
  41. public LabelAttribute(string text)
  42. {
  43. Text = text;
  44. }
  45. }
  46. /// <summary>
  47. /// 下拉菜单
  48. /// </summary>
  49. public class DropdownAttribute : InspectorAttribute
  50. {
  51. public List<string> DisplayOptions { get; private set; } = new List<string>();
  52. public List<object> Values { get; private set; } = new List<object>();
  53. public DropdownAttribute(IEnumerable<string> displayOptions, IEnumerable<object> values)
  54. {
  55. DisplayOptions.AddRange(displayOptions);
  56. Values.AddRange(values);
  57. }
  58. public DropdownAttribute() { }
  59. }
  60. /// <summary>
  61. /// 将某个类型的子类做为下拉菜单
  62. /// </summary>
  63. public class TypeDropdownAttribute : DropdownAttribute
  64. {
  65. public Type ValueType;
  66. public TypeDropdownAttribute(Type valueType)
  67. {
  68. Values.Add("None");
  69. DisplayOptions.Add("None");
  70. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  71. {
  72. foreach (var type in assembly.GetTypes())
  73. {
  74. if (type.IsSubclassOf(valueType) || (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == valueType))//判断type是否是其派生类
  75. if (type.IsClass && !type.IsAbstract && type.Name != valueType.Name)
  76. {
  77. Values.Add(type.FullName);
  78. DisplayOptions.Add(type.FullName);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// 场景资源路径
  86. /// </summary>
  87. public class SceneAttribute : InspectorAttribute
  88. {
  89. }
  90. public class MethodAttribute:Attribute
  91. {
  92. }
  93. /// <summary>
  94. /// 在字段后添加一个事件按钮
  95. /// </summary>
  96. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
  97. public class EventButtonAttribute : InspectorAttribute
  98. {
  99. public string Lable;
  100. public string Style;
  101. public string ActionName;
  102. public string Tip;
  103. public Texture Texture;
  104. public Type TypeValue;
  105. public EventButtonAttribute(string lable, string style, string actionName, Type type)
  106. {
  107. Lable = lable;
  108. Style = style;
  109. ActionName = actionName;
  110. TypeValue = type;
  111. }
  112. public EventButtonAttribute(string lable, string tip, Texture texture, string actionName, Type type)
  113. {
  114. Lable = lable;
  115. Tip = tip;
  116. Texture = texture;
  117. ActionName = actionName;
  118. TypeValue = type;
  119. }
  120. }
  121. /// <summary>
  122. /// 方法按钮
  123. /// </summary>
  124. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  125. public class ButtonAttribute : MethodAttribute
  126. {
  127. public string Name;
  128. public string CriteriaName;
  129. public bool CriteriaVaule;
  130. public object[] Parameter;
  131. public ButtonAttribute(params object[] parameter)
  132. {
  133. Parameter = parameter;
  134. }
  135. public ButtonAttribute(string buttonName, params object[] parameter)
  136. {
  137. Name = buttonName;
  138. Parameter = parameter;
  139. }
  140. /// <summary>
  141. ///
  142. /// </summary>
  143. /// <param name="buttonName">按钮显示文字</param>
  144. /// <param name="criteraName">激活按钮条件名字 可是字段,属性,方法(必须有返回值且返回值为bool)</param>
  145. /// <param name="criteriaValue">激活条件值</param>
  146. /// <param name="parameter">参数</param>
  147. public ButtonAttribute(string buttonName, string criteraName, bool criteriaValue = true, params object[] parameter)
  148. {
  149. Name = buttonName;
  150. CriteriaName = criteraName;
  151. CriteriaVaule = criteriaValue;
  152. Parameter = parameter;
  153. }
  154. }
  155. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
  156. public class GetTransFormValueAttribute : InspectorAttribute
  157. {
  158. public TranType TranType;
  159. /// <summary>
  160. /// 获取选中对象的Transform值
  161. /// </summary>
  162. /// <param name="tranType">要获取的Transform值TranType类型</param>
  163. public GetTransFormValueAttribute(TranType tranType)
  164. {
  165. TranType = tranType;
  166. }
  167. }
  168. public enum TranType
  169. {
  170. None,
  171. Position,
  172. Rotation,
  173. Scale
  174. }
  175. /// <summary>
  176. /// 属性
  177. /// </summary>
  178. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  179. public class PropertyAttribute:InspectorAttribute
  180. {
  181. public string Lable;
  182. public PropertyAttribute()
  183. {
  184. }
  185. public PropertyAttribute(string lable = "")
  186. {
  187. Lable = lable;
  188. }
  189. }
  190. /// <summary>
  191. /// 关联显示条件
  192. /// </summary>
  193. public class RelevancyShowAttribute : InspectorAttribute
  194. {
  195. public string FieldName;
  196. public object ShowValue;
  197. /// <summary>
  198. /// 关联字段/属性显示条件
  199. /// </summary>
  200. /// <param name="fieldName">关联字段名字</param>
  201. /// <param name="showValue">字段值为何值时显示</param>
  202. public RelevancyShowAttribute(string fieldName, object showValue)
  203. {
  204. FieldName = fieldName;
  205. ShowValue = showValue;
  206. }
  207. }
  208. public class OnlyReadAttribute : InspectorAttribute
  209. {
  210. }
  211. public class BegingFoldAttribute:InspectorAttribute
  212. {
  213. public string Label;
  214. public BegingFoldAttribute(string label)
  215. {
  216. Label = label;
  217. }
  218. }
  219. public class EndFoldAttribute : InspectorAttribute
  220. {
  221. }
  222. }