Skip to content

Commit d1321e0

Browse files
committed
fix: DrawSelectedBlockSection
1 parent 3b5c508 commit d1321e0

File tree

15 files changed

+222
-42
lines changed

15 files changed

+222
-42
lines changed

Assets/Code/LevelEditor/BlockDataEditor.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public class BlockDataEditor : ScriptableObject
2020
public void SetID(string newId)
2121
{
2222
id = newId;
23+
this.name = newId;
24+
2325
#if UNITY_EDITOR
24-
if (!string.IsNullOrEmpty(id))
26+
string assetPath = AssetDatabase.GetAssetPath(this);
27+
if (!string.IsNullOrEmpty(assetPath))
2528
{
26-
string assetPath = AssetDatabase.GetAssetPath(this);
27-
if (!string.IsNullOrEmpty(assetPath))
28-
{
29-
AssetDatabase.RenameAsset(assetPath, id);
30-
EditorUtility.SetDirty(this);
31-
}
29+
AssetDatabase.RenameAsset(assetPath, newId);
30+
EditorUtility.SetDirty(this);
31+
AssetDatabase.SaveAssets();
3232
}
3333
#endif
3434
}
@@ -63,12 +63,7 @@ private void OnValidate()
6363
{
6464
if (!string.IsNullOrEmpty(id))
6565
{
66-
string assetPath = AssetDatabase.GetAssetPath(this);
67-
if (!string.IsNullOrEmpty(assetPath))
68-
{
69-
AssetDatabase.RenameAsset(assetPath, id);
70-
EditorUtility.SetDirty(this);
71-
}
66+
SetID(id);
7267
}
7368
}
7469
#endif

Assets/Code/LevelEditor/BlockLibraryWindow.cs

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,12 @@ public static void ShowWindow()
4444
[EnumToggleButtons, HideLabel, PropertyOrder(0)]
4545
[SerializeField]
4646
private SortMode _sortMode = SortMode.ByID;
47-
48-
[HorizontalGroup("🔍 Search & Sort/SortRow", width: 25)]
49-
[Button("@_sortAscending ? \"\" : \"\"", ButtonSizes.Medium)]
50-
[PropertyOrder(1)]
51-
private void ToggleSortDirection() => _sortAscending = !_sortAscending;
52-
47+
48+
private BlockDataEditor _selectedBlock;
49+
private BlockEditorDTO _blockDraft = new BlockEditorDTO();
5350
private bool _sortAscending = true;
5451
private Vector2 _scroll;
5552

56-
[BoxGroup("🧾 Selected Block", centerLabel: true)]
57-
[ShowInInspector, InlineEditor(InlineEditorObjectFieldModes.Hidden)]
58-
[PropertyOrder(100)]
59-
private BlockDataEditor _selectedBlock;
60-
6153
protected override void OnEnable()
6254
{
6355
base.OnEnable();
@@ -80,17 +72,13 @@ protected override void DrawEditor(int index)
8072

8173
GUILayout.Space(10);
8274

83-
SirenixEditorGUI.BeginBox();
84-
SirenixEditorGUI.BeginBoxHeader();
85-
SirenixEditorGUI.Title("📦 Existing Blocks", null, TextAlignment.Center, true);
86-
SirenixEditorGUI.EndBoxHeader();
87-
75+
DrawSelectedBlockSection();
76+
GUILayout.Space(10);
77+
8878
_scroll = EditorGUILayout.BeginScrollView(_scroll);
8979
DrawExistingBlocks();
9080
GUILayout.FlexibleSpace();
9181
EditorGUILayout.EndScrollView();
92-
93-
SirenixEditorGUI.EndBox();
9482
}
9583

9684
private void LoadOrCreateLibrary()
@@ -123,6 +111,48 @@ private void CreateLibrary()
123111
Debug.Log("✅ BlockLibrary created at " + path);
124112
}
125113

114+
private void DrawSelectedBlockSection()
115+
{
116+
SirenixEditorGUI.BeginBox();
117+
SirenixEditorGUI.BeginBoxHeader();
118+
SirenixEditorGUI.Title("🎯 Selected Block", null, TextAlignment.Center, true);
119+
SirenixEditorGUI.EndBoxHeader();
120+
121+
if (_selectedBlock == null)
122+
{
123+
SirenixEditorGUI.InfoMessageBox("No block selected.");
124+
}
125+
else
126+
{
127+
EditorGUILayout.BeginHorizontal();
128+
GUILayout.Label(_selectedBlock.Icon != null ? _selectedBlock.Icon.texture : Texture2D.grayTexture, GUILayout.Width(32), GUILayout.Height(32));
129+
GUILayout.Label(_selectedBlock.ID, SirenixGUIStyles.BoldLabel);
130+
GUILayout.FlexibleSpace();
131+
EditorGUILayout.EndHorizontal();
132+
133+
GUILayout.Space(8);
134+
135+
// Поля редактирования DTO
136+
_blockDraft.Id = EditorGUILayout.TextField("ID", _blockDraft.Id);
137+
_blockDraft.Icon = (Sprite)EditorGUILayout.ObjectField("Icon", _blockDraft.Icon, typeof(Sprite), false);
138+
_blockDraft.Prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", _blockDraft.Prefab, typeof(GameObject), false);
139+
140+
GUILayout.Space(4);
141+
142+
if (GUILayout.Button("💾 Apply Changes", GUILayout.Height(25)))
143+
{
144+
_blockDraft.ApplyTo(_selectedBlock);
145+
}
146+
}
147+
148+
SirenixEditorGUI.EndBox();
149+
}
150+
151+
[HorizontalGroup("🔍 Search & Sort/SortRow", width: 25)]
152+
[Button("@_sortAscending ? \"\" : \"\"", ButtonSizes.Medium)]
153+
[PropertyOrder(1)]
154+
private void ToggleSortDirection() => _sortAscending = !_sortAscending;
155+
126156
private void DrawExistingBlocks()
127157
{
128158
if (_blockLibrary.AllBlocks == null || _blockLibrary.AllBlocks.Count == 0)
@@ -138,12 +168,15 @@ private void DrawExistingBlocks()
138168
blocks = blocks.FindAll(b => b != null && b.ID.ToLower().Contains(_searchFilter.ToLower()));
139169
}
140170

141-
blocks.Sort((a, b) => _sortMode switch
171+
blocks.Sort((a, b) =>
142172
{
143-
SortMode.ByID => string.Compare(a.ID, b.ID, StringComparison.OrdinalIgnoreCase),
144-
SortMode.ByIconName => string.Compare(a.Icon?.name ?? "", b.Icon?.name ?? "", StringComparison.OrdinalIgnoreCase),
145-
SortMode.ByPrefabName => SafeCompare(a.Prefab, b.Prefab),
146-
_ => 0
173+
return _sortMode switch
174+
{
175+
SortMode.ByID => string.Compare(a.ID, b.ID, StringComparison.OrdinalIgnoreCase),
176+
SortMode.ByIconName => string.Compare(a.Icon?.name ?? "", b.Icon?.name ?? "", StringComparison.OrdinalIgnoreCase),
177+
SortMode.ByPrefabName => SafeCompare(a.Prefab, b.Prefab),
178+
_ => 0
179+
};
147180
});
148181

149182
if (!_sortAscending)
@@ -159,14 +192,12 @@ private void DrawExistingBlocks()
159192
SirenixEditorGUI.EndBoxHeader();
160193

161194
GUILayout.BeginHorizontal();
195+
162196
GUILayout.Label(block.Icon != null ? block.Icon.texture : Texture2D.grayTexture, GUILayout.Width(32), GUILayout.Height(32));
163197
GUILayout.FlexibleSpace();
164198

165199
if (GUILayout.Button("Select", GUILayout.Width(60)))
166-
{
167200
_selectedBlock = block;
168-
Selection.activeObject = block;
169-
}
170201

171202
if (GUILayout.Button("X", GUILayout.Width(20)))
172203
{
@@ -230,4 +261,28 @@ private int SafeCompare(UnityEngine.Object a, UnityEngine.Object b)
230261
}
231262
}
232263
}
233-
}
264+
265+
[Serializable]
266+
public class BlockEditorDTO
267+
{
268+
public string Id;
269+
public Sprite Icon;
270+
public GameObject Prefab;
271+
272+
public void LoadFrom(BlockDataEditor block)
273+
{
274+
Id = block.ID;
275+
Icon = block.Icon;
276+
Prefab = block.Prefab;
277+
}
278+
279+
public void ApplyTo(BlockDataEditor block)
280+
{
281+
block.SetID(Id);
282+
block.SetIcon(Icon);
283+
block.SetPrefab(Prefab);
284+
EditorUtility.SetDirty(block);
285+
AssetDatabase.SaveAssets();
286+
}
287+
}
288+
}

Assets/Resources/StaticData/BlocksData/BlockLibrary.asset

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ MonoBehaviour:
1414
m_EditorClassIdentifier:
1515
AllBlocks:
1616
- {fileID: 11400000, guid: d7831333fc0fec44f9c7d1e185ca6ba9, type: 2}
17+
- {fileID: 11400000, guid: 39174ad4a8c18074f943c604c8246649, type: 2}
18+
- {fileID: 11400000, guid: 0c775f43ddd810b4e84cee93c7324651, type: 2}
19+
- {fileID: 11400000, guid: 6f840c6e80be02847a36539af4966f44, type: 2}
20+
- {fileID: 11400000, guid: 653a1b7a0d0a7584db631a6d3fe22975, type: 2}
21+
- {fileID: 11400000, guid: 275269e371b517a4c904804a054d498d, type: 2}

Assets/Resources/StaticData/BlocksData/Test_1.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ MonoBehaviour:
1313
m_Name: Test_1
1414
m_EditorClassIdentifier:
1515
id: Test_1
16-
icon: {fileID: 21300000, guid: 46ccb3ef822a43d429313a5a80c54504, type: 3}
16+
icon: {fileID: 21300000, guid: 8da47673cd771ef41b510dbc20b6aaa4, type: 3}
1717
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/Test_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: Test_2
14+
m_EditorClassIdentifier:
15+
id: Test_2
16+
icon: {fileID: 21300000, guid: 60b64bb9f8fe2424bbcd1d7d5c414084, type: 3}
17+
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/Test_2.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: Test_3
14+
m_EditorClassIdentifier:
15+
id: Test_3
16+
icon: {fileID: 21300000, guid: 2765584043d58ed4b94a001f125a202d, type: 3}
17+
prefab: {fileID: 0}

Assets/Resources/StaticData/BlocksData/Test_3.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: Test_4
14+
m_EditorClassIdentifier:
15+
id: Test_4
16+
icon: {fileID: 21300000, guid: 5068bed0629f6df43a4163b3bf71df86, type: 3}
17+
prefab: {fileID: 0}

0 commit comments

Comments
 (0)