using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEditor.UI; using UnityEngine; using UnityEngine.UI; using XRTool.Util; namespace XRTool.WorldUI { [InitializeOnLoad] [CustomEditor(typeof(XRScrollRect))] public class XRScrollRectEditor : ScrollRectEditor { private SerializedProperty layoutType; private SerializedProperty isLoop; private XRScrollRect xRScroll; protected override void OnEnable() { base.OnEnable(); layoutType = serializedObject.FindProperty("layoutType"); isLoop = serializedObject.FindProperty("isLoop"); xRScroll = target as XRScrollRect; } public override void OnInspectorGUI() { base.OnInspectorGUI(); EditorGUILayout.PropertyField(layoutType); EditorGUILayout.PropertyField(isLoop); serializedObject.ApplyModifiedProperties(); if (GUI.changed) { ///当指定布局管理器时 if (xRScroll.layoutType != LayoutType.UnKnow) { if (xRScroll.content) { var layout = xRScroll.content.GetComponent(); if (layout) { bool isDel = false; if (xRScroll.layoutType == LayoutType.None) { isDel = true; } if (xRScroll.layoutType == LayoutType.GridLayoutGroup) { if (!(layout is GridLayoutGroup)) { isDel = true; } } else if (xRScroll.layoutType == LayoutType.HorizontalLayoutGroup) { if (!(layout is HorizontalLayoutGroup)) { isDel = true; } } else if (xRScroll.layoutType == LayoutType.VerticalLayoutGroup) { if (!(layout is VerticalLayoutGroup)) { isDel = true; } } if (isDel) { DestroyImmediate(layout); layout = null; } } if (!layout) { if (xRScroll.layoutType == LayoutType.GridLayoutGroup) { layout = xRScroll.content.gameObject.AddComponent(); } else if (xRScroll.layoutType == LayoutType.HorizontalLayoutGroup) { layout = xRScroll.content.gameObject.AddComponent(); } else if (xRScroll.layoutType == LayoutType.VerticalLayoutGroup) { layout = xRScroll.content.gameObject.AddComponent(); } } } } } } [MenuItem("GameObject/XRUI/XRScrollRect", priority = 0)] static void Init() { var obj = Instantiate(Resources.Load(typeof(XRScrollRect).Name)); obj.name = (typeof(XRScrollRect).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; } } } }