Skip to content

Commit f129726

Browse files
committed
add: BlockLibraryWindow
1 parent 2a5ef62 commit f129726

File tree

8 files changed

+273
-26
lines changed

8 files changed

+273
-26
lines changed
Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,76 @@
11
using UnityEngine;
22

3+
#if UNITY_EDITOR
4+
using UnityEditor;
5+
#endif
6+
37
namespace Code.LevelEditor
48
{
59
[CreateAssetMenu(fileName = "NewBlock", menuName = "StaticData/Levels/Block", order = 802)]
610
public class BlockDataEditor : ScriptableObject
711
{
8-
public string ID;
9-
public Sprite Icon;
10-
public GameObject Prefab;
12+
[SerializeField] private string id;
13+
[SerializeField] private Sprite icon;
14+
[SerializeField] private GameObject prefab;
15+
16+
public string ID => id;
17+
public Sprite Icon => icon;
18+
public GameObject Prefab => prefab;
19+
20+
public void SetID(string newId)
21+
{
22+
id = newId;
23+
#if UNITY_EDITOR
24+
if (!string.IsNullOrEmpty(id))
25+
{
26+
string assetPath = AssetDatabase.GetAssetPath(this);
27+
if (!string.IsNullOrEmpty(assetPath))
28+
{
29+
AssetDatabase.RenameAsset(assetPath, id);
30+
EditorUtility.SetDirty(this);
31+
}
32+
}
33+
#endif
34+
}
35+
36+
public void SetIcon(Sprite newIcon)
37+
{
38+
icon = newIcon;
39+
}
40+
41+
public void SetPrefab(GameObject newPrefab)
42+
{
43+
prefab = newPrefab;
44+
}
45+
46+
public override string ToString()
47+
{
48+
return string.IsNullOrEmpty(id) ? "Unnamed Block" : id;
49+
}
50+
51+
public override bool Equals(object obj)
52+
{
53+
return obj is BlockDataEditor other && id == other.id;
54+
}
55+
56+
public override int GetHashCode()
57+
{
58+
return id != null ? id.GetHashCode() : 0;
59+
}
60+
61+
#if UNITY_EDITOR
62+
private void OnValidate()
63+
{
64+
if (!string.IsNullOrEmpty(id))
65+
{
66+
string assetPath = AssetDatabase.GetAssetPath(this);
67+
if (!string.IsNullOrEmpty(assetPath))
68+
{
69+
AssetDatabase.RenameAsset(assetPath, id);
70+
EditorUtility.SetDirty(this);
71+
}
72+
}
73+
}
74+
#endif
1175
}
1276
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
namespace Code.LevelEditor.Editor
4+
{
5+
public class BlockLibraryWindow : EditorWindow
6+
{
7+
private BlockLibrary _blockLibrary;
8+
9+
private string _newBlockId = "";
10+
private Sprite _newBlockSprite;
11+
private GameObject _newBlockPrefab;
12+
13+
private Vector2 _scrollPosition;
14+
private string _searchFilter = "";
15+
16+
[MenuItem("Tools/Block Library 🧱")]
17+
public static void ShowWindow()
18+
{
19+
var window = GetWindow<BlockLibraryWindow>(false, "Block Library", true);
20+
window.minSize = new Vector2(350, 400);
21+
}
22+
23+
private void OnEnable()
24+
{
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+
}
31+
}
32+
33+
private void OnGUI()
34+
{
35+
if (_blockLibrary == null)
36+
{
37+
EditorGUILayout.HelpBox("No BlockLibrary asset found.", MessageType.Warning);
38+
if (GUILayout.Button("Create BlockLibrary"))
39+
{
40+
CreateLibrary();
41+
}
42+
return;
43+
}
44+
45+
DrawSearchField();
46+
GUILayout.Space(10);
47+
48+
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
49+
DrawExistingBlocks();
50+
EditorGUILayout.EndScrollView();
51+
52+
GUILayout.Space(15);
53+
DrawCreateBlockSection();
54+
}
55+
56+
private void DrawSearchField()
57+
{
58+
GUILayout.Label("Search Blocks", EditorStyles.boldLabel);
59+
_searchFilter = EditorGUILayout.TextField("Filter", _searchFilter);
60+
}
61+
62+
private void DrawExistingBlocks()
63+
{
64+
GUILayout.Label("Existing Blocks", EditorStyles.boldLabel);
65+
66+
if (_blockLibrary.AllBlocks == null || _blockLibrary.AllBlocks.Count == 0)
67+
{
68+
GUILayout.Label("No blocks found.");
69+
return;
70+
}
71+
72+
BlockDataEditor blockToDelete = null;
73+
74+
foreach (var block in _blockLibrary.AllBlocks)
75+
{
76+
if (block == null) continue;
77+
if (!string.IsNullOrEmpty(_searchFilter) && !block.ID.ToLower().Contains(_searchFilter.ToLower()))
78+
continue;
79+
80+
EditorGUILayout.BeginHorizontal("box");
81+
82+
GUILayout.Label(block.Icon != null ? block.Icon.texture : Texture2D.grayTexture, GUILayout.Width(32), GUILayout.Height(32));
83+
GUILayout.Label(block.ID);
84+
85+
GUILayout.FlexibleSpace();
86+
87+
if (GUILayout.Button("Select", GUILayout.Width(60)))
88+
Selection.activeObject = block;
89+
90+
if (GUILayout.Button("X", GUILayout.Width(20)))
91+
{
92+
if (EditorUtility.DisplayDialog("Delete Block", $"Are you sure you want to delete '{block.ID}'?", "Yes", "No"))
93+
{
94+
blockToDelete = block;
95+
}
96+
}
97+
98+
EditorGUILayout.EndHorizontal();
99+
}
100+
101+
if (blockToDelete != null)
102+
{
103+
_blockLibrary.AllBlocks.Remove(blockToDelete);
104+
DestroyImmediate(blockToDelete, true);
105+
EditorUtility.SetDirty(_blockLibrary);
106+
AssetDatabase.SaveAssets();
107+
AssetDatabase.Refresh();
108+
}
109+
}
110+
111+
private void DrawCreateBlockSection()
112+
{
113+
GUILayout.Label("Create New Block", EditorStyles.boldLabel);
114+
115+
_newBlockId = EditorGUILayout.TextField("ID", _newBlockId);
116+
_newBlockSprite = (Sprite)EditorGUILayout.ObjectField("Sprite", _newBlockSprite, typeof(Sprite), false);
117+
_newBlockPrefab = (GameObject)EditorGUILayout.ObjectField("Prefab", _newBlockPrefab, typeof(GameObject), false);
118+
119+
GUI.enabled = !string.IsNullOrEmpty(_newBlockId) && _newBlockSprite != null;
120+
if (GUILayout.Button("Create Block", GUILayout.Height(30)))
121+
{
122+
CreateNewBlock();
123+
}
124+
GUI.enabled = true;
125+
}
126+
127+
private void CreateNewBlock()
128+
{
129+
BlockDataEditor newBlock = CreateInstance<BlockDataEditor>();
130+
newBlock.name = _newBlockId;
131+
newBlock.SetID(_newBlockId);
132+
newBlock.SetIcon(_newBlockSprite);
133+
newBlock.SetPrefab(_newBlockPrefab);
134+
135+
AssetDatabase.AddObjectToAsset(newBlock, AssetDatabase.GetAssetPath(_blockLibrary));
136+
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newBlock));
137+
138+
_blockLibrary.AllBlocks.Add(newBlock);
139+
EditorUtility.SetDirty(_blockLibrary);
140+
AssetDatabase.SaveAssets();
141+
AssetDatabase.Refresh();
142+
143+
Debug.Log($"✅ Created new block: {_newBlockId}");
144+
_newBlockId = "";
145+
_newBlockSprite = null;
146+
_newBlockPrefab = null;
147+
}
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+
}
159+
}
160+
}

Assets/Code/LevelEditor/BlockLibraryWindow.cs.meta

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

Assets/Resources/StaticData/BlocksData/Block_BlueBerry.asset

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

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

Assets/Resources/StaticData/BlocksData/BlockLibrary.asset.meta renamed to Assets/Resources/StaticData/BlocksData/test_01.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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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: 756ff7b64aa3495491247341179da300, type: 3}
13+
m_Name: text
14+
m_EditorClassIdentifier:
15+
AllBlocks:
16+
- {fileID: 3587345946097035076}
17+
--- !u!114 &3587345946097035076
18+
MonoBehaviour:
19+
m_ObjectHideFlags: 0
20+
m_CorrespondingSourceObject: {fileID: 0}
21+
m_PrefabInstance: {fileID: 0}
22+
m_PrefabAsset: {fileID: 0}
23+
m_GameObject: {fileID: 0}
24+
m_Enabled: 1
25+
m_EditorHideFlags: 0
26+
m_Script: {fileID: 11500000, guid: 73698c54e77f2b34ea84f49756a68126, type: 3}
27+
m_Name: text
28+
m_EditorClassIdentifier:
29+
id: text
30+
icon: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
31+
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/Block_BlueBerry.asset.meta renamed to Assets/Resources/StaticData/BlocksData/text.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.

0 commit comments

Comments
 (0)