using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.U2D;
using UnityEditor.U2D;
public class UITools
{
#region 刷新图集内容
[MenuItem("UGUI工具/刷新图集内容")]
static void AutoRefreshSpriteAtlas()
{
List<string> checkPath = new List<string>();
checkPath.Add("/AssetsPach/UIAltasOrginAssets");
string ignorePath = "UIAtlas/Common/Icon";
Debug.Log(Application.dataPath);
Dictionary<string, Dictionary<string, int>> markList = new Dictionary<string, Dictionary<string, int>>();
for (int i = 0; i < checkPath.Count; i++)
{
DirectoryInfo directoryInfo = new DirectoryInfo(string.Format("{0}{1}", Application.dataPath, checkPath[i]));
MarkDownImageInDoc(directoryInfo, ref markList, ignorePath);
bool ret = EditorUtility.DisplayCancelableProgressBar("信息", $"正在收集信息{checkPath[i]}", i * 1.0f / checkPath.Count);
if (ret == true)
{
Debug.Log("已取消处理");
EditorUtility.ClearProgressBar();
return;
}
}
List<Object> objects = new List<Object>();
int c = 1;
foreach (var item in markList)
{
if (item.Value.Count == 0)
continue;
objects.Clear();
SpriteAtlas spriteAtlas = GetOrCreateAtlas(item.Key);
//先移除现有的
spriteAtlas.Remove(spriteAtlas.GetPackables());
//再添加
foreach (var listItem in item.Value)
{
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(listItem.Key);
//if (!spriteAtlas.CanBindTo(sprite))
objects.Add(sprite);
}
spriteAtlas.Add(objects.ToArray());
bool ret = EditorUtility.DisplayCancelableProgressBar("信息", $"正在处理图集信息{spriteAtlas.name}", c * 1.0f / markList.Count);
c += 1;
if (ret == true)
{
Debug.Log("已取消处理");
EditorUtility.ClearProgressBar();
return;
}
}
objects.Clear();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
}
static SpriteAtlas GetOrCreateAtlas(string atlasName)
{
string filePath = string.Format("{0}/AssetsPach/WortAsset/SpriteAtlas/{1}.spriteatlas", Application.dataPath, atlasName);
string assetPath = string.Format("Assets/AssetsPach/WortAsset/SpriteAtlas/{0}.spriteatlas", atlasName);
SpriteAtlas atlas = null;
if (File.Exists(filePath))
{
atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(assetPath);
}
else
{
atlas = new SpriteAtlas();
// 设置参数 可根据项目具体情况进行设置
SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
{
blockOffset = 1,
enableRotation = false,
enableTightPacking = false,
padding = 4,
};
atlas.SetPackingSettings(packSetting);
SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
{
readable = false,
generateMipMaps = false,
sRGB = true,
filterMode = FilterMode.Bilinear,
};
atlas.SetTextureSettings(textureSetting);
TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
{
maxTextureSize = 2048,
format = TextureImporterFormat.Automatic,
crunchedCompression = true,
textureCompression = TextureImporterCompression.Compressed,
compressionQuality = 50,
};
atlas.SetPlatformSettings(platformSetting);
AssetDatabase.CreateAsset(atlas, assetPath);
}
return atlas;
}
static void MarkDownImageInDoc(DirectoryInfo docInfo, ref Dictionary<string, Dictionary<string, int>> markList, string ignorePath)
{
if (docInfo.FullName.Replace("\\", "/").Contains(ignorePath))
return;
foreach (var file in docInfo.GetFiles())
{
string fileName = file.Name;
string filePath = file.FullName.Replace("\\", "/");
filePath = filePath.Replace(Application.dataPath, "Assets/");
if (fileName.EndsWith(".png") || fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg"))
{
Dictionary<string, int> subList;
if (markList.TryGetValue(docInfo.Name, out subList))
{
if (subList.ContainsKey(fileName))
{
Debug.LogErrorFormat("图集有重复图片 :图集名称:{0} 图片名称:{1}", docInfo.Name, filePath);
}
else
{
subList.Add(filePath, 1);
}
}
else
{
subList = new Dictionary<string, int>();
subList.Add(filePath, 1);
markList.Add(docInfo.Name, subList);
}
}
}
foreach (var doc in docInfo.GetDirectories())
{
MarkDownImageInDoc(doc, ref markList, ignorePath);
}
}
#endregion
}
unity图集刷新工具
最新推荐文章于 2024-07-21 17:42:03 发布