XRIconEditor.cs 2.1 KB

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