using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.UI; using UnityEditor; using XRTool.Util; namespace XRTool.WorldUI { [InitializeOnLoad] [CustomEditor(typeof(WorldDlg))] public class WorldDlgEditor : Editor { private WorldDlg worldDlg; private Vector2 size; private SerializedProperty isShowTitle; private SerializedProperty titleDis; [MenuItem("GameObject/XRUI/WorldDlg", priority = 0)] static void Init() { var obj = Instantiate(Resources.Load(typeof(WorldDlg).Name)); obj.name = (typeof(WorldDlg).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; } } } } obj.transform.SetParent(parent.transform); Selection.activeGameObject = obj.gameObject; } } private void OnEnable() { isShowTitle = serializedObject.FindProperty("isShowTitle"); titleDis = serializedObject.FindProperty("titleDis"); worldDlg = target as WorldDlg; if (worldDlg) { if(worldDlg.BG) worldDlg.BG.InitComponent(); size = worldDlg.DlgTrans.rect.size; worldDlg.AutoSetScale(worldDlg.DlgTrans, size); worldDlg.BG.UpdateSize(size * WorldDlg.UIScale); worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset); worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle); } } public override void OnInspectorGUI() { base.OnInspectorGUI(); EditorGUILayout.PropertyField(isShowTitle, new GUIContent("是否显示标题")); if (worldDlg.isShowTitle) { EditorGUILayout.PropertyField(titleDis, new GUIContent("标题板距离")); } serializedObject.ApplyModifiedProperties(); if (worldDlg.DlgTrans.rect.size != size) { worldDlg.SetScale(worldDlg.DlgTrans.rect.size); } if (GUI.changed) { worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle); if (worldDlg.isShowTitle) { var pos = worldDlg.BG.Back.anchoredPosition3D; pos.y = worldDlg.titleDis + worldDlg.XRTitle.rectTransform.rect.size.y / 2; worldDlg.BG.Back.anchoredPosition3D = pos; } else { worldDlg.BG.Back.anchoredPosition3D = Vector3.zero; } worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset); worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle); worldDlg.AutoSetTitle(size); } } } }