77public class ScriptableObjectEditorWindow : EditorWindow
88{
99 // layout:
10- private float PropertyMinWidth = 40 ;
11- private float PropertySpace = 4 ;
12- private Vector2 ScrollPos = new Vector2 ( ) ;
13- private GUILayoutOption [ ] GUIL_StandartOptions = new GUILayoutOption [ ] { GUILayout . MinWidth ( 100 ) , GUILayout . ExpandWidth ( true ) } ;
14- private GUILayoutOption [ ] GUIL_DefaultOptions = new GUILayoutOption [ ] { GUILayout . MinWidth ( 150 ) , GUILayout . ExpandWidth ( true ) } ;
10+ private readonly float PropertyMinWidth = 40 ;
11+ private readonly float PropertySpace = 5 ;
12+ private Vector2 ScrollPosMain = new ( ) ;
13+ private readonly GUILayoutOption [ ] GUIL_StandartOptions = new GUILayoutOption [ ] { GUILayout . MinWidth ( 100 ) , GUILayout . ExpandWidth ( true ) } ;
14+ private readonly GUILayoutOption [ ] GUIL_DefaultOptions = new GUILayoutOption [ ] { GUILayout . MinWidth ( 150 ) , GUILayout . ExpandWidth ( true ) } ;
1515
1616 // data:
1717 private List < List < ScriptableObject > > groupedConfigs ; // Stores ScriptableObjects grouped by their class types in nested lists
18- private List < Type > selectedTypes = new List < Type > ( ) ; // Tracks which ScriptableObject types are currently selected for display
19- private List < Type > availableTypes = new List < Type > ( ) ; // Holds all unique ScriptableObject types found in the project
18+ private readonly List < Type > selectedTypes = new ( ) ; // Tracks which ScriptableObject types are currently selected for display
19+ private List < Type > availableTypes = new ( ) ; // Holds all unique ScriptableObject types found in the project
2020
2121 // window:
2222 [ MenuItem ( "Window/Game Config Editor" ) ]
@@ -58,7 +58,7 @@ private void OnGUI()
5858 }
5959
6060 // show configs
61- ScrollPos = EditorGUILayout . BeginScrollView ( ScrollPos ) ;
61+ ScrollPosMain = EditorGUILayout . BeginScrollView ( ScrollPosMain ) ;
6262 if ( groupedConfigs . Count != 0 )
6363 {
6464 foreach ( var configGroup in groupedConfigs )
@@ -147,14 +147,14 @@ private void PutPropertiesForObject<T>(List<T> Configs) where T : ScriptableObje
147147 {
148148 try
149149 {
150- EditorGUILayout . BeginVertical ( "box" ) ;
150+ EditorGUILayout . BeginVertical ( "box" , GUILayout . Width ( 170 ) ) ;
151151 try
152152 {
153153 // An empty label to align property names properly in the UI
154154 EditorGUILayout . LabelField ( "" , GUILayout . MinWidth ( PropertyMinWidth ) ) ;
155155
156156 // Iterate through the properties of the first ScriptableObject to display their names
157- SerializedObject serializedObject = new SerializedObject ( Configs [ 0 ] ) ;
157+ SerializedObject serializedObject = new ( Configs [ 0 ] ) ;
158158 SerializedProperty property = serializedObject . GetIterator ( ) ;
159159
160160 bool ShouldNext = property . NextVisible ( true ) ;
@@ -180,6 +180,9 @@ private void PutPropertiesForObject<T>(List<T> Configs) where T : ScriptableObje
180180 }
181181 EditorGUILayout . EndVertical ( ) ;
182182
183+ if ( Configs . Count == 0 )
184+ return ; // Exit if there are no configurations to display
185+
183186 foreach ( var Config in Configs )
184187 {
185188 EditorGUILayout . BeginVertical ( "box" ) ;
@@ -196,17 +199,11 @@ private void PutPropertiesForObject<T>(List<T> Configs) where T : ScriptableObje
196199 if ( GUILayout . Button ( "del" , GUILayout . MaxWidth ( 30 ) ) )
197200 {
198201 // Delete the selected ScriptableObject asset
199- if ( EditorUtility . DisplayDialog ( "Delete Config" , "Are you sure you want to delete this config? \n \n You cannot undo delete assets action." , "Yes" , "No" ) )
200- {
201- AssetDatabase . DeleteAsset ( AssetDatabase . GetAssetPath ( Config ) ) ;
202- AssetDatabase . SaveAssets ( ) ;
203- AssetDatabase . Refresh ( ) ;
204- GroupScriptableObjectsByType ( ) ; // Refresh the list after deletion
205- }
202+ DeleteConfig ( Config ) ;
206203 }
207204 EditorGUILayout . EndHorizontal ( ) ;
208205
209- SerializedObject serializedObject = new SerializedObject ( Config ) ;
206+ SerializedObject serializedObject = new ( Config ) ;
210207 SerializedProperty property = serializedObject . GetIterator ( ) ;
211208
212209 // Iterate through all properties to create editable fields based on their types
@@ -237,6 +234,7 @@ private void PutPropertiesForObject<T>(List<T> Configs) where T : ScriptableObje
237234 }
238235 EditorGUILayout . EndVertical ( ) ;
239236 }
237+
240238 }
241239 catch
242240 {
@@ -246,13 +244,24 @@ private void PutPropertiesForObject<T>(List<T> Configs) where T : ScriptableObje
246244 }
247245 }
248246
247+ private void DeleteConfig < T > ( T Config ) where T : ScriptableObject
248+ {
249+ if ( EditorUtility . DisplayDialog ( "Delete Config" , "Are you sure you want to delete this config? \n \n You cannot undo delete assets action." , "Yes" , "No" ) )
250+ {
251+ AssetDatabase . DeleteAsset ( AssetDatabase . GetAssetPath ( Config ) ) ;
252+ AssetDatabase . SaveAssets ( ) ;
253+ AssetDatabase . Refresh ( ) ;
254+ GroupScriptableObjectsByType ( ) ; // Refresh the list after deletion
255+ }
256+ }
257+
249258 public static float CalculatePropertyHeight < T > ( List < T > configs , SerializedProperty property ) where T : ScriptableObject
250259 {
251260 float maxHeight = 0f ;
252261
253262 foreach ( var config in configs )
254263 {
255- SerializedObject serializedObject = new SerializedObject ( config ) ;
264+ SerializedObject serializedObject = new ( config ) ;
256265 SerializedProperty targetProperty = serializedObject . FindProperty ( property . propertyPath ) ;
257266
258267 if ( targetProperty != null )
@@ -270,16 +279,16 @@ public static float CalculatePropertyHeight<T>(List<T> configs, SerializedProper
270279// Custom Popup Window for Config Type Selection
271280public class ConfigTypeSelectionPopup : PopupWindowContent
272281{
273- private List < Type > selectedTypes ;
274- private List < Type > availableTypes ;
275- private Action onSelectionChanged ;
276- private Vector2 ScrollPos = new Vector2 ( ) ;
282+ private readonly List < Type > SelectedTypes ;
283+ private readonly List < Type > AvailableTypes ;
284+ private readonly Action OnSelectionChanged ;
285+ private Vector2 ScrollPos = new ( ) ;
277286
278287 public ConfigTypeSelectionPopup ( List < Type > selectedTypes , Action onSelectionChanged , List < Type > availableTypes )
279288 {
280- this . selectedTypes = selectedTypes ;
281- this . onSelectionChanged = onSelectionChanged ;
282- this . availableTypes = availableTypes ;
289+ this . SelectedTypes = selectedTypes ;
290+ this . OnSelectionChanged = onSelectionChanged ;
291+ this . AvailableTypes = availableTypes ;
283292 }
284293
285294 public override Vector2 GetWindowSize ( )
@@ -291,25 +300,25 @@ public override void OnGUI(Rect rect)
291300 {
292301 GUILayout . Label ( "Select Config Types" , EditorStyles . boldLabel ) ;
293302
294- if ( availableTypes . Count > 0 )
303+ if ( AvailableTypes . Count > 0 )
295304 {
296305 ScrollPos = EditorGUILayout . BeginScrollView ( ScrollPos ) ;
297306
298307 // Display each available type with a toggle to enable or disable it
299- foreach ( var type in availableTypes )
308+ foreach ( var type in AvailableTypes )
300309 {
301- bool isSelected = selectedTypes . Contains ( type ) ;
310+ bool isSelected = SelectedTypes . Contains ( type ) ;
302311 bool toggle = EditorGUILayout . Toggle ( type . Name , isSelected ) ;
303312
304313 if ( toggle && ! isSelected )
305314 {
306- selectedTypes . Add ( type ) ; // Add the type to the selection list
307- onSelectionChanged . Invoke ( ) ;
315+ SelectedTypes . Add ( type ) ; // Add the type to the selection list
316+ OnSelectionChanged . Invoke ( ) ;
308317 }
309318 else if ( ! toggle && isSelected )
310319 {
311- selectedTypes . Remove ( type ) ; // Remove the type from the selection list
312- onSelectionChanged . Invoke ( ) ;
320+ SelectedTypes . Remove ( type ) ; // Remove the type from the selection list
321+ OnSelectionChanged . Invoke ( ) ;
313322 }
314323 }
315324
0 commit comments