12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor.UI;
- using UnityEditor;
- using XRTool.Util;
- using UnityEngine.UI;
- namespace XRTool.UI
- {
- [InitializeOnLoad]
- [CustomEditor(typeof(ToggleButton))]
- public class ToggleButtonEditor : ButtonEditor
- {
- private SerializedProperty closeImg;
- private SerializedProperty openImg;
- private SerializedProperty isOn;
- private ToggleButton toggle;
- [MenuItem("GameObject/XRUI/ToggleButton", priority = 5)]
- static void Init()
- {
- var obj = Selection.activeGameObject;
- if (obj && obj.GetComponent<Image>())
- {
- if (obj.GetComponent<Button>())
- {
- DestroyImmediate(obj.GetComponent<Button>());
- }
- if (!obj.GetComponent<ToggleButton>())
- {
- obj.AddComponent<ToggleButton>();
- }
- }
- }
- protected override void OnEnable()
- {
- base.OnEnable();
- closeImg = serializedObject.FindProperty("closeImg");
- openImg = serializedObject.FindProperty("openImg");
- isOn = serializedObject.FindProperty("isOn");
- toggle = target as ToggleButton;
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- EditorGUILayout.PropertyField(closeImg, new GUIContent("关闭图"));
- EditorGUILayout.PropertyField(openImg, new GUIContent("打开图"));
- EditorGUILayout.PropertyField(isOn, new GUIContent("isOn"));
- serializedObject.ApplyModifiedProperties();
- if (GUI.changed)
- {
- if (toggle)
- {
- toggle.image.sprite = toggle.isOn ? toggle.openImg : toggle.closeImg;
- }
- }
- }
- }
- }
|