11using UnityEditor ;
22using UnityEngine ;
3+ using System ;
4+ using System . Collections . Generic ;
5+
36namespace Code . LevelEditor . Editor
47{
58 public class BlockLibraryWindow : EditorWindow
69 {
710 private BlockLibrary _blockLibrary ;
8-
11+
912 private string _newBlockId = "" ;
1013 private Sprite _newBlockSprite ;
1114 private GameObject _newBlockPrefab ;
12-
15+
1316 private Vector2 _scrollPosition ;
1417 private string _searchFilter = "" ;
1518
19+ private enum SortMode { ByID , ByPrefabName , ByIconName }
20+ private SortMode _sortMode = SortMode . ByID ;
21+ private bool _sortAscending = true ;
22+
1623 [ MenuItem ( "Tools/Block Library 🧱" ) ]
1724 public static void ShowWindow ( )
1825 {
@@ -22,12 +29,7 @@ public static void ShowWindow()
2229
2330 private void OnEnable ( )
2431 {
25- string [ ] guids = AssetDatabase . FindAssets ( "t:BlockLibrary" ) ;
26- if ( guids . Length > 0 )
27- {
28- string path = AssetDatabase . GUIDToAssetPath ( guids [ 0 ] ) ;
29- _blockLibrary = AssetDatabase . LoadAssetAtPath < BlockLibrary > ( path ) ;
30- }
32+ LoadOrCreateLibrary ( ) ;
3133 }
3234
3335 private void OnGUI ( )
@@ -43,8 +45,9 @@ private void OnGUI()
4345 }
4446
4547 DrawSearchField ( ) ;
46- GUILayout . Space ( 10 ) ;
48+ DrawSortControls ( ) ;
4749
50+ GUILayout . Space ( 10 ) ;
4851 _scrollPosition = EditorGUILayout . BeginScrollView ( _scrollPosition ) ;
4952 DrawExistingBlocks ( ) ;
5053 EditorGUILayout . EndScrollView ( ) ;
@@ -53,12 +56,55 @@ private void OnGUI()
5356 DrawCreateBlockSection ( ) ;
5457 }
5558
59+ private void LoadOrCreateLibrary ( )
60+ {
61+ string [ ] guids = AssetDatabase . FindAssets ( "t:BlockLibrary" ) ;
62+ if ( guids . Length > 0 )
63+ {
64+ string path = AssetDatabase . GUIDToAssetPath ( guids [ 0 ] ) ;
65+ _blockLibrary = AssetDatabase . LoadAssetAtPath < BlockLibrary > ( path ) ;
66+ }
67+ else
68+ {
69+ CreateLibrary ( ) ;
70+ }
71+ }
72+
73+ private void CreateLibrary ( )
74+ {
75+ var asset = CreateInstance < BlockLibrary > ( ) ;
76+ string folderPath = "Assets/Resources/StaticData/BlocksData" ;
77+ if ( ! AssetDatabase . IsValidFolder ( folderPath ) )
78+ AssetDatabase . CreateFolder ( "Assets/Resources/StaticData" , "BlocksData" ) ;
79+
80+ string path = $ "{ folderPath } /BlockLibrary.asset";
81+ AssetDatabase . CreateAsset ( asset , path ) ;
82+ AssetDatabase . SaveAssets ( ) ;
83+ AssetDatabase . Refresh ( ) ;
84+ _blockLibrary = asset ;
85+ Debug . Log ( "✅ BlockLibrary created at " + path ) ;
86+ }
87+
5688 private void DrawSearchField ( )
5789 {
5890 GUILayout . Label ( "Search Blocks" , EditorStyles . boldLabel ) ;
5991 _searchFilter = EditorGUILayout . TextField ( "Filter" , _searchFilter ) ;
6092 }
6193
94+ private void DrawSortControls ( )
95+ {
96+ GUILayout . BeginHorizontal ( "box" ) ;
97+ GUILayout . Label ( "Sort By:" , GUILayout . Width ( 60 ) ) ;
98+ _sortMode = ( SortMode ) EditorGUILayout . EnumPopup ( _sortMode ) ;
99+
100+ string arrow = _sortAscending ? "▲" : "▼" ;
101+ if ( GUILayout . Button ( arrow , GUILayout . Width ( 25 ) ) )
102+ {
103+ _sortAscending = ! _sortAscending ;
104+ }
105+ GUILayout . EndHorizontal ( ) ;
106+ }
107+
62108 private void DrawExistingBlocks ( )
63109 {
64110 GUILayout . Label ( "Existing Blocks" , EditorStyles . boldLabel ) ;
@@ -69,17 +115,32 @@ private void DrawExistingBlocks()
69115 return ;
70116 }
71117
72- BlockDataEditor blockToDelete = null ;
118+ List < BlockDataEditor > blocksToShow = new List < BlockDataEditor > ( _blockLibrary . AllBlocks ) ;
119+
120+ if ( ! string . IsNullOrEmpty ( _searchFilter ) )
121+ {
122+ blocksToShow = blocksToShow . FindAll ( block =>
123+ block != null && block . ID . ToLower ( ) . Contains ( _searchFilter . ToLower ( ) ) ) ;
124+ }
125+
126+ SortBlocks ( ) ;
73127
74- foreach ( var block in _blockLibrary . AllBlocks )
128+ if ( ! _sortAscending )
129+ blocksToShow . Reverse ( ) ;
130+
131+ for ( int i = blocksToShow . Count - 1 ; i >= 0 ; i -- )
75132 {
76- if ( block == null ) continue ;
77- if ( ! string . IsNullOrEmpty ( _searchFilter ) && ! block . ID . ToLower ( ) . Contains ( _searchFilter . ToLower ( ) ) )
133+ var block = blocksToShow [ i ] ;
134+ if ( block == null )
135+ {
136+ _blockLibrary . AllBlocks . RemoveAt ( i ) ;
78137 continue ;
138+ }
79139
80140 EditorGUILayout . BeginHorizontal ( "box" ) ;
81141
82- GUILayout . Label ( block . Icon != null ? block . Icon . texture : Texture2D . grayTexture , GUILayout . Width ( 32 ) , GUILayout . Height ( 32 ) ) ;
142+ GUILayout . Label ( block . Icon != null ? block . Icon . texture : Texture2D . grayTexture ,
143+ GUILayout . Width ( 32 ) , GUILayout . Height ( 32 ) ) ;
83144 GUILayout . Label ( block . ID ) ;
84145
85146 GUILayout . FlexibleSpace ( ) ;
@@ -91,23 +152,42 @@ private void DrawExistingBlocks()
91152 {
92153 if ( EditorUtility . DisplayDialog ( "Delete Block" , $ "Are you sure you want to delete '{ block . ID } '?", "Yes" , "No" ) )
93154 {
94- blockToDelete = block ;
155+ string path = AssetDatabase . GetAssetPath ( block ) ;
156+ _blockLibrary . AllBlocks . Remove ( block ) ;
157+ AssetDatabase . DeleteAsset ( path ) ;
158+ EditorUtility . SetDirty ( _blockLibrary ) ;
159+ AssetDatabase . SaveAssets ( ) ;
160+ AssetDatabase . Refresh ( ) ;
161+ GUIUtility . ExitGUI ( ) ;
95162 }
96163 }
97164
98165 EditorGUILayout . EndHorizontal ( ) ;
99166 }
167+ }
100168
101- if ( blockToDelete != null )
169+ private void SortBlocks ( )
170+ {
171+ _blockLibrary . AllBlocks . Sort ( ( a , b ) =>
102172 {
103- _blockLibrary . AllBlocks . Remove ( blockToDelete ) ;
104- DestroyImmediate ( blockToDelete , true ) ;
105- EditorUtility . SetDirty ( _blockLibrary ) ;
106- AssetDatabase . SaveAssets ( ) ;
107- AssetDatabase . Refresh ( ) ;
108- }
173+ switch ( _sortMode )
174+ {
175+ case SortMode . ByID :
176+ return string . Compare ( a . ID , b . ID , StringComparison . OrdinalIgnoreCase ) ;
177+ case SortMode . ByIconName :
178+ var aSprite = a . Icon != null ? a . Icon . name : string . Empty ;
179+ var bSprite = b . Icon != null ? b . Icon . name : string . Empty ;
180+ return string . Compare ( aSprite , bSprite , StringComparison . OrdinalIgnoreCase ) ;
181+ case SortMode . ByPrefabName :
182+ var aPrefab = a . Prefab != null ? a . Prefab . name : string . Empty ;
183+ var bPrefab = b . Prefab != null ? b . Prefab . name : string . Empty ;
184+ return string . Compare ( aPrefab , bPrefab , StringComparison . OrdinalIgnoreCase ) ;
185+ default :
186+ return 0 ;
187+ }
188+ } ) ;
109189 }
110-
190+
111191 private void DrawCreateBlockSection ( )
112192 {
113193 GUILayout . Label ( "Create New Block" , EditorStyles . boldLabel ) ;
@@ -126,35 +206,34 @@ private void DrawCreateBlockSection()
126206
127207 private void CreateNewBlock ( )
128208 {
209+ if ( _blockLibrary == null )
210+ {
211+ Debug . LogError ( "BlockLibrary not assigned or found." ) ;
212+ return ;
213+ }
214+
215+ string folderPath = "Assets/Resources/StaticData/BlocksData" ;
216+ if ( ! AssetDatabase . IsValidFolder ( folderPath ) )
217+ AssetDatabase . CreateFolder ( "Assets/Resources/StaticData" , "BlocksData" ) ;
218+
129219 BlockDataEditor newBlock = CreateInstance < BlockDataEditor > ( ) ;
130220 newBlock . name = _newBlockId ;
131221 newBlock . SetID ( _newBlockId ) ;
132222 newBlock . SetIcon ( _newBlockSprite ) ;
133223 newBlock . SetPrefab ( _newBlockPrefab ) ;
134224
135- AssetDatabase . AddObjectToAsset ( newBlock , AssetDatabase . GetAssetPath ( _blockLibrary ) ) ;
136- AssetDatabase . ImportAsset ( AssetDatabase . GetAssetPath ( newBlock ) ) ;
225+ string assetPath = $ "{ folderPath } /{ _newBlockId } .asset";
226+ AssetDatabase . CreateAsset ( newBlock , assetPath ) ;
227+ AssetDatabase . SaveAssets ( ) ;
228+ AssetDatabase . Refresh ( ) ;
137229
138230 _blockLibrary . AllBlocks . Add ( newBlock ) ;
139231 EditorUtility . SetDirty ( _blockLibrary ) ;
140- AssetDatabase . SaveAssets ( ) ;
141- AssetDatabase . Refresh ( ) ;
142232
143233 Debug . Log ( $ "✅ Created new block: { _newBlockId } ") ;
144234 _newBlockId = "" ;
145235 _newBlockSprite = null ;
146236 _newBlockPrefab = null ;
147237 }
148-
149- private void CreateLibrary ( )
150- {
151- var asset = CreateInstance < BlockLibrary > ( ) ;
152- string path = "Assets/Resources/StaticData/BlockLibrary.asset" ;
153- AssetDatabase . CreateAsset ( asset , path ) ;
154- AssetDatabase . SaveAssets ( ) ;
155- AssetDatabase . Refresh ( ) ;
156- _blockLibrary = asset ;
157- Debug . Log ( "✅ BlockLibrary created at " + path ) ;
158- }
159238 }
160239}
0 commit comments