代码

  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. namespace GameContent
  6. {
  7. public class TestReorderlist : EditorWindow
  8. {
  9. [MenuItem("GM/TestReorderlist")]
  10. public static void ShowWindow()
  11. {
  12. GetWindow<TestReorderlist>("TestReorderlist");
  13. }
  14. //所有数据列表
  15. private List<TestElementData> mTotalDataList = new List<TestElementData>() { };
  16. //显示n个数据
  17. private TestElementData[] mPartDataArray = new TestElementData[5];
  18. //显示的列表组件
  19. private ReorderableList mReorderableList;
  20. private void OnEnable()
  21. {
  22. InitTestData();
  23. mReorderableList = new ReorderableList(mPartDataArray , typeof(TestElementData));
  24. mReorderableList.drawHeaderCallback = ReorderableList_DrawHeaderCallback;
  25. mReorderableList.drawElementCallback = ReorderableList_DrawElementCallback;
  26. mReorderableList.onAddCallback = ReorderableList_OnAddCallback;
  27. mReorderableList.onRemoveCallback = ReorderableList_OnRemoveCallback;
  28. mReorderableList.onReorderCallbackWithDetails = ReorderableList_OnReorderCallbackWithDetails;
  29. }
  30. private void ReorderableList_DrawHeaderCallback(Rect rect)
  31. {
  32. GUI.Label(rect , "Header");
  33. }
  34. private void ReorderableList_DrawElementCallback(Rect rect , int index , bool isActive , bool isFocused)
  35. {
  36. int dataIndex = (int)mSliderValue + index;
  37. if(dataIndex >= mTotalDataList.Count || dataIndex < 0)
  38. {
  39. GUI.Label(rect , $"index={index} ; mSliderValue={mSliderValue} ; dataIndex={dataIndex} ; 索引超出范围");
  40. }
  41. else
  42. {
  43. TestElementData elementData = mTotalDataList[dataIndex];
  44. GUI.Label(rect , $"index={index} ; mSliderValue={mSliderValue} ; dataIndex={dataIndex} ; name={elementData.Name}");
  45. }
  46. }
  47. private void ReorderableList_OnAddCallback(ReorderableList list)
  48. {
  49. int dataIndex = (int)mSliderValue + mReorderableList.index;
  50. if(dataIndex + 1 >= mTotalDataList.Count)
  51. {
  52. mTotalDataList.Add(new TestElementData()
  53. {
  54. Name = "AddName" + dataIndex ,
  55. Value = "AddValue" + dataIndex
  56. });
  57. }
  58. else
  59. {
  60. mTotalDataList.Insert(dataIndex + 1 , new TestElementData()
  61. {
  62. Name = "InsertName" + dataIndex ,
  63. Value = "InsertValue" + dataIndex
  64. });
  65. }
  66. }
  67. private void ReorderableList_OnRemoveCallback(ReorderableList list)
  68. {
  69. int dataIndex = (int)mSliderValue + mReorderableList.index;
  70. if(dataIndex >= 0 && dataIndex < mTotalDataList.Count)
  71. {
  72. mTotalDataList.RemoveAt(dataIndex);
  73. }
  74. }
  75. private void ReorderableList_OnReorderCallbackWithDetails(ReorderableList list , int oldIndex , int newIndex)
  76. {
  77. int oldDataIndex = (int)mSliderValue + oldIndex;
  78. int newDataIndex = (int)mSliderValue + newIndex;
  79. if(oldDataIndex >= 0 && oldDataIndex < mTotalDataList.Count && newDataIndex >= 0 && newDataIndex < mTotalDataList.Count)
  80. {
  81. var tempData = mTotalDataList[oldDataIndex];
  82. mTotalDataList[oldDataIndex] = mTotalDataList[newDataIndex];
  83. mTotalDataList[newDataIndex] = tempData;
  84. }
  85. }
  86. private float mSliderValue = 0;
  87. private void OnGUI()
  88. {
  89. mReorderableList.list = mPartDataArray;
  90. //绘制可拖拽列表
  91. float reorderableListPosX = 0;
  92. float reorderableListPosY = 0;
  93. float reorderableListWidth = position.width - 20;
  94. Rect reorderableListRect = new Rect(reorderableListPosX , reorderableListPosY , reorderableListWidth , 0);
  95. mReorderableList.DoList(reorderableListRect);
  96. //绘制列表的滚动条。使用mPartDataArray.Length作为滑块的大小,可以保障滑块到底部时的索引与数据索引一致。
  97. Rect reorderableSliderRect = new Rect(reorderableListWidth , reorderableListPosY , 0 , mReorderableList.GetHeight());
  98. int sliderMaxValue = mTotalDataList.Count > mPartDataArray.Length ? mTotalDataList.Count : mPartDataArray.Length;
  99. mSliderValue = GUI.VerticalScrollbar(reorderableSliderRect , mSliderValue , mPartDataArray.Length , 0f , sliderMaxValue);
  100. }
  101. private void InitTestData()
  102. {
  103. mTotalDataList.Clear();
  104. for(int i = 0 ; i < 50 ; i++)
  105. {
  106. mTotalDataList.Add(new TestElementData()
  107. {
  108. Name = "Name" + i ,
  109. Value = "Value" + i
  110. });
  111. }
  112. }
  113. }
  114. public class TestElementData
  115. {
  116. public string Name;
  117. public string Value;
  118. }
  119. }

视频演示效果

此处为语雀卡片,点击链接查看

参考