2023-04-03 11:04:31 +08:00

111 lines
2.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using BF;
using System;
namespace BFEditor.Resource
{
public static class LanguageAtlasTools
{
private static HashSet<String> waitHashSet = new HashSet<String>();
public static void UpdateLanguageAtlas()
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
Debug.Log("[bfinfo]更新多语言图集...");
UpdateAtlas(ResourceProcessConfig.LANGUAGE_PATH);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
watch.Stop();
Debug.Log("[bfinfo]UpdateLanguageAtlas耗时: " + BFEditorUtils.GetTimeStr(watch.ElapsedMilliseconds));
}
static void UpdateAtlas(string path)
{
path = path.Replace('\\', '/');
var dirs = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
for (var i = 0; i < dirs.Length; i++)
{
if (!dirs[i].Replace('\\', '/').Contains("/sprites/"))
{
continue;
}
UpdateAtlas(dirs[i]);
}
EditorUtility.DisplayProgressBar("提示", "正在更新 " + path, 1);
var sprites = GetSprites(path);
if (sprites.Count > 0)
{
var atlas = EnsureAtlas(path);
atlas.Clear();
// 排个序
sprites.Sort((x, y) => x.CompareTo(y));
for (int i = 0; i < sprites.Count; i++)
{
var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(sprites[i]);
atlas.AddSprite(sprite);
}
EditorUtility.SetDirty(atlas);
}
EditorUtility.ClearProgressBar();
}
static Atlas EnsureAtlas(string dirPath)
{
var index = dirPath.LastIndexOf("/");
var atlasName = dirPath.Substring(index + 1, dirPath.Length - index - 1);
var atlasPath = dirPath + "/" + atlasName + ".asset";
if (!File.Exists(atlasPath))
{
AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<Atlas>(), atlasPath);
AssetDatabase.SaveAssets();
}
return AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Atlas)) as Atlas;
}
static List<string> GetSprites(string dirPath)
{
var result = new List<string>();
var dirInfo = new DirectoryInfo(dirPath);
var files = dirInfo.GetFiles();
if (files.Length != 0)
{
foreach (var f in files)
{
if (".png".IndexOf(f.Extension.ToLower(), StringComparison.Ordinal) >= 0)
{
var resPath = "Assets" + f.FullName.Remove(0, Application.dataPath.Length);
result.Add(resPath);
}
}
}
return result;
}
public static void TryChangeAtlas(string spritePath)
{
var dirName = Path.GetDirectoryName(spritePath).Replace("\\", "/");
waitHashSet.TryAdd(dirName);
}
public static void CheckWaitList()
{
if(waitHashSet.Count <= 0)
{
return;
}
foreach(var dirName in waitHashSet)
{
UpdateAtlas(dirName);
}
waitHashSet.Clear();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}