96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : HJ
|
|
// Created : 2019-07-25
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "AutoCreateAtlas" company="DefaultCompany"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEditor.U2D;
|
|
using UnityEngine.U2D;
|
|
|
|
public class AutoCreateAtlas : Editor
|
|
{
|
|
private const string ATLAS_PATH = "Assets/Ryff/Arts/Textures/Atlas";
|
|
private const string TEXTURE_PATH = "Assets/Ryff/Arts/Textures/UI";
|
|
|
|
[MenuItem("Game/图集处理")]
|
|
public static void CreateAtlas()
|
|
{
|
|
var rootDirInfo = new DirectoryInfo(TEXTURE_PATH);
|
|
var spts = new List<Sprite>();
|
|
|
|
foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories())
|
|
{
|
|
//spts.Clear();
|
|
//foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
|
|
//{
|
|
// string allPath = pngFile.FullName;
|
|
// string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
|
|
// Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
|
|
// if (IsPackable(sprite) && !ContainChinese(sprite.name))
|
|
// spts.Add(sprite);
|
|
//}
|
|
string atlasName = dirInfo.Name + ".spriteatlas";
|
|
var atlasPath = CreateAtlas(string.Format("{0}/{1}", ATLAS_PATH, atlasName));
|
|
var sptAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(atlasPath);
|
|
Debug.Log(sptAtlas.tag);
|
|
var obj = AssetDatabase.LoadAssetAtPath<Object>(string.Format("{0}/{1}",TEXTURE_PATH, dirInfo.Name));
|
|
var fileinfo = Directory.GetFiles(string.Format("{0}/{1}", TEXTURE_PATH, dirInfo.Name));
|
|
if (sptAtlas.GetPackables().Length == 0)
|
|
{
|
|
sptAtlas.Add(new Object[] { obj });
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in sptAtlas.GetPackables())
|
|
{
|
|
if (obj.name != item.name)
|
|
{
|
|
sptAtlas.Add(new Object[] { obj });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
SpriteAtlasUtility.PackAllAtlases(BuildTarget.StandaloneWindows);
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
|
|
|
|
private static bool IsPackable(Object o)
|
|
{
|
|
return o != null && (o.GetType() == typeof(Sprite) || o.GetType() == typeof(Texture2D) || (o.GetType() == typeof(DefaultAsset) && ProjectWindowUtil.IsFolder(o.GetInstanceID())));
|
|
}
|
|
|
|
private static string CreateAtlas(string atlasPath)
|
|
{
|
|
if (!File.Exists(atlasPath))
|
|
{
|
|
if (!Directory.Exists(ATLAS_PATH))
|
|
{
|
|
Directory.CreateDirectory(ATLAS_PATH);
|
|
}
|
|
var tempAtlas = new SpriteAtlas();
|
|
AssetDatabase.CreateAsset(tempAtlas, atlasPath);
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
return atlasPath;
|
|
}
|
|
|
|
static bool ContainChinese(string input)
|
|
{
|
|
string pattern = "[\u4e00-\u9fbb]";
|
|
return Regex.IsMatch(input, pattern);
|
|
}
|
|
|
|
}
|