|
Unity脚本自动打包图集
思路
1.打开所有的场景
2.在每个场景里寻找SpriteRenderer,获取每个SpriteRenderer.sprite的地址,引用计数+1,如果计数>1,放入public图集
3.然后生成各个场景中的图集
<hr>
代码using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading;
using ET;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.U2D;
using UnityEngine.UI;
namespace DJ
{
public class GenerateSpirteAtlas:Editor
{
private static string scenePath ="Assets/Bundles/Map/";
private static string sceneResPath ="Assets/Res/Environment/Map/";
private const int maxSize =2048;
private static List publicSceneSprites;
private static Dictionary> mapDict;
[MenuItem("Tools/Map/从文件夹生成图集",false,1)]
public static void GenerateAllSceneSpirteAtlasByFoder()
{
var direcorys =Directory.GetDirectories(sceneResPath);
foreach (var pathin direcorys)
{
GenerateAllSceneSpirteAtlasByFolder(path);
}
AssetDatabase.Refresh();
Debug.Log("完成~");
}
///
/// 生成folder的所有精灵图
///
///
[MenuItem("Tools/Map/从场景生成图集",false,2)]
public static void GenerateAllSceneSpirteAtlasByScene()
{
mapDict =new Dictionary>();
publicSceneSprites =new List();
var scenePathList =new List();
FileHelper.GetAllFiles(scenePathList,scenePath,"*.unity");
var defalutScenePath =SceneManager.GetActiveScene().path;
foreach (var scenePathin scenePathList)
{
var scene=EditorSceneManager.OpenScene(scenePath);
var scenename = scene.name;
var mySceneDict =new Dictionary();
var sprites = FindObjectsOfType(true);
foreach (var spritein sprites)
{
if (sprite.sprite ==null)
{
continue;
}
var path =AssetDatabase.GetAssetPath(sprite.sprite.texture);
//查找其他有没有引用
var otherSceneDict = GetAssetDictionary(path);
if (otherSceneDict !=null)
{
//计数++
otherSceneDict[path]++;
continue;
}
//查看本场景有没有添加过
if (!mySceneDict.ContainsKey(path))
{
mySceneDict.Add(path,1);
}
// Debug.Log($"-------------资源路径:{path}");
}
mapDict.Add(scenename,mySceneDict);
}
//所有场景资源都引用到了去重剔除共用部分
foreach (var mapAssetDictin mapDict)
{
var folderPath = @$"{scenePath}{mapAssetDict.Key}/";
var path = folderPath +$"{mapAssetDict.Key}spriteatlas.spriteatlas";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var texture2ds =new List();
foreach (var assetDictin mapAssetDict.Value)
{
var assetPath = assetDict.Key;
var referenceNum = assetDict.Value;
if (referenceNum >1)
{
publicSceneSprites.Add(assetPath);
continue;
}
var texture2d =AssetDatabase.LoadAssetAtPath(assetPath);
texture2ds.Add(texture2d);
}
if (texture2ds.Count >0)
{
var spriteAtlas= CreateSpritAtlas(path);
spriteAtlas.Add(texture2ds.ToArray());
}
//是否超过最大尺寸
}
//生成共用的精灵图集
if (publicSceneSprites.Count >0)
{
var publicTexture2d =new List();
var publicPath = scenePath +"publicMap/";
if (!Directory.Exists(publicPath))
{
Directory.CreateDirectory(publicPath);
}
var pubSpirtePath = publicPath+"publicMap.spriteatlas";
var publicSprites = CreateSpritAtlas(pubSpirtePath);
foreach (var pathin publicSceneSprites)
{
var texture2d =AssetDatabase.LoadAssetAtPath(path);
publicTexture2d.Add(texture2d);
}
publicSprites.Add(publicTexture2d.ToArray());
}
EditorSceneManager.OpenScene(defalutScenePath);
AssetDatabase.Refresh();
Debug.Log("完成所有场景的精灵图更新");
}
private static Dictionary GetAssetDictionary(string path)
{
foreach (var mapAssetDictin mapDict.Values)
{
if (mapAssetDict.ContainsKey(path))
{
return mapAssetDict;
}
}
return null;
}
///
/// 生成folder的所有精灵图
///
///
public static void GenerateAllSceneSpirteAtlasByFolder(string folderPath)
{
var spriteFileList =new List();
var names = folderPath.SplitToArray('/');
var name = names.Last();
FileHelper.GetAllFiles(spriteFileList, folderPath,"*.png");
var path = folderPath +$"/{name}.spriteatlas";
var spriteAtlas = CreateSpritAtlas(path);
var bigSprite =new List();
var nomalSpirte =new List();
foreach (var spritePathin spriteFileList)
{
var sprite =AssetDatabase.LoadAssetAtPath(spritePath);
var bytes= sprite.GetRawTextureDat();
// var file =new FileStream()
var width = sprite.width;
var height =sprite.height;
if (width>=750||height>=750)
{
bigSprite.Add(sprite);
continue;
}
nomalSpirte.Add(sprite);
}
spriteAtlas.Add(nomalSpirte.ToArray());
}
public static SpriteAtlas CreateSpritAtlas(string path)
{
SpriteAtlas spriteAtlas =AssetDatabase.LoadAssetAtPath(path);
if (spriteAtlas ==null)
{
spriteAtlas =new SpriteAtlas();
AssetDatabase.CreateAsset(spriteAtlas, path);
}
else
{
spriteAtlas.Remove(spriteAtlas.GetPackables());
}
var atlasSetting = spriteAtlas.GetPlatformSettings(GetPlatformName(EditorUserBuildSettings.activeBuildTarget));
atlasSetting.maxTextureSize = maxSize;
atlasSetting.textureCompression =TextureImporterCompression.CompressedHQ;
atlasSetting.format =TextureImporterFormat.RGBA32;
spriteAtlas.SetPlatformSettings(atlasSetting);
return spriteAtlas;
}
///
/// 获取打包平台
///
///
///
public static string GetPlatformName(BuildTarget target)
{
string platformName ="";
switch (target)
{
case BuildTarget.Android:
platformName ="Android";
break;
case BuildTarget.iOS:
platformName ="iPhone";
break;
case BuildTarget.PS4:
platformName ="PS4";
break;
case BuildTarget.XboxOne:
platformName ="XboxOne";
break;
case BuildTarget.NoTarget:
platformName ="DefaultTexturePlatform";
break;
default:
platformName ="Standalone";
break;
}
return platformName;
}
}
} |
|