123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- 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<LayoutGroup>();
- 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<GridLayoutGroup>();
- }
- else if (xRScroll.layoutType == LayoutType.HorizontalLayoutGroup)
- {
- layout = xRScroll.content.gameObject.AddComponent<HorizontalLayoutGroup>();
- }
- else if (xRScroll.layoutType == LayoutType.VerticalLayoutGroup)
- {
- layout = xRScroll.content.gameObject.AddComponent<VerticalLayoutGroup>();
- }
- }
- }
- }
- }
- }
- [MenuItem("GameObject/XRUI/XRScrollRect", priority = 0)]
- static void Init()
- {
- var obj = Instantiate(Resources.Load<XRScrollRect>(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;
- }
- }
- }
- }
|