XRScrollRectEditor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditor.UI;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using XRTool.Util;
  8. namespace XRTool.WorldUI
  9. {
  10. [InitializeOnLoad]
  11. [CustomEditor(typeof(XRScrollRect))]
  12. public class XRScrollRectEditor : ScrollRectEditor
  13. {
  14. private SerializedProperty layoutType;
  15. private SerializedProperty isLoop;
  16. private XRScrollRect xRScroll;
  17. protected override void OnEnable()
  18. {
  19. base.OnEnable();
  20. layoutType = serializedObject.FindProperty("layoutType");
  21. isLoop = serializedObject.FindProperty("isLoop");
  22. xRScroll = target as XRScrollRect;
  23. }
  24. public override void OnInspectorGUI()
  25. {
  26. base.OnInspectorGUI();
  27. EditorGUILayout.PropertyField(layoutType);
  28. EditorGUILayout.PropertyField(isLoop);
  29. serializedObject.ApplyModifiedProperties();
  30. if (GUI.changed)
  31. {
  32. ///当指定布局管理器时
  33. if (xRScroll.layoutType != LayoutType.UnKnow)
  34. {
  35. if (xRScroll.content)
  36. {
  37. var layout = xRScroll.content.GetComponent<LayoutGroup>();
  38. if (layout)
  39. {
  40. bool isDel = false;
  41. if (xRScroll.layoutType == LayoutType.None)
  42. {
  43. isDel = true;
  44. }
  45. if (xRScroll.layoutType == LayoutType.GridLayoutGroup)
  46. {
  47. if (!(layout is GridLayoutGroup))
  48. {
  49. isDel = true;
  50. }
  51. }
  52. else if (xRScroll.layoutType == LayoutType.HorizontalLayoutGroup)
  53. {
  54. if (!(layout is HorizontalLayoutGroup))
  55. {
  56. isDel = true;
  57. }
  58. }
  59. else if (xRScroll.layoutType == LayoutType.VerticalLayoutGroup)
  60. {
  61. if (!(layout is VerticalLayoutGroup))
  62. {
  63. isDel = true;
  64. }
  65. }
  66. if (isDel)
  67. {
  68. DestroyImmediate(layout);
  69. layout = null;
  70. }
  71. }
  72. if (!layout)
  73. {
  74. if (xRScroll.layoutType == LayoutType.GridLayoutGroup)
  75. {
  76. layout = xRScroll.content.gameObject.AddComponent<GridLayoutGroup>();
  77. }
  78. else if (xRScroll.layoutType == LayoutType.HorizontalLayoutGroup)
  79. {
  80. layout = xRScroll.content.gameObject.AddComponent<HorizontalLayoutGroup>();
  81. }
  82. else if (xRScroll.layoutType == LayoutType.VerticalLayoutGroup)
  83. {
  84. layout = xRScroll.content.gameObject.AddComponent<VerticalLayoutGroup>();
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. [MenuItem("GameObject/XRUI/XRScrollRect", priority = 0)]
  92. static void Init()
  93. {
  94. var obj = Instantiate(Resources.Load<XRScrollRect>(typeof(XRScrollRect).Name));
  95. obj.name = (typeof(XRScrollRect).Name);
  96. if (obj)
  97. {
  98. var parent = Selection.activeGameObject;
  99. if (!parent)
  100. {
  101. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  102. if (canvas != null)
  103. {
  104. for (int i = 0; i < canvas.Length; i++)
  105. {
  106. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  107. {
  108. parent = (canvas[i] as Canvas).gameObject;
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  115. Selection.activeGameObject = obj.gameObject;
  116. }
  117. }
  118. }
  119. }