public void RebuildAtlas() { RefreshTextureLookup(); if (!TextureLookup.ContainsKey("transparent")) { RequireTransparentTexture(); } TextureImportHelper.SetTexturesReadable(Textures); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = $"{name}_atlas"; AtlasRects = supertexture.PackTextures(Textures.ToArray(), 2, 4096, false); var copyTexture = new Texture2D(supertexture.width, supertexture.height, TextureFormat.RGBA32, false); var pixels = supertexture.GetPixels(0, 0, supertexture.width, supertexture.height, 0); //Debug.Log(pixels.Length); copyTexture.SetPixels(pixels); copyTexture.name = supertexture.name; supertexture = copyTexture; supertexture.wrapMode = TextureWrapMode.Clamp; TextureImportHelper.PatchAtlasEdges(supertexture, AtlasRects); if (Atlas != null) { AssetDatabase.RemoveObjectFromAsset(Atlas); } var path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(this)); path = Path.Combine(path, "atlas").Replace("\\", "/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //File.WriteAllBytes(@"../atlas.png", Atlas.EncodeToPNG()); supertexture = TextureImportHelper.SaveAndUpdateTexture(supertexture, Path.Combine(path, supertexture.name + ".png")); //Debug.Log(path); // AssetDatabase.CreateAsset(supertexture, Path.Combine(path, supertexture.name + ".asset").Replace("\\", "/")); Atlas = supertexture; EditorUtility.SetDirty(this); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
public void LoadTextures(string savePath) { var path = Path.Combine("Assets/models/atlas", savePath); //Path.Combine(savePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var atlasName = $"{model.Name.Replace("\\", "_")}_atlas"; var atlasPath = Path.Combine(path, atlasName + ".png"); model.Textures = new List <string>(); var textures = new List <Texture2D>(); var texCount = br.ReadInt32(); for (var i = 0; i < texCount; i++) { var tName = br.ReadKoreanString(40); model.Textures.Add(tName); var texout = TextureImportHelper.GetOrImportTextureToProject(tName, RagnarokDirectory.GetRagnarokDataDirectory, "Assets/models"); textures.Add(texout); } TextureImportHelper.SetTexturesReadable(textures); var extratexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); textures.Add(extratexture); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = atlasName; atlasRects = supertexture.PackTextures(textures.ToArray(), 2, 4096, false); if (File.Exists(atlasPath)) { //we still needed to make the atlas to get the rects. We assume they're the same, but they might not be? GameObject.DestroyImmediate(supertexture); atlas = AssetDatabase.LoadAssetAtPath <Texture2D>(atlasPath); return; } TextureImportHelper.PatchAtlasEdges(supertexture, atlasRects); supertexture = TextureImportHelper.SaveAndUpdateTexture(supertexture, atlasPath); //var bytes = supertexture.EncodeToPNG(); //File.WriteAllBytes(atlasPath, bytes); //TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(atlasPath); //importer.textureType = TextureImporterType.Default; //importer.npotScale = TextureImporterNPOTScale.None; //importer.textureFormat = TextureImporterFormat.Automatic; //importer.textureCompression = TextureImporterCompression.CompressedHQ; //importer.wrapMode = TextureWrapMode.Clamp; //importer.isReadable = false; //importer.mipmapEnabled = false; //importer.alphaIsTransparency = true; //importer.maxTextureSize = 4096; //importer.SaveAndReimport(); //supertexture = AssetDatabase.LoadAssetAtPath<Texture2D>(atlasPath); //Get rid of mipmaps by copying the texture data. We have no need of mipmaps in our world. //var copyTexture = new Texture2D(supertexture.width, supertexture.height, TextureFormat.RGBA32, false); ////var pixels = supertexture.GetPixels(0, 0, supertexture.width, supertexture.height, 0); ////copyTexture.SetPixels(pixels); //copyTexture.name = supertexture.name; //supertexture = copyTexture; //supertexture.wrapMode = TextureWrapMode.Clamp; //AssetDatabase.CreateAsset(supertexture, atlasPath); atlas = supertexture; }
public void MakeAtlas(string path) { var atlasName = $"{baseName.Replace("\\", "_")}_atlas"; var atlasPath = Path.Combine(path, atlasName + ".png"); //if (!Directory.Exists(path)) // Directory.CreateDirectory(path); //if (!Directory.Exists("Assets/Effects/Sprites/" + baseName)) // Directory.CreateDirectory("Assets/Effects/Sprites/" + baseName); for (var i = 0; i < textureNames.Count; i++) { var texout = TextureImportHelper.GetOrImportTextureToProject(textureNames[i], basePath, "Assets/Effects/Textures/" + baseName); textures.Add(texout); } TextureImportHelper.SetTexturesReadable(textures); var extratexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); textures.Add(extratexture); var supertexture = new Texture2D(2, 2, TextureFormat.RGBA32, false); supertexture.name = atlasName; var atlasRects = supertexture.PackTextures(textures.ToArray(), 2, 4096, false); if (File.Exists(atlasPath)) { File.Delete(atlasPath); ////we still needed to make the atlas to get the rects. We assume they're the same, but they might not be? //GameObject.DestroyImmediate(supertexture); //atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlasPath); //return; } TextureImportHelper.PatchAtlasEdges(supertexture, atlasRects); anim.Atlas = supertexture; anim.AtlasRects = atlasRects; //atlas = TextureImportHelper.SaveAndUpdateTexture(supertexture, atlasPath); AssetDatabase.AddObjectToAsset(supertexture, anim); sprites = new List <Sprite>(textureNames.Count); for (var i = 0; i < textureNames.Count; i++) { var texrect = new Rect(atlasRects[i].x * supertexture.width, atlasRects[i].y * supertexture.height, atlasRects[i].width * supertexture.width, atlasRects[i].height * supertexture.height); var sprite = Sprite.Create(supertexture, texrect, new Vector2(0.5f, 0.5f), 50, 0, SpriteMeshType.FullRect); sprite.name = Path.GetFileNameWithoutExtension(textureNames[i]); AssetDatabase.AddObjectToAsset(sprite, anim); //AssetDatabase.CreateAsset(sprite, "Assets/Effects/Sprites/" + baseName + "/" + sprite.name); } AssetDatabase.SetMainObject(anim, Path.Combine("Assets/Effects/", baseName + ".asset")); AssetDatabase.SaveAssets(); }