ToggleButtonEditor.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.UI;
  5. using UnityEditor;
  6. using XRTool.Util;
  7. using UnityEngine.UI;
  8. namespace XRTool.UI
  9. {
  10. [InitializeOnLoad]
  11. [CustomEditor(typeof(ToggleButton))]
  12. public class ToggleButtonEditor : ButtonEditor
  13. {
  14. private SerializedProperty closeImg;
  15. private SerializedProperty openImg;
  16. private SerializedProperty isOn;
  17. private ToggleButton toggle;
  18. [MenuItem("GameObject/XRUI/ToggleButton", priority = 5)]
  19. static void Init()
  20. {
  21. var obj = Selection.activeGameObject;
  22. if (obj && obj.GetComponent<Image>())
  23. {
  24. if (obj.GetComponent<Button>())
  25. {
  26. DestroyImmediate(obj.GetComponent<Button>());
  27. }
  28. if (!obj.GetComponent<ToggleButton>())
  29. {
  30. obj.AddComponent<ToggleButton>();
  31. }
  32. }
  33. }
  34. protected override void OnEnable()
  35. {
  36. base.OnEnable();
  37. closeImg = serializedObject.FindProperty("closeImg");
  38. openImg = serializedObject.FindProperty("openImg");
  39. isOn = serializedObject.FindProperty("isOn");
  40. toggle = target as ToggleButton;
  41. }
  42. public override void OnInspectorGUI()
  43. {
  44. base.OnInspectorGUI();
  45. EditorGUILayout.PropertyField(closeImg, new GUIContent("关闭图"));
  46. EditorGUILayout.PropertyField(openImg, new GUIContent("打开图"));
  47. EditorGUILayout.PropertyField(isOn, new GUIContent("isOn"));
  48. serializedObject.ApplyModifiedProperties();
  49. if (GUI.changed)
  50. {
  51. if (toggle)
  52. {
  53. toggle.image.sprite = toggle.isOn ? toggle.openImg : toggle.closeImg;
  54. }
  55. }
  56. }
  57. }
  58. }