1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
| using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using Unity.EditorCoroutines.Editor; using UnityEditor; using UnityEngine;
namespace ToolEidtor { public class BitMapEditorSelfTool : EditorWindow { [Serializable] private class ImgInfo { public char mapChar; public Texture2D texture; public Vector2Int offset;
[NonSerialized] public Vector2Int size; [NonSerialized] public Vector2Int startPos; }
[MenuItem("Tool/BitMap/SelfTool")] private static void ShowAtlasEditorWindow() { var window = GetWindow<BitMapEditorSelfTool>(); window.autoRepaintOnSceneChange = true; window.Show(); }
private SerializedObject serObj; private SerializedProperty imgPro; private SerializedProperty imgInfoPro; [SerializeField] private List<ImgInfo> imgInfos; [SerializeField] private List<Texture2D> imgs;
private bool startExcute = false;
private Vector2 scrollPos; private float flagH = 0;
private string saveUrl; private string relateUrl; private string relateUrlHead;
private void OnEnable() { imgInfos = new List<ImgInfo>(); imgs = new List<Texture2D>(); serObj = new SerializedObject(this); imgInfoPro = serObj.FindProperty("imgInfos"); imgPro = serObj.FindProperty("imgs"); saveUrl = Application.dataPath; relateUrlHead = saveUrl.Substring(0, saveUrl.Length - 6); relateUrl = Path.GetRelativePath(relateUrlHead, saveUrl); }
private void OnGUI() { serObj.Update(); if (startExcute) { SetPropertyField(imgInfoPro, new GUIContent("图片信息"), flagH >= 300, 300, ref scrollPos, ref flagH); EditorGUILayout.LabelField("选择你要保存的位置,显示的是你当前项目的相对位置"); EditorGUILayout.BeginHorizontal(); EditorGUILayout.TextField(relateUrl); if(GUILayout.Button("...",GUILayout.MaxWidth(50))) { string path = EditorUtility.OpenFolderPanel("选择路径", relateUrlHead, "Assets"); if(!string.IsNullOrEmpty(path)) { if(path.Contains(relateUrlHead)) { relateUrl = Path.GetRelativePath(relateUrlHead, path); saveUrl = path; } else { EditorUtility.DisplayDialog("选择路径有错", "请选择你项目下的文件", "OK"); } } } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("一键生成")) { Dictionary<char, List<int>> repeatRecord = new Dictionary<char, List<int>>(); bool existRepeat = false; for(int i = 0;i < imgInfos.Count; ++i) { var imgInfo = imgInfos[i]; if (repeatRecord.ContainsKey(imgInfo.mapChar)) { existRepeat = true; repeatRecord[imgInfo.mapChar].Add(i); } else repeatRecord.Add(imgInfo.mapChar,new List<int>() { i}); } if(existRepeat) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("字符必须唯一!\n"); foreach (var i in repeatRecord.Keys) { var lst = repeatRecord[i]; if(lst.Count > 1) {
stringBuilder.Append(string.Format("字符串{0}在下标", i)); stringBuilder.Append(lst[0].ToString()); for (int j = 1;j < lst.Count; ++j) { stringBuilder.Append(','); stringBuilder.Append(lst[j].ToString()); } stringBuilder.Append("已经重复了\n"); } } EditorUtility.DisplayDialog("存在重复字符", stringBuilder.ToString(), "OK"); } else { EditorCoroutineUtility.StartCoroutineOwnerless(GenerateBitMap()); } } } else { var proFieldMaxH = 216 + EditorGUIUtility.singleLineHeight * 2; SetPropertyField(imgPro, new GUIContent("所需图片"), flagH >= proFieldMaxH, proFieldMaxH, ref scrollPos, ref flagH);
if (GUILayout.Button("开始")) { foreach (var img in imgs) { imgInfos.Add(new ImgInfo() { mapChar = img.name[0], texture = img }); } startExcute = true; scrollPos = Vector2.zero; }
} serObj.ApplyModifiedProperties(); }
private void SetPropertyField(SerializedProperty pro,GUIContent label,bool needScroll,float scrollHeight, ref Vector2 curScrollPos,ref float fieldHeight) { if(needScroll) { curScrollPos = EditorGUILayout.BeginScrollView(curScrollPos, GUILayout.MaxHeight(scrollHeight)); EditorGUILayout.PropertyField(pro, label, true); GetLastRectHeight(ref fieldHeight); EditorGUILayout.EndScrollView(); } else { curScrollPos = Vector2.zero; EditorGUILayout.PropertyField(pro, label, true); GetLastRectHeight(ref fieldHeight); } }
private void GetLastRectHeight(ref float curVal,float minVal = 18) { var rect = GUILayoutUtility.GetLastRect(); if(rect != null) { if (rect.size.y >= minVal) curVal = rect.size.y; } } private IEnumerator GenerateBitMap() { int progressCount = imgInfos.Count * 3; Vector2Int maxSingleSize = new Vector2Int(0,0); for(int i = 0;i < imgInfos.Count; ++i) { var curInfo = imgInfos[i]; var rectInfo = GetImgMinRectangle(curInfo.texture); curInfo.size = new Vector2Int((int)rectInfo.x, (int)rectInfo.y); curInfo.startPos = new Vector2Int((int)rectInfo.z, (int)rectInfo.w); maxSingleSize = Vector2Int.Max(maxSingleSize, curInfo.size); } var ratio = GetAlatWH(imgInfos.Count); int totalWidth = ratio.x * maxSingleSize.x; int totalHeight = ratio.y * maxSingleSize.y; int extraW = totalWidth % 4; int extraH = totalHeight % 4; if (extraW != 0) { totalWidth = (totalWidth / 4 + 1) * 4; extraW = 4 - extraW; } if (extraH != 0) { totalHeight = (totalHeight / 4 + 1) * 4; extraH = 4 - extraH; } Texture2D altas = new Texture2D(totalWidth, totalHeight); List<CharacterInfo> fntInfo = new List<CharacterInfo>(); for(int i = 0;i < imgInfos.Count; ++i) { var curInfo = imgInfos[i]; var curSp = curInfo.texture; int offsetX = (maxSingleSize.x - curInfo.size.x) / 2; int offsetY = (maxSingleSize.y - curInfo.size.y) / 2; int startX = i % ratio.x; int startY = ratio.y - i / ratio.x - 1; int minXRange = startX * maxSingleSize.x + offsetX; int maxXRange = minXRange + curInfo.size.x; int minYRange = startY * maxSingleSize.y + offsetY; int maxYRange = minYRange + curInfo.size.y; for (int j = startX * maxSingleSize.x; j < startX * maxSingleSize.x + maxSingleSize.x; ++ j) { for(int k = startY * maxSingleSize.y; k < startY * maxSingleSize.y + maxSingleSize.y; ++k) { if (j >= minXRange && j < maxXRange && k >= minYRange && k < maxYRange) { var col = curSp.GetPixel(j - minXRange + curInfo.startPos.x, k - minYRange + curInfo.startPos.y); altas.SetPixel(j, k, col); } else altas.SetPixel(j, k, new Color(1,1,1,0)); } } CharacterInfo info = new CharacterInfo(); Rect uv = new Rect(); uv.x = 1.0f * minXRange / totalWidth; uv.y = 1.0f * minYRange / totalHeight; uv.width = curInfo.size.x * 1.0f / totalWidth; uv.height = curInfo.size.y * 1.0f / totalHeight; info.index = curInfo.mapChar; info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin); info.uvBottomRight = new Vector2(uv.xMax, uv.yMin); info.uvTopLeft = new Vector2(uv.xMin, uv.yMax); info.uvTopRight = new Vector2(uv.xMax, uv.yMax); info.advance = curInfo.size.x; info.glyphWidth = curInfo.size.x; info.glyphHeight = curInfo.size.y; info.minX = curInfo.offset.x; info.maxX = curInfo.offset.x + curInfo.size.x; info.minY = -curInfo.offset.y - curInfo.size.y; info.maxY = -curInfo.offset.y; fntInfo.Add(info); } for(int i = 0;i < extraW; ++i) { for(int j = 0; j < totalHeight; ++j) altas.SetPixel(totalWidth - 1 - i, j, new Color(1, 1, 1, 0)); } for (int i = 0; i < extraH; ++i) { for (int j = 0; j < totalWidth; ++j) altas.SetPixel(j, totalHeight - 1 - i,new Color(1, 1, 1, 0)); }
altas.name = "BitMap"; string altasUrl = Path.Combine(saveUrl, "BitMap.png"); byte[] bytes = altas.EncodeToPNG(); if (!File.Exists(altasUrl)) { File.Create(altasUrl).Dispose(); } File.WriteAllBytesAsync(altasUrl, bytes);
string fontFileUrl = Path.Combine(saveUrl, "BitMapFont.fontsettings"); if (!File.Exists(fontFileUrl)) { AssetDatabase.CreateAsset(new Font(), Path.Combine(relateUrl, "BitMapFont.fontsettings")); AssetDatabase.Refresh(); yield return new EditorWaitForSeconds(0.2f); }
var font = AssetDatabase.LoadAssetAtPath<Font>(Path.Combine(relateUrl, "BitMapFont.fontsettings")); EditorUtility.SetDirty(font); font.characterInfo = fntInfo.ToArray();
SerializedObject serializedObject = new SerializedObject(font); serializedObject.FindProperty("m_LineSpacing").floatValue = maxSingleSize.y;
serializedObject.ApplyModifiedProperties(); serializedObject.UpdateIfRequiredOrScript(); serializedObject.SetIsDifferentCacheDirty();
AssetDatabase.Refresh();
var matTex = AssetDatabase.LoadAssetAtPath<Texture2D>(Path.Combine(relateUrl, "BitMap.png"));
if(File.Exists(Path.Combine(saveUrl, "BitMapMat.mat"))) { Material material = AssetDatabase.LoadAssetAtPath<Material>(Path.Combine(relateUrl, "BitMapMat.mat")); material.shader = Shader.Find("GUI/Text Shader"); material.SetTexture("_MainTex", matTex); font.material = material; } else { Material material = new Material(Shader.Find("GUI/Text Shader")); material.SetTexture("_MainTex", matTex); font.material = material; AssetDatabase.CreateAsset(material, Path.Combine(relateUrl, "BitMapMat.mat")); }
AssetDatabase.SaveAssetIfDirty(font);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("生成成功", "已经成功了,如果你使用有问题记得找一下这段代码的编写者", "OK"); yield break; }
private Vector4 GetImgMinRectangle(Texture2D tex) { Vector4 vector4 = new Vector4(tex.width, 0, 0, tex.height); for (int j = 0; j < tex.width; ++j) { if (j > vector4.x) break; for (int k = 0; k < tex.height; ++k) { var color = tex.GetPixel(j, k); if (color.a > 0) { vector4.x = Mathf.Min(vector4.x, j); break; } } } for (int j = tex.height - 1; j > -1; --j) { if (j < vector4.y) break; for (int k = 0; k < tex.width; ++k) { var color = tex.GetPixel(k, j); if (color.a > 0) { vector4.y = Mathf.Max(vector4.y, j); break; } } } for (int j = tex.width - 1; j > -1; --j) { if (j < vector4.z) break; for (int k = 0; k < tex.height; ++k) { var color = tex.GetPixel(j, k); if (color.a != 0) { vector4.z = Mathf.Max(vector4.z, j); break; } } } for (int j = 0; j < tex.height; ++j) { if (j > vector4.w) break; for (int k = 0; k < tex.width; ++k) { var color = tex.GetPixel(k, j); if (color.a != 0) { vector4.w = Mathf.Min(vector4.w, j); break; } } } Vector4 rectInfo = new Vector4(vector4.z - vector4.x, vector4.y - vector4.w, vector4.x, vector4.w); return rectInfo; }
private Vector2Int GetAlatWH(int frameCount) { int sq = Mathf.CeilToInt(Mathf.Sqrt(frameCount)); for (int i = sq; i > 0; --i) { if ((frameCount % i) == 0) { int val = Mathf.Max(i, frameCount / i); return new Vector2Int(val, frameCount / val); } } return Vector2Int.one; } } }
|