本文主要是介绍优化版UGUI打图集代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先是代码的原版,雨松MOMO的博客
http://www.xuanyusong.com/archives/3304
雨松MOMO 2014年10月24日 于 雨松MOMO程序研究院 发表
代码很好用,但在使用过程中,遇到了一点问题。即便精灵图没发生改变,代码每次生成出来的Prefab文件都会发生细微的改变,提交版本库的时候,每次都要提交所有的Prefab,而且过时的Prefab没有被删除,于是自己在原代码的基础上加以改进,忽略了没有变化的Prefab,并删除了过时的Prefab,废话不多说,贴上代码。
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;public class AtlasMaker {[MenuItem("MyMenu/AtlasMaker")]static private void MakeAtlasTest(){string spriteDir = Application.dataPath + "/Resources/Sprite";DirectoryInfo prefabDir = new DirectoryInfo(spriteDir);Dictionary<string,string> prefabDict = new Dictionary<string, string>();Dictionary<string, string> prefabDictRemain = new Dictionary<string, string>();foreach (var fileInfo in prefabDir.GetFiles("*.prefab", SearchOption.AllDirectories)){prefabDict[fileInfo.Name.Replace(".prefab", "")] = fileInfo.FullName;prefabDictRemain[fileInfo.Name.Replace(".prefab", "")] = fileInfo.FullName;}foreach (var dir in prefabDir.GetDirectories()){foreach (var fileInfo in dir.GetFiles("*.prefab", SearchOption.AllDirectories)){prefabDict[fileInfo.Name.Replace(".prefab", "")] = fileInfo.FullName;prefabDictRemain[fileInfo.Name.Replace(".prefab", "")] = fileInfo.FullName;}}if (!Directory.Exists(spriteDir)){Directory.CreateDirectory(spriteDir);}DirectoryInfo rootDirInfo = new DirectoryInfo(Application.dataPath + "/Atlas");foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()){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);GameObject go;if (prefabDict.ContainsKey(sprite.name)){string path = prefabDict[sprite.name].Substring(prefabDict[sprite.name].IndexOf("Assets"));go = AssetDatabase.LoadAssetAtPath<GameObject>(path);prefabDictRemain.Remove(sprite.name);var spriteRed = go.GetComponent<SpriteRenderer>();if (spriteRed.sprite != sprite){spriteRed.sprite = sprite;}}else{go = new GameObject(sprite.name);go.AddComponent<SpriteRenderer>().sprite = sprite;allPath = spriteDir + "/" + sprite.name + ".prefab";string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));PrefabUtility.CreatePrefab(prefabPath, go);GameObject.DestroyImmediate(go);}}}foreach (var remain in prefabDictRemain){string path = remain.Value.Substring(remain.Value.IndexOf("Assets"));AssetDatabase.DeleteAsset(path);}AssetDatabase.SaveAssets();}
}
这篇关于优化版UGUI打图集代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!