@@ -7,7 +7,7 @@ namespace dev.klebersilva.tools.bitmapfontcreator
7
7
{
8
8
internal static class BitmapFontCreator
9
9
{
10
- public static void CreateFont ( ExecutionData data )
10
+ public static void TryCreateFont ( ExecutionData data , bool warnBeforeOverwrite )
11
11
{
12
12
var error = CheckForErrors ( data ) ;
13
13
if ( ! string . IsNullOrEmpty ( error ) )
@@ -16,24 +16,32 @@ public static void CreateFont(ExecutionData data)
16
16
return ;
17
17
}
18
18
19
- // TODO check if the asset already exists to warn the user
20
-
21
19
var path = AssetDatabase . GetAssetPath ( data . Texture ) ;
22
20
var baseName = Path . GetFileNameWithoutExtension ( path ) ;
21
+ path = path [ ..path . LastIndexOf ( "." ) ] ;
22
+ var materialPath = path + ".mat" ;
23
+ var fontPath = path + ".fontsettings" ;
24
+
25
+ if ( warnBeforeOverwrite && ! ( AssetDatabase . GUIDFromAssetPath ( materialPath ) == null && AssetDatabase . GUIDFromAssetPath ( fontPath ) == null ) )
26
+ {
27
+ if ( ! EditorUtility . DisplayDialog ( "Warning" , "Asset already exists. Overwrite? (It will keep the references)" , "Yes" , "No" ) )
28
+ return ;
29
+ }
23
30
24
31
var material = CreateMaterial ( baseName , data . Texture ) ;
25
32
var font = CreateFontAsset ( baseName , material , data ) ;
26
33
27
- path = path [ ..path . LastIndexOf ( "." ) ] ;
28
- AssetDatabase . CreateAsset ( material , path + ".mat" ) ;
29
- CreateOrReplaceAsset ( font , path + ".fontsettings" ) ;
34
+ AssetDatabase . CreateAsset ( material , materialPath ) ;
35
+ CreateOrReplaceAsset ( font , fontPath ) ;
30
36
31
37
AssetDatabase . Refresh ( ) ;
32
38
}
33
39
34
- // TODO all checks
35
40
private static string CheckForErrors ( ExecutionData data )
36
41
{
42
+ if ( data . Cols < 1 ) return "Cols must be greater than 0" ;
43
+ if ( data . Rows < 1 ) return "Rows must be greater than 0" ;
44
+
37
45
if ( data . Texture == null ) return "Texture cannot be null" ;
38
46
if ( ! data . Texture . isReadable ) return "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties" ;
39
47
@@ -72,7 +80,7 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
72
80
var cellUVSize = new Vector2 ( 1f / data . Cols , 1f / data . Rows ) ;
73
81
74
82
var characters = new List < CharacterInfo > ( ) ;
75
- int xMin , yMin , xMax , yMax , advance ;
83
+ int xMin , xMax , advance ;
76
84
int largestAdvance = 0 ;
77
85
78
86
// horizontal
@@ -87,13 +95,14 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
87
95
if ( ch == ' ' || ch == '\r ' || ch == '\n ' ) continue ;
88
96
89
97
GetCharacterBounds (
90
- data . Texture ,
91
- data . AlphaThreshold ,
92
- col * ( int ) cellSize . x ,
93
- ( data . Rows - row ) * ( int ) cellSize . y ,
94
- ( int ) cellSize . x ,
95
- ( int ) cellSize . y ,
96
- out xMin , out yMin , out xMax , out yMax
98
+ tex : data . Texture ,
99
+ alphaThreshold : data . AlphaThreshold ,
100
+ x0 : col * ( int ) cellSize . x ,
101
+ y0 : ( data . Rows - row ) * ( int ) cellSize . y ,
102
+ width : ( int ) cellSize . x ,
103
+ height : ( int ) cellSize . y ,
104
+ xMin : out xMin ,
105
+ xMax : out xMax
97
106
) ;
98
107
99
108
advance = xMax - xMin + data . DefaultCharacterSpacing ;
@@ -130,14 +139,13 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
130
139
return characters . ToArray ( ) ;
131
140
}
132
141
133
- // TODO maybe we can remove yMin and yMax
134
- private static void GetCharacterBounds ( Texture2D tex , float alphaThreshold , int x0 , int y0 , int width , int height ,
135
- out int xMin , out int yMin , out int xMax , out int yMax )
142
+ private static void GetCharacterBounds ( Texture2D tex , float alphaThreshold , int x0 , int y0 ,
143
+ int width , int height , out int xMin , out int xMax )
136
144
{
137
145
xMin = width ;
138
- yMin = height ;
139
146
xMax = 0 ;
140
- yMax = 0 ;
147
+ // yMin = height;
148
+ // yMax = 0;
141
149
142
150
int xx , yy ;
143
151
for ( var y = 0 ; y < height ; y ++ )
@@ -149,8 +157,8 @@ private static void GetCharacterBounds(Texture2D tex, float alphaThreshold, int
149
157
if ( tex . GetPixel ( xx , yy ) . a <= alphaThreshold ) continue ;
150
158
if ( x < xMin ) xMin = x ;
151
159
if ( x > xMax ) xMax = x ;
152
- if ( y < yMin ) yMin = y ;
153
- if ( y > yMax ) yMax = y ;
160
+ // if (y < yMin) yMin = y;
161
+ // if (y > yMax) yMax = y;
154
162
}
155
163
}
156
164
}
0 commit comments