XRIcon3DEditor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using UnityEditor.UI;
  3. using UnityEditor;
  4. using XRTool.Util;
  5. namespace XRTool.WorldUI
  6. {
  7. [InitializeOnLoad]
  8. [CustomEditor(typeof(XRIcon3D))]
  9. public class XRIcon3DEditor : XRImage3DEditor
  10. {
  11. private XRIcon3D icon3D;
  12. private SerializedProperty img;
  13. [MenuItem("GameObject/XRUI/XRUIModel/XRIcon3D", priority = 0)]
  14. static void Init()
  15. {
  16. var obj = Instantiate(Resources.Load<XRIcon3D>(typeof(XRIcon3D).Name));
  17. obj.name = (typeof(XRIcon3D).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. [MenuItem("GameObject/XRUI/XRUIModel/XRItem", priority = 0)]
  41. static void InitItem()
  42. {
  43. var obj = Instantiate(Resources.Load<GameObject>("XRItem"));
  44. obj.name = "XRItem";
  45. if (obj)
  46. {
  47. var parent = Selection.activeGameObject;
  48. if (!parent)
  49. {
  50. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  51. if (canvas != null)
  52. {
  53. for (int i = 0; i < canvas.Length; i++)
  54. {
  55. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  56. {
  57. parent = (canvas[i] as Canvas).gameObject;
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  64. Selection.activeGameObject = obj.gameObject;
  65. }
  66. }
  67. protected override void OnEnable()
  68. {
  69. base.OnEnable();
  70. img = serializedObject.FindProperty("img");
  71. icon3D = target as XRIcon3D;
  72. icon3D.AutoSetSize();
  73. }
  74. public override void OnInspectorGUI()
  75. {
  76. base.OnInspectorGUI();
  77. EditorGUILayout.PropertyField(img, new GUIContent("Icon"));
  78. serializedObject.ApplyModifiedProperties();
  79. if (GUI.changed)
  80. {
  81. if (icon3D)
  82. {
  83. icon3D.SetIcon();
  84. }
  85. }
  86. }
  87. }
  88. }