XRIconEditor.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEditor;
  2. using UnityEditor.UI;
  3. using UnityEngine;
  4. using XRTool.Util;
  5. namespace XRTool.WorldUI
  6. {
  7. [InitializeOnLoad]
  8. [CustomEditor(typeof(XRIcon))]
  9. public class XRIconEditor : ImageEditor
  10. {
  11. private XRIcon xrIcon;
  12. private SerializedProperty icon;
  13. [MenuItem("GameObject/XRUI/XRIcon", priority = 10)]
  14. static void Init()
  15. {
  16. var obj = Instantiate(Resources.Load<XRIcon>(typeof(XRIcon).Name));
  17. obj.name = (typeof(XRIcon).Name);
  18. if (obj)
  19. {
  20. var parent = Selection.activeGameObject;
  21. if (!parent)
  22. {
  23. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  24. if (canvas != null)
  25. {
  26. for (int i = 0; i < canvas.Length; i++)
  27. {
  28. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  29. {
  30. parent = (canvas[i] as Canvas).gameObject;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  37. Selection.activeGameObject = obj.gameObject;
  38. }
  39. }
  40. protected override void OnEnable()
  41. {
  42. base.OnEnable();
  43. icon = serializedObject.FindProperty("icon");
  44. xrIcon = target as XRIcon;
  45. xrIcon.AutoSetSprite();
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. public override void OnInspectorGUI()
  51. {
  52. base.OnInspectorGUI();
  53. EditorGUILayout.PropertyField(icon, new GUIContent("图标"));
  54. serializedObject.ApplyModifiedProperties();
  55. if (GUI.changed)
  56. {
  57. if (xrIcon)
  58. {
  59. xrIcon.AutoSetSprite();
  60. }
  61. }
  62. }
  63. }
  64. }