XRToggleEditor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using UnityEditor.UI;
  3. using UnityEditor;
  4. using XRTool.Util;
  5. namespace XRTool.WorldUI
  6. {
  7. [InitializeOnLoad]
  8. [CustomEditor(typeof(XRToggle))]
  9. public class XRToggleEditor : ToggleEditor
  10. {
  11. private XRToggle toggle;
  12. private SerializedProperty openPos;
  13. private SerializedProperty moveTime;
  14. private bool isCheckBox = false;
  15. private bool isSwitch;
  16. [MenuItem("GameObject/XRUI/XRToggle/XRToggle_CheckBox", priority = 0)]
  17. static void Init()
  18. {
  19. UnityLog.Instance.Log(typeof(XRToggle).Name + "_CheckBox");
  20. var obj = Instantiate(Resources.Load<XRToggle>(typeof(XRToggle).Name + "_CheckBox"));
  21. obj.name = (typeof(XRToggle).Name + "_CheckBox");
  22. if (obj)
  23. {
  24. var parent = Selection.activeGameObject;
  25. if (!parent)
  26. {
  27. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  28. if (canvas != null)
  29. {
  30. for (int i = 0; i < canvas.Length; i++)
  31. {
  32. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  33. {
  34. parent = (canvas[i] as Canvas).gameObject;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  41. Selection.activeGameObject = obj.gameObject;
  42. }
  43. }
  44. [MenuItem("GameObject/XRUI/XRToggle/XRToggle_Switch", priority = 0)]
  45. static void Init_Switch()
  46. {
  47. var obj = Instantiate(Resources.Load<XRToggle>(typeof(XRToggle).Name + "_Switch"));
  48. obj.name = (typeof(XRToggle).Name + "_Switch");
  49. if (obj)
  50. {
  51. var parent = Selection.activeGameObject;
  52. if (!parent)
  53. {
  54. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  55. if (canvas != null)
  56. {
  57. for (int i = 0; i < canvas.Length; i++)
  58. {
  59. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  60. {
  61. parent = (canvas[i] as Canvas).gameObject;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  68. Selection.activeGameObject = obj.gameObject;
  69. }
  70. }
  71. protected override void OnEnable()
  72. {
  73. base.OnEnable();
  74. toggle = target as XRToggle;
  75. toggle.initListen();
  76. openPos = serializedObject.FindProperty("openPos");
  77. moveTime = serializedObject.FindProperty("moveTime");
  78. isCheckBox = false;
  79. isSwitch = false ;
  80. if (toggle.name.Contains("CheckBox"))
  81. {
  82. isCheckBox = true;
  83. }
  84. if(toggle.name.Contains("Switch"))
  85. {
  86. isSwitch = true;
  87. }
  88. toggle.isHideCheck = isCheckBox;
  89. toggle.isSwitch = isSwitch;
  90. }
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. public override void OnInspectorGUI()
  95. {
  96. base.OnInspectorGUI();
  97. if (isSwitch)
  98. {
  99. EditorGUILayout.PropertyField(openPos, new GUIContent("位移"));
  100. EditorGUILayout.PropertyField(moveTime, new GUIContent("时间"));
  101. serializedObject.ApplyModifiedProperties();
  102. if (GUI.changed)
  103. {
  104. }
  105. }
  106. }
  107. }
  108. }