前言:
项目需要,1.需提供给美术合图集工具,2.图集更新功能,使得新图集内的子图不必重新拖拽定位
解决思路:
1.unity调用外部TexturePacker命令行工具执行合图,unity根据TP生成的数据对图集切割出多个sprite
2.图集内的子图拖拽定位时,资源内产生对应的GUID及FileID。因此新图集更新对应的GUID与FileID即可,但是API没找到相应的接口,因此从资源本身下手通过txt方式打开,强制替换相应的ID
参考文档:
https://www.xuanyusong.com/archives/4207
效果:
源码:
下面三个文件,放置于工程Assets\Editor即可
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
using System.Text;
using System.Diagnostics;
using System.Linq;
using System.Xml;
using System.Collections;
using LuaFramework;
public class TexturePackerBuild : Editor
{
private const string BuildDir = "Assets/TexturePacker/打包该图集";
/// <summary>
/// TexturePacker.exe ###https://www.codeandweb.com/texturepacker/documentation/texture-settings
/// </summary>
private const string Paramet = " --sheet {0}.png --data {1}.xml --format sparrow --trim-mode CropKeepPos --pack-mode Best --algorithm MaxRects --max-size 4096 --size-constraints POT --disable-rotation {2}";
/// <summary>
/// 输出目录
/// </summary>
private const string OutPutDirRoot = "Assets/TPSpriteBuild/";
private static string tpDir = "";
private static string argument = "";
[MenuItem(BuildDir, false)]
public static void BuildOneTP()
{
if (Directory.Exists(OutPutDirRoot) == false)
{
Directory.CreateDirectory(OutPutDirRoot);
}
var assetPaths = Selection.assetGUIDs.Select(AssetDatabase.GUIDToAssetPath).ToList();
List<string> filePathList = new List<string>();
foreach (var assetPath in assetPaths)
{
if (AssetDatabase.IsValidFolder(assetPath))
{
string[] filesPath = Directory.GetFiles(assetPath);
foreach (var filePath in filesPath)
{
if (filePathList.Contains(filePath) == false)
{
filePathList.Add(filePath);
}
else
{
UnityEngine.Debug.LogErrorFormat("检测到相同文件 {0}", assetPath);
}
}
}
else
{
filePathList.Add(assetPath);
}
}
Dictionary<string, FileData> fileDic = new Dictionary<string, FileData>();
f