此页面提供了使旧版Sprite Packer打包的现有资源能够自Unity 2020.1起在降级后继续工作的必要脚本,以下有一些限制
IPreprocessBuildWithReport
。使用以下脚本来让Unity打包被分配打包标记的纹理到图集。
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
class BuildPreProcessor : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report)
{
EditorSettings.spritePackerMode = SpritePackerMode.BuildTimeOnly;
UnityEditor.Sprites.Packer.RebuildAtlasCacheIfNeeded(BuildTarget.StandaloneWindows64, false);
}
}
注意:BuildPreProcessor
类继承自IPreprocessBuildWithReport
,并且仅在构建时触发。
使用以下转换脚本将现有的打包标记图集迁移到精灵图集系统中。
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.U2D;
using UnityEngine.U2D;
public class PrintAtlas : MonoBehaviour
{
[MenuItem("Atlas/ConvertTags")]
static void ConvertTags()
{
Dictionary<string, List<string>> TagSpriteMap = new Dictionary<string, List<string>>();
foreach (string s in AssetDatabase.GetAllAssetPaths())
{
TextureImporter ta = AssetImporter.GetAtPath(s) as TextureImporter;
if (ta != null)
{
if (ta.spritePackingTag != "")
{
if (!TagSpriteMap.ContainsKey(ta.spritePackingTag))
{
List<string> newList = new List<string>();
TagSpriteMap[ta.spritePackingTag] = newList;
}
TagSpriteMap[ta.spritePackingTag].Add(s);
}
}
}
AssetDatabase.CreateFolder("Assets", "SpriteAtlas");
foreach (var tagSprite in TagSpriteMap)
{
List<string> assetsforTag = tagSprite.Value;
string alasPath = "Assets/SpriteAtlas/" + tagSprite.Key + ".spriteatlas";
SpriteAtlas sa = new SpriteAtlas();
AssetDatabase.CreateAsset(sa, alasPath);
foreach (var asset in assetsforTag)
{
Debug.Log(tagSprite.Key + " = " + asset);
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(asset);
sa.Add(new Object[1] { tex } );
}
}
AssetDatabase.SaveAssets();
}
}