using UnityEditor; using UnityEditor.UI; using UnityEngine; using XRTool.Util; namespace XRTool.WorldUI { [InitializeOnLoad] [CustomEditor(typeof(XRIcon))] public class XRIconEditor : ImageEditor { private XRIcon xrIcon; private SerializedProperty icon; [MenuItem("GameObject/XRUI/XRIcon", priority = 10)] static void Init() { var obj = Instantiate(Resources.Load(typeof(XRIcon).Name)); obj.name = (typeof(XRIcon).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(); icon = serializedObject.FindProperty("icon"); xrIcon = target as XRIcon; xrIcon.AutoSetSprite(); } /// /// /// public override void OnInspectorGUI() { base.OnInspectorGUI(); EditorGUILayout.PropertyField(icon, new GUIContent("图标")); serializedObject.ApplyModifiedProperties(); if (GUI.changed) { if (xrIcon) { xrIcon.AutoSetSprite(); } } } } }