123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 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<XRIcon>(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();
- }
- /// <summary>
- ///
- /// </summary>
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- EditorGUILayout.PropertyField(icon, new GUIContent("图标"));
- serializedObject.ApplyModifiedProperties();
- if (GUI.changed)
- {
- if (xrIcon)
- {
- xrIcon.AutoSetSprite();
- }
- }
- }
- }
- }
|