LoopGridViewEditor.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. namespace SuperScrollView
  8. {
  9. [CustomEditor(typeof(LoopGridView))]
  10. public class LoopGridViewEditor : Editor
  11. {
  12. SerializedProperty mGridFixedType;
  13. SerializedProperty mGridFixedRowOrColumn;
  14. SerializedProperty mKeepFillShow;
  15. SerializedProperty mSortStartIdx;
  16. SerializedProperty mItemSnapEnable;
  17. SerializedProperty mArrangeType;
  18. SerializedProperty mItemPrefabDataList;
  19. SerializedProperty mItemSnapPivot;
  20. SerializedProperty mViewPortSnapPivot;
  21. SerializedProperty mPadding;
  22. SerializedProperty mItemSize;
  23. SerializedProperty mItemPadding;
  24. SerializedProperty mItemRecycleDistance;
  25. GUIContent mItemSnapEnableContent = new GUIContent("ItemSnapEnable");
  26. GUIContent mArrangeTypeGuiContent = new GUIContent("ArrangeType");
  27. GUIContent mItemPrefabListContent = new GUIContent("ItemPrefabList");
  28. GUIContent mItemSnapPivotContent = new GUIContent("ItemSnapPivot");
  29. GUIContent mViewPortSnapPivotContent = new GUIContent("ViewPortSnapPivot");
  30. GUIContent mGridFixedTypeContent = new GUIContent("GridFixedType");
  31. GUIContent mPaddingContent = new GUIContent("Padding");
  32. GUIContent mItemSizeContent = new GUIContent("ItemSize");
  33. GUIContent mItemPaddingContent = new GUIContent("ItemPadding");
  34. GUIContent mGridFixedRowContent = new GUIContent("RowCount");
  35. GUIContent mKeepFillShowContent = new GUIContent("KeepFillShow", "保持Item的数量至少完全覆盖显示区域,当数量大于显示区域时,会按照ColumnCount或RowCount来填充成完整列表");
  36. GUIContent mSortStartIdxContent = new GUIContent("SortStartIdx");
  37. GUIContent mGridFixedColumnContent = new GUIContent("ColumnCount");
  38. GUIContent mItemRecycleDistanceContent = new GUIContent("RecycleDistance");
  39. protected virtual void OnEnable()
  40. {
  41. mGridFixedType = serializedObject.FindProperty("mGridFixedType");
  42. mItemSnapEnable = serializedObject.FindProperty("mItemSnapEnable");
  43. mArrangeType = serializedObject.FindProperty("mArrangeType");
  44. mItemPrefabDataList = serializedObject.FindProperty("mItemPrefabDataList");
  45. mItemSnapPivot = serializedObject.FindProperty("mItemSnapPivot");
  46. mViewPortSnapPivot = serializedObject.FindProperty("mViewPortSnapPivot");
  47. mGridFixedRowOrColumn = serializedObject.FindProperty("mFixedRowOrColumnCount");
  48. mKeepFillShow = serializedObject.FindProperty("mKeepFillShow");
  49. mSortStartIdx = serializedObject.FindProperty("mSortStartIdx");
  50. mItemPadding = serializedObject.FindProperty("mItemPadding");
  51. mPadding = serializedObject.FindProperty("mPadding");
  52. mItemSize = serializedObject.FindProperty("mItemSize");
  53. mItemRecycleDistance = serializedObject.FindProperty("mItemRecycleDistance");
  54. }
  55. void ShowItemPrefabDataList(LoopGridView listView)
  56. {
  57. EditorGUILayout.PropertyField(mItemPrefabDataList, mItemPrefabListContent);
  58. if (mItemPrefabDataList.isExpanded == false)
  59. {
  60. return;
  61. }
  62. EditorGUI.indentLevel += 1;
  63. if (GUILayout.Button("Add New"))
  64. {
  65. mItemPrefabDataList.InsertArrayElementAtIndex(mItemPrefabDataList.arraySize);
  66. if (mItemPrefabDataList.arraySize > 0)
  67. {
  68. SerializedProperty itemData = mItemPrefabDataList.GetArrayElementAtIndex(mItemPrefabDataList.arraySize - 1);
  69. SerializedProperty mItemPrefab = itemData.FindPropertyRelative("mItemPrefab");
  70. mItemPrefab.objectReferenceValue = null;
  71. }
  72. }
  73. int removeIndex = -1;
  74. EditorGUILayout.PropertyField(mItemPrefabDataList.FindPropertyRelative("Array.size"));
  75. for (int i = 0; i < mItemPrefabDataList.arraySize; i++)
  76. {
  77. SerializedProperty itemData = mItemPrefabDataList.GetArrayElementAtIndex(i);
  78. SerializedProperty mInitCreateCount = itemData.FindPropertyRelative("mInitCreateCount");
  79. SerializedProperty mItemPrefab = itemData.FindPropertyRelative("mItemPrefab");
  80. EditorGUILayout.BeginHorizontal();
  81. EditorGUILayout.PropertyField(itemData);
  82. if (GUILayout.Button("Remove"))
  83. {
  84. removeIndex = i;
  85. }
  86. EditorGUILayout.EndHorizontal();
  87. if (itemData.isExpanded == false)
  88. {
  89. continue;
  90. }
  91. mItemPrefab.objectReferenceValue = EditorGUILayout.ObjectField("ItemPrefab", mItemPrefab.objectReferenceValue, typeof(GameObject), true);
  92. mInitCreateCount.intValue = EditorGUILayout.IntField("InitCreateCount", mInitCreateCount.intValue);
  93. EditorGUILayout.Space();
  94. EditorGUILayout.Space();
  95. }
  96. if (removeIndex >= 0)
  97. {
  98. mItemPrefabDataList.DeleteArrayElementAtIndex(removeIndex);
  99. }
  100. EditorGUI.indentLevel -= 1;
  101. }
  102. public override void OnInspectorGUI()
  103. {
  104. serializedObject.Update();
  105. LoopGridView tListView = serializedObject.targetObject as LoopGridView;
  106. if (tListView == null)
  107. {
  108. return;
  109. }
  110. ShowItemPrefabDataList(tListView);
  111. EditorGUILayout.Space();
  112. EditorGUILayout.PropertyField(mGridFixedType, mGridFixedTypeContent);
  113. if (mGridFixedType.enumValueIndex == (int)GridFixedType.ColumnCountFixed)
  114. {
  115. EditorGUILayout.PropertyField(mGridFixedRowOrColumn, mGridFixedColumnContent);
  116. EditorGUILayout.HelpBox("当 ColumnCount <= 0 时, 会根据显示区的大小自动计算ColumnCount的适宜数量", MessageType.Info);
  117. }
  118. else
  119. {
  120. EditorGUILayout.PropertyField(mGridFixedRowOrColumn, mGridFixedRowContent);
  121. EditorGUILayout.HelpBox("当 RowCount <= 0 时, 会根据显示区的大小自动计算RowCount的适宜数量", MessageType.Info);
  122. }
  123. EditorGUILayout.Space();
  124. DrawToggleIntProperty(mKeepFillShow, mKeepFillShowContent);
  125. DrawToggleIntProperty(mSortStartIdx, mSortStartIdxContent);
  126. EditorGUILayout.PropertyField(mPadding, mPaddingContent, true);
  127. EditorGUILayout.PropertyField(mItemSize, mItemSizeContent);
  128. EditorGUILayout.PropertyField(mItemPadding, mItemPaddingContent);
  129. EditorGUILayout.PropertyField(mItemRecycleDistance, mItemRecycleDistanceContent);
  130. EditorGUILayout.Space();
  131. EditorGUILayout.PropertyField(mItemSnapEnable, mItemSnapEnableContent);
  132. if (mItemSnapEnable.boolValue == true)
  133. {
  134. EditorGUILayout.PropertyField(mItemSnapPivot, mItemSnapPivotContent);
  135. EditorGUILayout.PropertyField(mViewPortSnapPivot, mViewPortSnapPivotContent);
  136. }
  137. EditorGUILayout.Space();
  138. EditorGUILayout.PropertyField(mArrangeType, mArrangeTypeGuiContent);
  139. serializedObject.ApplyModifiedProperties();
  140. }
  141. private void DrawToggleIntProperty(SerializedProperty serializedProperty, GUIContent gUIContent)
  142. {
  143. Rect controlRect = EditorGUILayout.GetControlRect();
  144. GUIContent label = EditorGUI.BeginProperty(controlRect, gUIContent, serializedProperty);
  145. Rect val = EditorGUI.PrefixLabel(controlRect, label);
  146. Rect position = val;
  147. position.width = 16f;
  148. Rect position2 = val;
  149. position2.xMin = (position2.xMin + 16f);
  150. EditorGUI.BeginChangeCheck();
  151. bool flag = EditorGUI.ToggleLeft(position, GUIContent.none, serializedProperty.intValue >= 0f);
  152. if (EditorGUI.EndChangeCheck())
  153. {
  154. serializedProperty.intValue = ((!flag) ? -1 : 0);
  155. }
  156. if (!serializedProperty.hasMultipleDifferentValues && serializedProperty.intValue >= 0f)
  157. {
  158. EditorGUIUtility.labelWidth = 4f;
  159. EditorGUI.BeginChangeCheck();
  160. int num = EditorGUI.IntField(position2, new GUIContent(" "), serializedProperty.intValue);
  161. if (EditorGUI.EndChangeCheck())
  162. {
  163. serializedProperty.intValue = Mathf.Max(0, num);
  164. }
  165. EditorGUIUtility.labelWidth = 0f;
  166. }
  167. EditorGUI.EndProperty();
  168. }
  169. }
  170. }