Skip to content

Commit a12301f

Browse files
committed
feat: added sorted in BlockLibraryWindow
1 parent f129726 commit a12301f

File tree

11 files changed

+202
-59
lines changed

11 files changed

+202
-59
lines changed
Lines changed: 117 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
using UnityEditor;
22
using UnityEngine;
3+
using System;
4+
using System.Collections.Generic;
5+
36
namespace 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
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 73698c54e77f2b34ea84f49756a68126, type: 3}
13+
m_Name: Amar_1
14+
m_EditorClassIdentifier:
15+
id: Amar_1
16+
icon: {fileID: 21300000, guid: 7717b28d4af90ce4e9eae2c8ae0876ce, type: 3}
17+
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/text.asset.meta renamed to Assets/Resources/StaticData/BlocksData/Amar_1.asset.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 73698c54e77f2b34ea84f49756a68126, type: 3}
13+
m_Name: Amur
14+
m_EditorClassIdentifier:
15+
id: Amur
16+
icon: {fileID: 21300000, guid: 1c2691b95cb8945428c5e656e7bcb6cb, type: 3}
17+
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/test_01.asset.meta renamed to Assets/Resources/StaticData/BlocksData/Amur.asset.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/StaticData/BlocksData/test_01.asset renamed to Assets/Resources/StaticData/BlocksData/BlockLibrary.asset

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ MonoBehaviour:
1010
m_Enabled: 1
1111
m_EditorHideFlags: 0
1212
m_Script: {fileID: 11500000, guid: 756ff7b64aa3495491247341179da300, type: 3}
13-
m_Name: test_01
13+
m_Name: BlockLibrary
1414
m_EditorClassIdentifier:
15-
AllBlocks: []
15+
AllBlocks:
16+
- {fileID: 11400000, guid: 4e69c2dd531702e42a33d4a6c94c811a, type: 2}
17+
- {fileID: 11400000, guid: 96e05a53fcff82746af78cdfbda8503f, type: 2}
18+
- {fileID: 11400000, guid: fc5914abd9f97564983e7d01b39ed2fa, type: 2}

Assets/Resources/StaticData/BlocksData/BlockLibrary.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
%YAML 1.1
22
%TAG !u! tag:unity3d.com,2011:
33
--- !u!114 &11400000
4-
MonoBehaviour:
5-
m_ObjectHideFlags: 0
6-
m_CorrespondingSourceObject: {fileID: 0}
7-
m_PrefabInstance: {fileID: 0}
8-
m_PrefabAsset: {fileID: 0}
9-
m_GameObject: {fileID: 0}
10-
m_Enabled: 1
11-
m_EditorHideFlags: 0
12-
m_Script: {fileID: 11500000, guid: 756ff7b64aa3495491247341179da300, type: 3}
13-
m_Name: text
14-
m_EditorClassIdentifier:
15-
AllBlocks:
16-
- {fileID: 3587345946097035076}
17-
--- !u!114 &3587345946097035076
184
MonoBehaviour:
195
m_ObjectHideFlags: 0
206
m_CorrespondingSourceObject: {fileID: 0}
@@ -24,8 +10,8 @@ MonoBehaviour:
2410
m_Enabled: 1
2511
m_EditorHideFlags: 0
2612
m_Script: {fileID: 11500000, guid: 73698c54e77f2b34ea84f49756a68126, type: 3}
27-
m_Name: text
13+
m_Name: Text_1
2814
m_EditorClassIdentifier:
29-
id: text
15+
id: Text_1
3016
icon: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
3117
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/Text_1.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 73698c54e77f2b34ea84f49756a68126, type: 3}
13+
m_Name: Text_2
14+
m_EditorClassIdentifier:
15+
id: Text_2
16+
icon: {fileID: 21300000, guid: 1cc5832122ec43a4cbd0c713119107c1, type: 3}
17+
prefab: {fileID: 0}

0 commit comments

Comments
 (0)