Unity属性面板扩展

ObjectInspector.cs 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using UnityEditor.SceneManagement;
  7. using System.Reflection;
  8. using UObject = UnityEngine.Object;
  9. using UnityEngine.UIElements;
  10. namespace TModule.Editor
  11. {
  12. [CustomEditor(typeof(UObject),true)]
  13. public class ObjectInspector : UnityEditor.Editor
  14. {
  15. BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  16. List<MethodDrawer> methodDrawers = new List<MethodDrawer>();
  17. List<PropertyInfo> propertyInfos = new List<PropertyInfo>();
  18. List<PropertyDrawer> propertyDrawers = new List<PropertyDrawer>();
  19. Dictionary<Type,Type> methodDrawerTypes = new Dictionary<Type,Type>();
  20. Dictionary<Type, Type> propertyDrawerTypes = new Dictionary<Type, Type>();
  21. private void OnEnable()
  22. {
  23. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  24. {
  25. foreach (var type in assembly.GetTypes())
  26. {
  27. if (type.IsDefined(typeof(CustomMethodDrawerAttribute), true))
  28. {
  29. Type type1 = type.GetCustomAttribute<CustomMethodDrawerAttribute>().AttributeType;
  30. if (methodDrawerTypes.ContainsKey(type1))
  31. methodDrawerTypes[type1] = type;
  32. else
  33. methodDrawerTypes.Add(type1, type);
  34. }
  35. if (type.IsDefined(typeof(TCustomPropertyDrawerAttribute), true))
  36. {
  37. Type type1 = type.GetCustomAttribute<TCustomPropertyDrawerAttribute>().AttributeType;
  38. if (propertyDrawerTypes.ContainsKey(type1))
  39. propertyDrawerTypes[type1] = type;
  40. else
  41. propertyDrawerTypes.Add(type1, type);
  42. }
  43. }
  44. }
  45. foreach (var method in target.GetType().GetMethods(flags))
  46. {
  47. if(method.IsDefined(typeof(MethodAttribute), true))
  48. {
  49. MethodAttribute attribute = method.GetCustomAttribute<MethodAttribute>();
  50. if (!methodDrawerTypes.ContainsKey(attribute.GetType())) continue;
  51. MethodDrawer methodDrawer = Activator.CreateInstance(methodDrawerTypes[attribute.GetType()]) as MethodDrawer;
  52. if (methodDrawer != null)
  53. {
  54. methodDrawer.attribute = attribute;
  55. methodDrawer.methodInfo = method;
  56. methodDrawers.Add(methodDrawer);
  57. }
  58. }
  59. }
  60. foreach (var property in target.GetType().GetProperties(flags))
  61. {
  62. if (property.IsDefined(typeof(PropertyAttribute), true))
  63. {
  64. PropertyAttribute attribute = property.GetCustomAttribute<PropertyAttribute>();
  65. if (!propertyDrawerTypes.ContainsKey(attribute.GetType())) continue;
  66. PropertyDrawer propertyDrawer = Activator.CreateInstance(propertyDrawerTypes[attribute.GetType()]) as PropertyDrawer;
  67. if (propertyDrawer != null)
  68. {
  69. propertyDrawer.attribute = attribute;
  70. propertyDrawer.propertyInfo = property;
  71. propertyDrawers.Add(propertyDrawer);
  72. }
  73. }
  74. }
  75. }
  76. public override void OnInspectorGUI()
  77. {
  78. base.OnInspectorGUI();
  79. foreach (var property in propertyDrawers)
  80. {
  81. property.OnGUI(serializedObject);
  82. }
  83. foreach (var method in methodDrawers)
  84. {
  85. method.OnGUI(serializedObject);
  86. }
  87. }
  88. }
  89. public abstract class BaseAttributeDrawer : UnityEditor.PropertyDrawer
  90. {
  91. Rect copyRect = Rect.zero;
  92. Rect pasteRect = Rect.zero;
  93. public UObject target;
  94. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  95. {
  96. InspectorAttribute _attribute = (InspectorAttribute)attribute;
  97. if (_attribute is BegingFoldAttribute && property.isExpanded)
  98. {
  99. if (_attribute.IsShow)
  100. return base.GetPropertyHeight(property, label);
  101. else
  102. return 0;
  103. }
  104. else
  105. return base.GetPropertyHeight(property, label);
  106. }
  107. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  108. {
  109. if (property != null) target = property.serializedObject.targetObject;
  110. InspectorAttribute _attribute = (InspectorAttribute)attribute;
  111. OnDarwBefore(position, property, label);
  112. if (!_attribute.IsShow) return;
  113. if (_attribute is BegingFoldAttribute && !property.isExpanded) return;
  114. float _width = position.width;
  115. _width -= (Convert.ToInt32(_attribute.IsEnableCopy) + Convert.ToInt32(_attribute.IsEnadlePaste)) * 43;
  116. float _btnWidth = _width;
  117. if (_attribute.IsEnableCopy)
  118. {
  119. _btnWidth += 23;
  120. copyRect.Set(_btnWidth, position.y, 40, position.height);
  121. if (GUI.Button(copyRect, "¸´ÖÆ"))
  122. Copy(property);
  123. _btnWidth += 43;
  124. }
  125. if(_attribute.IsEnadlePaste)
  126. {
  127. pasteRect.Set(_btnWidth, position.y, 40, position.height);
  128. if (GUI.Button(pasteRect, "Õ³Ìù"))
  129. Paste(property);
  130. }
  131. position.width = _width;
  132. switch (_attribute.DrawMode)
  133. {
  134. case DrawMode.EditModeAndRunTime:
  135. OnDarw(position, property, label);
  136. break;
  137. case DrawMode.OnlyEditMode:
  138. if (!EditorApplication.isPlaying)
  139. OnDarwEditMode(position, property, label);
  140. break;
  141. case DrawMode.OnlyRuntime:
  142. if (EditorApplication.isPlaying)
  143. OnDarwRuntime(position, property, label);
  144. break;
  145. default:
  146. base.OnGUI(position, property, label);
  147. break;
  148. }
  149. }
  150. public virtual void OnDarwBefore(Rect position, SerializedProperty property, GUIContent label)
  151. {
  152. }
  153. public virtual void Paste(SerializedProperty property)
  154. {
  155. property.serializedObject.Update();
  156. switch (property.propertyType)
  157. {
  158. case SerializedPropertyType.Generic:
  159. break;
  160. case SerializedPropertyType.Integer:
  161. if (int.TryParse(EditorGUIUtility.systemCopyBuffer, out int v))
  162. property.intValue = v;
  163. break;
  164. case SerializedPropertyType.Boolean:
  165. if (bool.TryParse(EditorGUIUtility.systemCopyBuffer, out bool b))
  166. property.boolValue = b;
  167. break;
  168. case SerializedPropertyType.Float:
  169. string fstr = EditorGUIUtility.systemCopyBuffer.Replace("f", "");
  170. if (float.TryParse(fstr, out float f))
  171. property.floatValue = f;
  172. break;
  173. case SerializedPropertyType.String:
  174. property.stringValue = EditorGUIUtility.systemCopyBuffer;
  175. break;
  176. case SerializedPropertyType.Color:
  177. property.colorValue = JsonUtility.FromJson<Color>(EditorGUIUtility.systemCopyBuffer);
  178. break;
  179. case SerializedPropertyType.ObjectReference:
  180. property.objectReferenceValue = JsonUtility.FromJson<UnityEngine.Object>(EditorGUIUtility.systemCopyBuffer);
  181. break;
  182. case SerializedPropertyType.LayerMask:
  183. break;
  184. case SerializedPropertyType.Enum:
  185. property.enumValueIndex = JsonUtility.FromJson<int>(EditorGUIUtility.systemCopyBuffer);
  186. break;
  187. case SerializedPropertyType.Vector2:
  188. property.vector2Value = JsonUtility.FromJson<Vector2>(EditorGUIUtility.systemCopyBuffer);
  189. break;
  190. case SerializedPropertyType.Vector3:
  191. property.vector3Value = JsonUtility.FromJson<Vector3>(EditorGUIUtility.systemCopyBuffer);
  192. break;
  193. case SerializedPropertyType.Vector4:
  194. property.vector4Value = JsonUtility.FromJson<Vector4>(EditorGUIUtility.systemCopyBuffer);
  195. break;
  196. case SerializedPropertyType.Rect:
  197. property.rectValue = JsonUtility.FromJson<Rect>(EditorGUIUtility.systemCopyBuffer);
  198. break;
  199. case SerializedPropertyType.ArraySize:
  200. property.arraySize = JsonUtility.FromJson<int>(EditorGUIUtility.systemCopyBuffer);
  201. break;
  202. case SerializedPropertyType.Character:
  203. break;
  204. case SerializedPropertyType.AnimationCurve:
  205. property.animationCurveValue = JsonUtility.FromJson<AnimationCurve>(EditorGUIUtility.systemCopyBuffer);
  206. break;
  207. case SerializedPropertyType.Bounds:
  208. property.boundsValue = JsonUtility.FromJson<Bounds>(EditorGUIUtility.systemCopyBuffer);
  209. break;
  210. case SerializedPropertyType.Gradient:
  211. break;
  212. case SerializedPropertyType.Quaternion:
  213. property.quaternionValue = JsonUtility.FromJson<Quaternion>(EditorGUIUtility.systemCopyBuffer);
  214. break;
  215. case SerializedPropertyType.ExposedReference:
  216. property.exposedReferenceValue = JsonUtility.FromJson<UnityEngine.Object>(EditorGUIUtility.systemCopyBuffer);
  217. break;
  218. case SerializedPropertyType.FixedBufferSize:
  219. break;
  220. case SerializedPropertyType.Vector2Int:
  221. property.vector2IntValue = JsonUtility.FromJson<Vector2Int>(EditorGUIUtility.systemCopyBuffer);
  222. break;
  223. case SerializedPropertyType.Vector3Int:
  224. property.vector3IntValue = JsonUtility.FromJson<Vector3Int>(EditorGUIUtility.systemCopyBuffer);
  225. break;
  226. case SerializedPropertyType.RectInt:
  227. property.rectIntValue = JsonUtility.FromJson<RectInt>(EditorGUIUtility.systemCopyBuffer);
  228. break;
  229. case SerializedPropertyType.BoundsInt:
  230. property.boundsIntValue = JsonUtility.FromJson<BoundsInt>(EditorGUIUtility.systemCopyBuffer);
  231. break;
  232. case SerializedPropertyType.ManagedReference:
  233. property.managedReferenceValue = JsonUtility.FromJson<object>(EditorGUIUtility.systemCopyBuffer);
  234. break;
  235. case SerializedPropertyType.Hash128:
  236. property.hash128Value = JsonUtility.FromJson<Hash128>(EditorGUIUtility.systemCopyBuffer);
  237. break;
  238. default:
  239. break;
  240. }
  241. property.serializedObject.ApplyModifiedProperties();
  242. }
  243. public virtual void Copy(SerializedProperty property)
  244. {
  245. switch (property.propertyType)
  246. {
  247. case SerializedPropertyType.Generic:
  248. break;
  249. case SerializedPropertyType.Integer:
  250. EditorGUIUtility.systemCopyBuffer=property.intValue.ToString();
  251. break;
  252. case SerializedPropertyType.Boolean:
  253. EditorGUIUtility.systemCopyBuffer = property.boolValue.ToString();
  254. break;
  255. case SerializedPropertyType.Float:
  256. EditorGUIUtility.systemCopyBuffer = property.floatValue + "f";
  257. break;
  258. case SerializedPropertyType.String:
  259. EditorGUIUtility.systemCopyBuffer = property.stringValue;
  260. break;
  261. case SerializedPropertyType.Color:
  262. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.colorValue);
  263. break;
  264. case SerializedPropertyType.ObjectReference:
  265. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.objectReferenceValue);
  266. break;
  267. case SerializedPropertyType.LayerMask:
  268. break;
  269. case SerializedPropertyType.Enum:
  270. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.enumValueIndex);
  271. break;
  272. case SerializedPropertyType.Vector2:
  273. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.vector2Value);
  274. break;
  275. case SerializedPropertyType.Vector3:
  276. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.vector3Value);
  277. break;
  278. case SerializedPropertyType.Vector4:
  279. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.vector4Value);
  280. break;
  281. case SerializedPropertyType.Rect:
  282. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.rectValue);
  283. break;
  284. case SerializedPropertyType.ArraySize:
  285. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.arraySize);
  286. break;
  287. case SerializedPropertyType.Character:
  288. break;
  289. case SerializedPropertyType.AnimationCurve:
  290. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.animationCurveValue);
  291. break;
  292. case SerializedPropertyType.Bounds:
  293. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.boundsValue);
  294. break;
  295. case SerializedPropertyType.Gradient:
  296. break;
  297. case SerializedPropertyType.Quaternion:
  298. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.quaternionValue);
  299. break;
  300. case SerializedPropertyType.ExposedReference:
  301. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.exposedReferenceValue);
  302. break;
  303. case SerializedPropertyType.FixedBufferSize:
  304. break;
  305. case SerializedPropertyType.Vector2Int:
  306. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.vector2IntValue);
  307. break;
  308. case SerializedPropertyType.Vector3Int:
  309. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.vector3IntValue);
  310. break;
  311. case SerializedPropertyType.RectInt:
  312. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.rectIntValue);
  313. break;
  314. case SerializedPropertyType.BoundsInt:
  315. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.boundsIntValue);
  316. break;
  317. case SerializedPropertyType.ManagedReference:
  318. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.managedReferenceValue);
  319. break;
  320. case SerializedPropertyType.Hash128:
  321. EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson(property.hash128Value);
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. public virtual void OnDarwRuntime(Rect position, SerializedProperty property, GUIContent label)
  328. {
  329. DrawUI(position, property, label);
  330. }
  331. public virtual void OnDarwEditMode(Rect position, SerializedProperty property, GUIContent label)
  332. {
  333. DrawUI(position, property, label);
  334. }
  335. public virtual void OnDarw(Rect position, SerializedProperty property, GUIContent label)
  336. {
  337. DrawUI(position, property, label);
  338. }
  339. public abstract void DrawUI(Rect position, SerializedProperty property, GUIContent label);
  340. public void MarkerChange()
  341. {
  342. if (target == null) return;
  343. EditorUtility.SetDirty(target);
  344. if (EditorApplication.isPlaying) return;
  345. Component component = target as Component;
  346. if (component != null && component.gameObject.scene != null)
  347. {
  348. EditorSceneManager.MarkSceneDirty(component.gameObject.scene);
  349. }
  350. }
  351. }
  352. public abstract class MethodDrawer
  353. {
  354. public MethodInfo methodInfo;
  355. public MethodAttribute attribute;
  356. public virtual void OnGUI(SerializedObject serializedObject)
  357. {
  358. }
  359. }
  360. public abstract class PropertyDrawer
  361. {
  362. public PropertyInfo propertyInfo;
  363. public PropertyAttribute attribute;
  364. public UObject target;
  365. public EventButtonAttribute buttonAttribute;
  366. public GetTransFormValueAttribute formValueAttribute;
  367. public string Name;
  368. public virtual void OnGUI(SerializedObject serializedObject)
  369. {
  370. target = serializedObject.targetObject;
  371. Name = string.IsNullOrEmpty(attribute.Lable) ? propertyInfo.Name : attribute.Lable;
  372. buttonAttribute = target?.GetType().GetCustomAttribute<EventButtonAttribute>();
  373. formValueAttribute = target?.GetType().GetCustomAttribute<GetTransFormValueAttribute>();
  374. using(new EditorGUILayout.HorizontalScope())
  375. {
  376. switch (attribute.DrawMode)
  377. {
  378. case DrawMode.EditModeAndRunTime:
  379. OnDarw(serializedObject);
  380. break;
  381. case DrawMode.OnlyEditMode:
  382. if (!EditorApplication.isPlaying)
  383. OnDarwEditMode(serializedObject);
  384. break;
  385. case DrawMode.OnlyRuntime:
  386. if (EditorApplication.isPlaying)
  387. OnDarwRuntime(serializedObject);
  388. break;
  389. default:
  390. DrawUI(serializedObject);
  391. break;
  392. }
  393. if (attribute.IsEnableCopy)
  394. {
  395. if (GUILayout.Button("¸´ÖÆ",GUILayout.ExpandWidth(false)))
  396. Copy(serializedObject);
  397. }
  398. if (attribute.IsEnadlePaste)
  399. {
  400. if (GUILayout.Button("Õ³Ìù", GUILayout.ExpandWidth(false)))
  401. Paste(serializedObject);
  402. }
  403. if(buttonAttribute!=null)
  404. {
  405. if(GUILayout.Button(buttonAttribute.Lable, GUILayout.ExpandWidth(false)))
  406. {
  407. buttonAttribute.TypeValue.GetMethod(buttonAttribute.ActionName)?.Invoke(target,null);
  408. }
  409. }
  410. if(formValueAttribute!=null)
  411. {
  412. if(GUILayout.Button(EditorGUIUtility.IconContent("Custom@2x").image, GUILayout.Width(30), GUILayout.Height(20)))
  413. {
  414. Vector3 value = (Vector3)propertyInfo.GetValue(target);
  415. if(Selection.activeTransform)
  416. switch (formValueAttribute.TranType)
  417. {
  418. case TranType.None:
  419. break;
  420. case TranType.Position:
  421. value = Selection.activeTransform.position;
  422. break;
  423. case TranType.Rotation:
  424. value = Selection.activeTransform.rotation.eulerAngles;
  425. break;
  426. case TranType.Scale:
  427. value = Selection.activeTransform.localScale;
  428. break;
  429. default:
  430. break;
  431. }
  432. propertyInfo.SetValue(target, value);
  433. }
  434. }
  435. }
  436. }
  437. public virtual void Paste(SerializedObject serializedObject)
  438. {
  439. try
  440. {
  441. propertyInfo.SetValue(target, EditorGUIUtility.systemCopyBuffer);
  442. MarkerChange();
  443. }
  444. catch
  445. {
  446. }
  447. }
  448. public virtual void Copy(SerializedObject serializedObject)
  449. {
  450. EditorGUIUtility.systemCopyBuffer = propertyInfo.GetValue(target).ToString();
  451. }
  452. public virtual void OnDarwRuntime(SerializedObject serializedObject)
  453. {
  454. DrawUI(serializedObject);
  455. }
  456. public virtual void OnDarwEditMode(SerializedObject serializedObject)
  457. {
  458. DrawUI(serializedObject);
  459. }
  460. public virtual void OnDarw(SerializedObject serializedObject)
  461. {
  462. DrawUI(serializedObject);
  463. }
  464. public void MarkerChange()
  465. {
  466. if (target == null) return;
  467. EditorUtility.SetDirty(target);
  468. if (EditorApplication.isPlaying) return;
  469. Component component = target as Component;
  470. if (component != null && component.gameObject.scene != null)
  471. {
  472. EditorSceneManager.MarkSceneDirty(component.gameObject.scene);
  473. }
  474. }
  475. public abstract void DrawUI(SerializedObject serializedObject);
  476. }
  477. [CustomPropertyDrawer(typeof(LabelAttribute),true)]
  478. public class LabelAttributeDrawer:BaseAttributeDrawer
  479. {
  480. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  481. {
  482. LabelAttribute _attribute = (LabelAttribute)attribute;
  483. label.text = _attribute.Text;
  484. EditorGUI.PropertyField(position, property, label);
  485. }
  486. }
  487. [CustomPropertyDrawer(typeof(EventButtonAttribute), true)]
  488. public class EventButtonAttributeDrawer : BaseAttributeDrawer
  489. {
  490. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  491. {
  492. EventButtonAttribute _attribute = (EventButtonAttribute)attribute;
  493. float width = position.width - 33;
  494. width += 23;
  495. if (GUI.Button(new Rect(width,position.y,30,position.height),_attribute.Lable))
  496. {
  497. _attribute.TypeValue.GetMethod(_attribute.ActionName).Invoke(target, null);
  498. }
  499. EditorGUI.PropertyField(new Rect(position.x,position.y,width-23,position.height), property, label);
  500. }
  501. }
  502. [CustomPropertyDrawer(typeof(DropdownAttribute),true)]
  503. public class DropdownAttributeDrawer : BaseAttributeDrawer
  504. {
  505. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  506. {
  507. object value = fieldInfo.GetValue(property.serializedObject.targetObject);
  508. DropdownAttribute dropdown = attribute as DropdownAttribute;
  509. int selectIndex = dropdown.Values.IndexOf(value);
  510. if (selectIndex < 0)
  511. {
  512. selectIndex = 0;
  513. fieldInfo.SetValue(property.serializedObject.targetObject, dropdown.Values[selectIndex]);
  514. MarkerChange();
  515. }
  516. EditorGUI.BeginChangeCheck();
  517. int newIndex = EditorGUI.Popup(position,label.text, selectIndex, dropdown.DisplayOptions.ToArray());
  518. if (EditorGUI.EndChangeCheck())
  519. {
  520. Undo.RecordObject(target, "Dropdown");
  521. fieldInfo.SetValue(target, dropdown.Values[newIndex]);
  522. MarkerChange();
  523. }
  524. }
  525. }
  526. [CustomPropertyDrawer(typeof(SceneAttribute))]
  527. public class SceneAttributeDrawer : BaseAttributeDrawer
  528. {
  529. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  530. {
  531. object value = fieldInfo.GetValue(target);
  532. EditorGUI.BeginChangeCheck();
  533. SceneAsset scene = EditorGUI.ObjectField(position,label, AssetDatabase.LoadAssetAtPath<SceneAsset>((string)value), typeof(SceneAsset), false) as SceneAsset;
  534. if (EditorGUI.EndChangeCheck())
  535. {
  536. Undo.RecordObject(target, "Scene");
  537. fieldInfo.SetValue(target, AssetDatabase.GetAssetPath(scene));
  538. MarkerChange();
  539. }
  540. }
  541. }
  542. [CustomPropertyDrawer(typeof(RelevancyShowAttribute))]
  543. public class RelevancyShowAttributeDrawer : BaseAttributeDrawer
  544. {
  545. BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Default | BindingFlags.Instance;
  546. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  547. {
  548. EditorGUI.PropertyField(position, property, label);
  549. }
  550. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  551. {
  552. RelevancyShowAttribute _attribute = attribute as RelevancyShowAttribute;
  553. MemberInfo[] member = property.serializedObject.targetObject.GetType().GetMember(_attribute.FieldName, flags);
  554. if (member != null && member.Length > 0)
  555. {
  556. if (member[0].MemberType == MemberTypes.Field)
  557. _attribute.IsShow = (member[0] as FieldInfo).GetValue(property.serializedObject.targetObject).Equals(_attribute.ShowValue);
  558. else if (member[0].MemberType == MemberTypes.Property)
  559. _attribute.IsShow = (member[0] as PropertyInfo).GetValue(property.serializedObject.targetObject).Equals(_attribute.ShowValue);
  560. else if (member[0].MemberType == MemberTypes.Method)
  561. _attribute.IsShow = (member[0] as MethodInfo).Invoke(property.serializedObject.targetObject, null).Equals(_attribute.ShowValue);
  562. }
  563. return base.GetPropertyHeight(property, label);
  564. }
  565. }
  566. [CustomPropertyDrawer(typeof(OnlyReadAttribute))]
  567. public class OnlyReadAttributeDrawer : BaseAttributeDrawer
  568. {
  569. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  570. {
  571. GUI.enabled = false;
  572. EditorGUI.PropertyField(position, property, label);
  573. GUI.enabled = true;
  574. }
  575. }
  576. [CustomPropertyDrawer(typeof(BegingFoldAttribute))]
  577. public class BegingFoldAttributeDrawer : BaseAttributeDrawer
  578. {
  579. int count = 0;
  580. BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  581. public override void OnDarwBefore(Rect position, SerializedProperty property, GUIContent label)
  582. {
  583. BegingFoldAttribute _attribute = attribute as BegingFoldAttribute;
  584. property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, _attribute.Label);
  585. }
  586. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  587. {
  588. position.y += 23;
  589. position.height = 20;
  590. EditorGUI.PropertyField(position, property, label);
  591. }
  592. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  593. {
  594. using (SerializedProperty iterator = property.serializedObject.GetIterator())
  595. {
  596. HashSet<string> fieldPaths = new HashSet<string>();
  597. bool flags1 = false;
  598. while (iterator.NextVisible(true))
  599. {
  600. SerializedProperty _property = property.serializedObject.FindProperty(iterator.name);
  601. if (!flags1)
  602. flags1 = _property.propertyPath == property.propertyPath;
  603. if (_property != null && !fieldPaths.Contains(_property.propertyPath)&&flags1)
  604. {
  605. fieldPaths.Add(_property.propertyPath);
  606. FieldInfo fieldInfo = _property.serializedObject.targetObject.GetType().GetField(_property.name, flags);
  607. InspectorAttribute _attribute = fieldInfo.GetCustomAttribute<InspectorAttribute>();
  608. if(_attribute!=null)
  609. {
  610. if (fieldInfo.GetCustomAttribute<BegingFoldAttribute>() != null)
  611. {
  612. count++;
  613. }
  614. if(fieldInfo.GetCustomAttribute<EndFoldAttribute>() != null)
  615. {
  616. count--;
  617. if (count == 0)
  618. break;
  619. }
  620. }
  621. _property.isExpanded = property.isExpanded;
  622. }
  623. }
  624. }
  625. return base.GetPropertyHeight(property, label)+23;
  626. }
  627. }
  628. [CustomPropertyDrawer(typeof(GetTransFormValueAttribute))]
  629. public class GetTransFormValueAttributeDrawer : BaseAttributeDrawer
  630. {
  631. public override void DrawUI(Rect position, SerializedProperty property, GUIContent label)
  632. {
  633. GetTransFormValueAttribute _attribute = (GetTransFormValueAttribute)attribute;
  634. float width = position.width - 33;
  635. width += 23;
  636. if (GUI.Button(new Rect(width, position.y, 30, position.height), EditorGUIUtility.IconContent("Custom@2x").image))
  637. {
  638. Vector3 value = property.vector3Value;
  639. if (Selection.activeTransform)
  640. switch (_attribute.TranType)
  641. {
  642. case TranType.None:
  643. break;
  644. case TranType.Position:
  645. value = Selection.activeTransform.position;
  646. break;
  647. case TranType.Rotation:
  648. value = Selection.activeTransform.rotation.eulerAngles;
  649. break;
  650. case TranType.Scale:
  651. value = Selection.activeTransform.localScale;
  652. break;
  653. default:
  654. break;
  655. }
  656. property.vector3Value = value;
  657. }
  658. EditorGUI.PropertyField(new Rect(position.x, position.y, width-23, position.height), property, label);
  659. }
  660. }
  661. [TCustomPropertyDrawer(typeof(PropertyAttribute))]
  662. public class PropertyAttributeDrawer : PropertyDrawer
  663. {
  664. public override void DrawUI(SerializedObject serializedObject)
  665. {
  666. if (propertyInfo.CanWrite)
  667. {
  668. CanWritePainting();
  669. }
  670. else
  671. {
  672. ReadOnlyPainting();
  673. }
  674. }
  675. private void CanWritePainting()
  676. {
  677. EditorGUI.BeginChangeCheck();
  678. object value = propertyInfo.GetValue(target);
  679. object newValue = value;
  680. if (propertyInfo.PropertyType.IsEnum)
  681. {
  682. Enum realValue = EditorGUILayout.EnumPopup(Name, (Enum)value);
  683. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  684. }
  685. else if (propertyInfo.PropertyType == typeof(string))
  686. {
  687. string realValue = EditorGUILayout.TextField(Name, (string)value);
  688. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  689. }
  690. else if (propertyInfo.PropertyType == typeof(int))
  691. {
  692. int realValue = EditorGUILayout.IntField(Name, (int)value);
  693. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  694. }
  695. else if (propertyInfo.PropertyType == typeof(float))
  696. {
  697. float realValue = EditorGUILayout.FloatField(Name, (float)value);
  698. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  699. }
  700. else if (propertyInfo.PropertyType == typeof(bool))
  701. {
  702. bool realValue = EditorGUILayout.Toggle(Name, (bool)value);
  703. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  704. }
  705. else if (propertyInfo.PropertyType == typeof(Vector2))
  706. {
  707. Vector2 realValue = EditorGUILayout.Vector2Field(Name, (Vector2)value);
  708. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  709. }
  710. else if (propertyInfo.PropertyType == typeof(Vector3))
  711. {
  712. Vector3 realValue = EditorGUILayout.Vector3Field(Name, (Vector3)value);
  713. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  714. }
  715. else if (propertyInfo.PropertyType == typeof(Color))
  716. {
  717. Color realValue = EditorGUILayout.ColorField(Name, (Color)value);
  718. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  719. }
  720. else if (propertyInfo.PropertyType.IsSubclassOf(typeof(UObject)))
  721. {
  722. UObject realValue = EditorGUILayout.ObjectField(Name, value as UObject, propertyInfo.PropertyType, true);
  723. if (EditorGUI.EndChangeCheck()) newValue = realValue;
  724. }
  725. else
  726. {
  727. string realValue = EditorGUILayout.TextField(Name, value != null ? value.ToString() : "null");
  728. if(EditorGUI.EndChangeCheck()) newValue = realValue;
  729. }
  730. if (value != newValue)
  731. {
  732. Undo.RecordObject(target, "propertyInfo Changed");
  733. propertyInfo.SetValue(target, newValue);
  734. MarkerChange();
  735. }
  736. }
  737. private void ReadOnlyPainting()
  738. {
  739. GUI.enabled = false;
  740. object value = propertyInfo.GetValue(target);
  741. if (propertyInfo.PropertyType.IsEnum)
  742. {
  743. EditorGUILayout.EnumPopup(Name, (Enum)value);
  744. }
  745. else if (propertyInfo.PropertyType == typeof(string))
  746. {
  747. EditorGUILayout.TextField(Name, (string)value);
  748. }
  749. else if (propertyInfo.PropertyType == typeof(int))
  750. {
  751. EditorGUILayout.IntField(Name, (int)value);
  752. }
  753. else if (propertyInfo.PropertyType == typeof(float))
  754. {
  755. EditorGUILayout.FloatField(Name, (float)value);
  756. }
  757. else if (propertyInfo.PropertyType == typeof(bool))
  758. {
  759. EditorGUILayout.Toggle(Name, (bool)value);
  760. }
  761. else if (propertyInfo.PropertyType == typeof(Vector2))
  762. {
  763. EditorGUILayout.Vector2Field(Name, (Vector2)value);
  764. }
  765. else if (propertyInfo.PropertyType == typeof(Vector3))
  766. {
  767. EditorGUILayout.Vector3Field(Name, (Vector3)value);
  768. }
  769. else if (propertyInfo.PropertyType == typeof(Color))
  770. {
  771. EditorGUILayout.ColorField(Name, (Color)value);
  772. }
  773. else if (propertyInfo.PropertyType.IsSubclassOf(typeof(UObject)))
  774. {
  775. EditorGUILayout.ObjectField(Name, value as UObject, propertyInfo.PropertyType, false);
  776. }
  777. else
  778. {
  779. EditorGUILayout.TextField(Name, value != null ? value.ToString() : "null");
  780. }
  781. GUI.enabled = true;
  782. }
  783. }
  784. [CustomMethodDrawer(typeof(ButtonAttribute))]
  785. public class ButtonAttributeDrawer : MethodDrawer
  786. {
  787. BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Default;
  788. public bool IsActive = true;
  789. public string Name;
  790. public override void OnGUI(SerializedObject serializedObject)
  791. {
  792. ButtonAttribute _attribute = attribute as ButtonAttribute;
  793. Name = string.IsNullOrEmpty(_attribute.Name) ? methodInfo.Name : _attribute.Name;
  794. if (!string.IsNullOrEmpty(_attribute.CriteriaName))
  795. {
  796. MemberInfo[] member = serializedObject.targetObject.GetType().GetMember(_attribute.CriteriaName, flags);
  797. if (member != null && member.Length > 0)
  798. {
  799. if (member[0].MemberType == MemberTypes.Field)
  800. IsActive = (bool)(member[0] as FieldInfo).GetValue(serializedObject.targetObject);
  801. else if (member[0].MemberType == MemberTypes.Property)
  802. IsActive = (bool)(member[0] as PropertyInfo).GetValue(serializedObject.targetObject);
  803. else if (member[0].MemberType == MemberTypes.Method)
  804. IsActive = (bool)(member[0] as MethodInfo).Invoke(serializedObject.targetObject, null);
  805. }
  806. GUI.enabled = IsActive == _attribute.CriteriaVaule;
  807. }
  808. if (GUILayout.Button(Name))
  809. {
  810. if (methodInfo.IsStatic)
  811. methodInfo.Invoke(null, _attribute.Parameter);
  812. else
  813. methodInfo.Invoke(serializedObject.targetObject, _attribute.Parameter);
  814. }
  815. GUI.enabled = true;
  816. }
  817. }
  818. }