LoopListViewEditor.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(LoopListView))]
  10. public class LoopListViewEditor : Editor
  11. {
  12. SerializedProperty mStartPadding;
  13. SerializedProperty mEndPadding;
  14. SerializedProperty mSupportScrollBar;
  15. SerializedProperty mItemSnapEnable;
  16. SerializedProperty mArrangeType;
  17. SerializedProperty mItemPrefabDataList;
  18. SerializedProperty mItemSnapPivot;
  19. SerializedProperty mViewPortSnapPivot;
  20. SerializedProperty mAutoAdapterSize;
  21. GUIContent mStartPaddingContent = new GUIContent("StartPadding");
  22. GUIContent mEndPaddingContent = new GUIContent("EndPadding");
  23. GUIContent mSupportScrollBarContent = new GUIContent("SupportScrollBar");
  24. GUIContent mItemSnapEnableContent = new GUIContent("ItemSnapEnable");
  25. GUIContent mArrangeTypeGuiContent = new GUIContent("ArrangeType");
  26. GUIContent mItemPrefabListContent = new GUIContent("ItemPrefabList");
  27. GUIContent mItemSnapPivotContent = new GUIContent("ItemSnapPivot");
  28. GUIContent mViewPortSnapPivotContent = new GUIContent("ViewPortSnapPivot");
  29. GUIContent mAutoAdapterSizeContent = new GUIContent("AutoAdapterSize", "如果LoopListView是大小是可变的,并且需要影响到LoopListViewItem,则可勾选这个");
  30. protected virtual void OnEnable()
  31. {
  32. mStartPadding = serializedObject.FindProperty("mStartPadding");
  33. mEndPadding = serializedObject.FindProperty("mEndPadding");
  34. mSupportScrollBar = serializedObject.FindProperty("mSupportScrollBar");
  35. mItemSnapEnable = serializedObject.FindProperty("mItemSnapEnable");
  36. mArrangeType = serializedObject.FindProperty("mArrangeType");
  37. mItemPrefabDataList = serializedObject.FindProperty("mItemPrefabDataList");
  38. mItemSnapPivot = serializedObject.FindProperty("mItemSnapPivot");
  39. mViewPortSnapPivot = serializedObject.FindProperty("mViewPortSnapPivot");
  40. mAutoAdapterSize = serializedObject.FindProperty("mAutoAdapterSize");
  41. }
  42. void ShowItemPrefabDataList(LoopListView listView)
  43. {
  44. EditorGUILayout.PropertyField(mItemPrefabDataList, mItemPrefabListContent);
  45. if (mItemPrefabDataList.isExpanded == false)
  46. {
  47. return;
  48. }
  49. EditorGUI.indentLevel += 1;
  50. if (GUILayout.Button("Add New"))
  51. {
  52. mItemPrefabDataList.InsertArrayElementAtIndex(mItemPrefabDataList.arraySize);
  53. if (mItemPrefabDataList.arraySize > 0)
  54. {
  55. SerializedProperty itemData = mItemPrefabDataList.GetArrayElementAtIndex(mItemPrefabDataList.arraySize - 1);
  56. SerializedProperty mItemPrefab = itemData.FindPropertyRelative("mItemPrefab");
  57. mItemPrefab.objectReferenceValue = null;
  58. }
  59. }
  60. int removeIndex = -1;
  61. EditorGUILayout.PropertyField(mItemPrefabDataList.FindPropertyRelative("Array.size"));
  62. for (int i = 0; i < mItemPrefabDataList.arraySize; i++)
  63. {
  64. SerializedProperty itemData = mItemPrefabDataList.GetArrayElementAtIndex(i);
  65. SerializedProperty mInitCreateCount = itemData.FindPropertyRelative("mInitCreateCount");
  66. SerializedProperty mItemPrefab = itemData.FindPropertyRelative("mItemPrefab");
  67. SerializedProperty mItemPrefabPadding = itemData.FindPropertyRelative("mPadding");
  68. SerializedProperty mItemStartPosOffset = itemData.FindPropertyRelative("mStartPosOffset");
  69. EditorGUILayout.BeginHorizontal();
  70. EditorGUILayout.PropertyField(itemData);
  71. if (GUILayout.Button("Remove"))
  72. {
  73. removeIndex = i;
  74. }
  75. EditorGUILayout.EndHorizontal();
  76. if (itemData.isExpanded == false)
  77. {
  78. continue;
  79. }
  80. mItemPrefab.objectReferenceValue = EditorGUILayout.ObjectField("ItemPrefab", mItemPrefab.objectReferenceValue, typeof(GameObject), true);
  81. mItemPrefabPadding.floatValue = EditorGUILayout.FloatField("ItemPadding", mItemPrefabPadding.floatValue);
  82. if (listView.ArrangeType == ListItemArrangeType.TopToBottom || listView.ArrangeType == ListItemArrangeType.BottomToTop)
  83. {
  84. mItemStartPosOffset.floatValue = EditorGUILayout.FloatField("XPosOffset", mItemStartPosOffset.floatValue);
  85. }
  86. else
  87. {
  88. mItemStartPosOffset.floatValue = EditorGUILayout.FloatField("YPosOffset", mItemStartPosOffset.floatValue);
  89. }
  90. mInitCreateCount.intValue = EditorGUILayout.IntField("InitCreateCount", mInitCreateCount.intValue);
  91. EditorGUILayout.Space();
  92. EditorGUILayout.Space();
  93. }
  94. if (removeIndex >= 0)
  95. {
  96. mItemPrefabDataList.DeleteArrayElementAtIndex(removeIndex);
  97. }
  98. EditorGUI.indentLevel -= 1;
  99. }
  100. public override void OnInspectorGUI()
  101. {
  102. serializedObject.Update();
  103. LoopListView tListView = serializedObject.targetObject as LoopListView;
  104. if (tListView == null)
  105. {
  106. return;
  107. }
  108. ShowItemPrefabDataList(tListView);
  109. EditorGUILayout.Space();
  110. EditorGUILayout.PropertyField(mStartPadding, mStartPaddingContent);
  111. EditorGUILayout.PropertyField(mEndPadding, mEndPaddingContent);
  112. EditorGUILayout.PropertyField(mSupportScrollBar, mSupportScrollBarContent);
  113. EditorGUILayout.PropertyField(mItemSnapEnable, mItemSnapEnableContent);
  114. EditorGUILayout.PropertyField(mAutoAdapterSize, mAutoAdapterSizeContent);
  115. if (mItemSnapEnable.boolValue == true)
  116. {
  117. EditorGUILayout.PropertyField(mItemSnapPivot, mItemSnapPivotContent);
  118. EditorGUILayout.PropertyField(mViewPortSnapPivot, mViewPortSnapPivotContent);
  119. }
  120. EditorGUILayout.PropertyField(mArrangeType, mArrangeTypeGuiContent);
  121. serializedObject.ApplyModifiedProperties();
  122. }
  123. }
  124. }