using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.UI; using UnityEditor; using XRTool.Util; namespace XRTool.WorldUI { [InitializeOnLoad] [CustomEditor(typeof(XRButton))] public class XRButtonEditor : ButtonEditor { private SerializedProperty thickness; private SerializedProperty pressDis; private SerializedProperty isShowBack; private SerializedProperty isShowBox; private SerializedProperty pressTime; private SerializedProperty isTweenOnClick; private Vector2 size; private XRButton button; [MenuItem("GameObject/XRUI/XRButton", priority = 0)] static void Init() { var obj = Instantiate(Resources.Load(typeof(XRButton).Name)); obj.name = (typeof(XRButton).Name); if (obj) { var parent = Selection.activeGameObject; if (!parent) { var canvas = GameObject.FindObjectsOfType(typeof(Canvas)); if (canvas != null) { for (int i = 0; i < canvas.Length; i++) { if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace) { parent = (canvas[i] as Canvas).gameObject; break; } } } } UnityUtil.SetParent(parent ? parent.transform : null, obj.transform); Selection.activeGameObject = obj.gameObject; } } protected override void OnEnable() { base.OnEnable(); thickness = serializedObject.FindProperty("thickness"); pressDis = serializedObject.FindProperty("pressDis"); isShowBack = serializedObject.FindProperty("isShowBack"); isShowBox = serializedObject.FindProperty("isShowBox"); pressTime = serializedObject.FindProperty("pressTime"); isTweenOnClick = serializedObject.FindProperty("isTweenOnClick"); button = target as XRButton; if (button) { size = button.Body.rect.size; } } public override void OnInspectorGUI() { base.OnInspectorGUI(); EditorGUILayout.PropertyField(thickness, new GUIContent("按钮厚度")); EditorGUILayout.PropertyField(pressDis, new GUIContent("压缩比例")); EditorGUILayout.PropertyField(pressTime, new GUIContent("压缩时间比例")); EditorGUILayout.PropertyField(isShowBack, new GUIContent("显示背景")); EditorGUILayout.PropertyField(isShowBox, new GUIContent("显示Box")); EditorGUILayout.PropertyField(isTweenOnClick, new GUIContent("是否移动")); if (button.Body.rect.size != size) { size = button.Body.rect.size; button.AutoSetSize(); //AutoSetScale(button.transform,size); } serializedObject.ApplyModifiedProperties(); if (GUI.changed) { button.Back.gameObject.SetActive(button.isShowBack); button.Box.gameObject.SetActive(button.isShowBox); button.UpdateThickness(); } } } }