Skip to content

Commit c112833

Browse files
committed
Added: Showing error for Custom Character Properties when the given character is empty
1 parent dead1d0 commit c112833

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

Editor/BitmapFontCreator.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ internal static class BitmapFontCreator
99
{
1010
private const char IgnoreCharacter = ' ';
1111

12-
public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
12+
public static bool TryCreateFont(ExecutionData data, bool warnBeforeOverwrite, out string error)
1313
{
14-
var error = CheckForErrors(data);
15-
if (!string.IsNullOrEmpty(error))
16-
{
17-
Debug.LogError(error);
18-
return;
19-
}
14+
error = CheckForErrors(data);
15+
if (!string.IsNullOrEmpty(error)) return false;
2016

2117
var path = AssetDatabase.GetAssetPath(data.Texture);
2218
var baseName = Path.GetFileNameWithoutExtension(path);
@@ -27,16 +23,18 @@ public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
2723
if (warnBeforeOverwrite && !(AssetDatabase.GUIDFromAssetPath(materialPath) == null && AssetDatabase.GUIDFromAssetPath(fontPath) == null))
2824
{
2925
if (!EditorUtility.DisplayDialog("Warning", "Asset already exists. Overwrite? (It will keep the references)", "Yes", "No"))
30-
return;
26+
return false;
3127
}
3228

3329
var material = CreateMaterial(baseName, data.Texture);
34-
var font = CreateFontAsset(baseName, material, data);
30+
var font = CreateFontAsset(baseName, material, data, out error);
31+
if (!string.IsNullOrEmpty(error)) return false;
3532

3633
AssetDatabase.CreateAsset(material, materialPath);
3734
CreateOrReplaceAsset(font, fontPath);
3835

3936
AssetDatabase.Refresh();
37+
return true;
4038
}
4139

4240
private static string CheckForErrors(ExecutionData data)
@@ -62,11 +60,20 @@ private static Material CreateMaterial(string baseName, Texture2D texture)
6260
};
6361
}
6462

65-
private static Font CreateFontAsset(string baseName, Material material, ExecutionData data)
63+
private static Font CreateFontAsset(string baseName, Material material, ExecutionData data, out string error)
6664
{
65+
error = null;
6766
var map = new Dictionary<char, CharacterProps>();
68-
foreach (var e in data.CustomCharacterProps)
67+
for (var i = 0; i < data.CustomCharacterProps.Count; i++)
68+
{
69+
var e = data.CustomCharacterProps[i];
70+
if (string.IsNullOrEmpty(e.Character))
71+
{
72+
error = $"Character for Custom Character Properties at position {i + 1} is empty";
73+
return null;
74+
}
6975
map.Add(e.Character[0], e);
76+
}
7077

7178
return new Font(baseName)
7279
{

Editor/Model/BitmapFontCreatorModel.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ private void UpdateChacters()
7474
}
7575

7676
public void OnBeforeSerialize() { }
77-
public void OnAfterDeserialize()
78-
{
79-
Debug.Log("Deserializing");
80-
UpdateChacters();
81-
}
77+
public void OnAfterDeserialize() { UpdateChacters(); }
8278
}
8379

8480
internal class ExecutionData : BitmapFontCreatorData

Editor/UI/BitmapFontCreatorEditor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class BitmapFontCreatorEditor : EditorWindow
1212
private ProfilesView _profilesView;
1313
private PrefsView _prefsView;
1414
private Settings _settings;
15+
private string _error;
1516

1617
private Vector2 _charactersScrollPos = Vector2.zero;
1718
private Vector2 _mainScrollPos = Vector2.zero;
@@ -89,6 +90,14 @@ private void OnGUI()
8990
GUILayout.FlexibleSpace();
9091

9192
DrawBottomMenu();
93+
94+
if (!string.IsNullOrEmpty(_error)) ShowCurrentError();
95+
}
96+
97+
private void ShowCurrentError()
98+
{
99+
Debug.LogError(_error);
100+
_error = null;
92101
}
93102

94103
private void DrawTextureField()
@@ -130,9 +139,8 @@ private void DrawCreateFontButton()
130139
GUILayout.BeginHorizontal();
131140
GUILayout.FlexibleSpace();
132141
GUI.color = Color.cyan;
133-
134142
if (GUILayout.Button(UI.CreateButton, Styles.CreateButton))
135-
BitmapFontCreator.TryCreateFont(_data, _prefsView.Model.WarnOnReplaceFont);
143+
BitmapFontCreator.TryCreateFont(_data, _prefsView.Model.WarnOnReplaceFont, out _error);
136144

137145
GUI.color = Color.white;
138146
GUILayout.FlexibleSpace();

0 commit comments

Comments
 (0)