using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.UI; using UnityEditor; using XRTool.Util; namespace XRTool.WorldUI { [InitializeOnLoad] [CustomEditor(typeof(XRImage))] public class XRImageEditor : ImageEditor { private SerializedProperty isNeedBg; private SerializedProperty thickness; private SerializedProperty isNeedFront; private SerializedProperty isAutoScale; private XRImage xRImage; private Vector2 size; public Material mate; [MenuItem("GameObject/XRUI/XRUIModel/XRImage", priority = 0)] static void Init() { var obj = Instantiate(Resources.Load(typeof(XRImage).Name)); obj.name = (typeof(XRImage).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(); xRImage = target as XRImage; xRImage.InitComponent(); isNeedBg = serializedObject.FindProperty("isNeedBg"); thickness = serializedObject.FindProperty("thickness"); isNeedFront = serializedObject.FindProperty("isNeedFront"); isAutoScale = serializedObject.FindProperty("isAutoScale"); size = xRImage.rectTransform.rect.size * WorldDlg.UIScale; if (xRImage.isAutoScale) { xRImage.rectTransform.localScale = Vector3.one / WorldDlg.UIScale; } xRImage.UpdateSize(size/ WorldDlg.UIScale); xRImage.SetBoundThickness(); } public override void OnInspectorGUI() { base.OnInspectorGUI(); EditorGUILayout.PropertyField(isNeedFront, new GUIContent("世界面板")); EditorGUILayout.PropertyField(thickness, new GUIContent("面板厚度")); EditorGUILayout.PropertyField(isNeedBg, new GUIContent("背景板")); EditorGUILayout.PropertyField(isAutoScale, new GUIContent("自动缩放")); mate = (Material)EditorGUILayout.ObjectField("背景材质",mate,typeof(Material)); if (xRImage.rectTransform.rect.size != size) { size=xRImage.rectTransform.rect.size; xRImage.UpdateSize(size); } serializedObject.ApplyModifiedProperties(); if (GUI.changed) { if (xRImage) { xRImage.SetFrontActive(); xRImage.SetBackActive(); xRImage.SetBoundThickness(); if (mate) { xRImage.SetMainMater(mate); } } } } } }