/// <summary> ///用Atlas替换掉Mesh使用的Material的小图 /// </summary> /// <param name="meshDataList"></param> public static void ReplaceTextureUseAtlas(List <MeshData> meshDataList) { List <TextureAtlas> atlasAssetList = new List <TextureAtlas>(); string[] assetFileArray = Directory.GetFiles(AtlasConfig.AssetDir, "*.asset", SearchOption.AllDirectories); for (int i = 0; i < assetFileArray.Length; ++i) { TextureAtlas textureAtlasAsset = AssetDatabase.LoadAssetAtPath <TextureAtlas>(assetFileArray[i]); if (null != textureAtlasAsset) { atlasAssetList.Add(textureAtlasAsset); } } for (int i = 0; i < meshDataList.Count; ++i) { MeshData meshData = meshDataList[i]; if (null != meshData) { Texture2D texture = meshData.material.mainTexture as Texture2D; for (int j = 0; j < atlasAssetList.Count; ++j) { TextureAtlas textureAtlas = atlasAssetList[j]; if (null != textureAtlas) { TextureAtlasElement element = textureAtlas.GetElement(texture); if (null != element) { meshData.texData = new TexData() { Atlas = textureAtlas, Element = element }; break; } } } if (null == meshData.texData) { Debug.LogError("Texture not combine to atlas. " + texture.name); } } } }
/// <summary> /// 合并贴图 /// </summary> /// <param name="textureList"></param> /// <param name="atlasName"></param> /// <param name="isTransparent"></param> /// <param name="atlasWidth"></param> /// <param name="atlasHeight"></param> /// <returns></returns> private static List <TextureAtlas> PackAtlas(List <Texture2D> textureList, string atlasName, bool isTransparent, int atlasWidth, int atlasHeight) { int atlasIndex = 0; int textureCount = textureList.Count; textureList.Sort((a, b) => { return(a.width * a.height - b.width * b.height); }); List <TextureAtlas> atlasList = new List <TextureAtlas>(); string fileName = atlasName + "_" + atlasIndex; string assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); while (File.Exists(assetPath)) { TextureAtlas atlas = AssetDatabase.LoadAssetAtPath <TextureAtlas>(assetPath); if (null != atlas) { atlas.Layout(); atlasList.Add(atlas); } atlasIndex++; fileName = atlasName + "_" + atlasIndex; assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); } List <Texture2D> newlyTextures = new List <Texture2D>(); //找出新增的图片 for (int i = 0; i < textureList.Count; ++i) { bool found = false; for (int j = 0; j < atlasList.Count; ++j) { TextureAtlas atlas = atlasList[j]; if (null != atlas) { TextureAtlasElement element = atlas.GetElement(textureList[i]); if (null != element) { found = true; break; } } } if (!found) { newlyTextures.Add(textureList[i]); } } //Asset中删除图片已经删除的记录 for (int i = 0; i < atlasList.Count; ++i) { TextureAtlas atlas = atlasList[i]; for (int j = atlas.ElementList.Count - 1; j >= 0; --j) { Texture2D elementTex = atlas.ElementList[j].Tex; bool found = false; for (int m = 0; m < textureList.Count; ++m) { if (elementTex == textureList[m]) { found = true; break; } } if (!found) { atlas.RemoveElementAt(j); } } } //排列新增的图片 for (int i = 0; i < newlyTextures.Count; ++i) { Texture2D newlyTexture = newlyTextures[i]; if (null != newlyTexture) { bool added = false; for (int j = 0; j < atlasList.Count; ++j) { if (atlasList[j].AddTexture(newlyTexture)) { added = true; break; } } if (!added) { fileName = atlasName + "_" + atlasList.Count; assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); TextureAtlas atlas = ScriptableObject.CreateInstance <TextureAtlas>(); if (null != atlas) { atlas.Init(atlasWidth, atlasHeight, false, isTransparent, fileName); atlas.AddTexture(newlyTexture); atlasList.Add(atlas); } else { Debug.Log("Create atlas instance failed."); } } } } for (int i = 0; i < atlasList.Count; ++i) { EditorUtility.DisplayProgressBar("", "Pack atlas ", (i + 1) / atlasList.Count); atlasList[i].Pack(); } EditorUtility.ClearProgressBar(); return(atlasList); }