// CollectionEditorOnInspectorGUI 是一个泛型方法,负责对所有的 CollectionEditor 相关的编辑器进行渲染 // 使用的时候需要在相应的 CollectionEditor 的 OnInspectorGUI() 中 call 这个function public static void CollectionEditorOnInspectorGUI <T>(SerializedProperty collectionProperty, string name, T editor, ref bool fold) where T : Editor { //EditorGUI.indentLevel++; GUILayout.BeginVertical(GUI.skin.box); fold = EditorGUILayout.Foldout(fold, name + " Collection"); if (fold) { EditorGUI.indentLevel++; for (int i = 0; i < collectionProperty.arraySize; i++) { GUILayout.BeginHorizontal(); SerializedProperty elementProperty = collectionProperty.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(elementProperty); if (GUILayout.Button("-", GUILayout.Width(18.0f))) { collectionProperty.RemoveElementFromPropertyByIndex(i); } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; GUILayout.Space(5.0f); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); string buttonStr = "Add New " + name; if (GUILayout.Button(buttonStr, GUILayout.Width(150.0f))) { collectionProperty.AddEmptyElementToProperty(); } buttonStr = "Remove " + name; if (GUILayout.Button(buttonStr, GUILayout.Width(150.0f))) { int index = collectionProperty.arraySize; collectionProperty.RemoveElementFromPropertyByIndex(index - 1); } GUILayout.EndHorizontal(); GUILayout.Space(5.0f); Rect fullWidthRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(60.0f)); DragAndDropAreaGUI(fullWidthRect, CheckEditorTargetType(editor).ToString()); DraggingAndDropping(fullWidthRect, editor); } GUILayout.EndVertical(); //EditorGUI.indentLevel--; }