public static Uni2DAnimationClip CreateAnimationClip(List <Texture2D> a_rTexturesList, string a_rAnimationClipName = null, string a_rAnimationClipPath = null) { // Create a new animation clip prefab GameObject oModel = new GameObject( ); Uni2DAnimationClip oAnimationClipModel = oModel.AddComponent <Uni2DAnimationClip>( ); // Path to save prefab string oPrefabPath; if (a_rTexturesList != null && a_rTexturesList.Count > 0) { // Sort by name IOrderedEnumerable <Texture2D> rOrderedTexturesEnumerable = a_rTexturesList.OrderBy(x => x.name); Texture2D rFirstTexture = rOrderedTexturesEnumerable.First( ); // Create frames foreach (Texture2D rTexture in rOrderedTexturesEnumerable) { Uni2DAnimationFrame oAnimationFrame = new Uni2DAnimationFrame( ); oAnimationFrame.textureContainer = new Texture2DContainer(rTexture, true); oAnimationClipModel.frames.Add(oAnimationFrame); } // Apply oAnimationClipModel.ApplySettings(Uni2DAnimationClip.AnimationClipRegeneration.RegenerateAnimationClipOnly); oPrefabPath = (a_rAnimationClipPath == null ? Uni2DEditorUtils.GetLocalAssetFolderPath(rFirstTexture) : a_rAnimationClipPath) + (a_rAnimationClipName == null ? ("AnimationClip_" + rFirstTexture.name) : a_rAnimationClipName) + ".prefab"; // Make prefab path unique oPrefabPath = AssetDatabase.GenerateUniqueAssetPath(oPrefabPath); } else { // Unique prefab path string oClipName = (a_rAnimationClipName == null ? mc_oAnimationClipDefaultName : a_rAnimationClipName); oPrefabPath = (a_rAnimationClipPath == null ? Uni2DEditorUtils.GenerateNewPrefabLocalPath(oClipName) : AssetDatabase.GenerateUniqueAssetPath(a_rAnimationClipPath + oClipName)); } // Save it as a prefab GameObject oPrefab = PrefabUtility.CreatePrefab(oPrefabPath, oModel); // Destroy model GameObject.DestroyImmediate(oModel); // Return prefab animation clip component return(oPrefab.GetComponent <Uni2DAnimationClip>( )); }
// Generate atlas materials private Material GenerateAtlasMaterial(int a_iEntryIndex, Material a_rCurrentAtlasMaterial, Texture2D a_rAtlasTexture, string a_oGeneratedDataPathLocal) { bool bNewMaterial = false; Material rAtlasMaterial = a_rCurrentAtlasMaterial; // Create material if (a_rCurrentAtlasMaterial == null) { // Clone base material rAtlasMaterial = this.materialOverride != null ? new Material(this.materialOverride) : new Material(Shader.Find(Uni2DEditorSpriteBuilderUtils.mc_oSpriteDefaultShader)); bNewMaterial = true; } else { string oFolderPathLocal = Uni2DEditorUtils.GetLocalAssetFolderPath(a_rCurrentAtlasMaterial); if (oFolderPathLocal != a_oGeneratedDataPathLocal) { // Duplicate rAtlasMaterial = new Material(a_rCurrentAtlasMaterial); bNewMaterial = true; } } // If we have created a new material if (bNewMaterial) { rAtlasMaterial.name = gameObject.name + "_AtlasMaterial" + a_iEntryIndex; string oMaterialPathLocal = a_oGeneratedDataPathLocal + rAtlasMaterial.name + ".mat"; // Ensure the material can be created Material rMaterialAtWantedPath = AssetDatabase.LoadAssetAtPath(oMaterialPathLocal, typeof(Texture2D)) as Material; if (rMaterialAtWantedPath != null) { // Todo_Sev : ask user before deletion? AssetDatabase.DeleteAsset(oMaterialPathLocal); } // Create material AssetDatabase.CreateAsset(rAtlasMaterial, oMaterialPathLocal); } // Assign the atlas texture rAtlasMaterial.mainTexture = a_rAtlasTexture; return(rAtlasMaterial); }
// Generate public bool Generate( ) { // Make sure the data directory exist string oGeneratedDataPathLocal = Uni2DEditorUtils.GetLocalAssetFolderPath(gameObject) + gameObject.name + "_GeneratedData" + "/"; string oGeneratedDataPathGlobal = Uni2DEditorUtils.LocalToGlobalAssetPath(oGeneratedDataPathLocal); if (!Directory.Exists(oGeneratedDataPathGlobal)) { Directory.CreateDirectory(oGeneratedDataPathGlobal); } // Generate data! bool bSuccess = GenerateAtlasData(oGeneratedDataPathLocal); generationId = System.Guid.NewGuid( ).ToString( ); EditorUtility.SetDirty(this); AssetDatabase.SaveAssets( ); return(bSuccess); }
private Texture2D ExportAndSaveAtlasTexture(int a_iAtlasEntryIndex, Texture2D a_rCurrentAtlasTexture, Texture2D a_rNewAtlasTexture, string a_oGeneratedDataPathLocal) { bool bNewTexture = false; // Look if there's already a texture at desired path if (a_rCurrentAtlasTexture == null) { // No => create the texture bNewTexture = true; } else { string oFolderPathLocal = Uni2DEditorUtils.GetLocalAssetFolderPath(a_rCurrentAtlasTexture); if (oFolderPathLocal != a_oGeneratedDataPathLocal) { bNewTexture = true; } } // Set atlas name accordingly if (bNewTexture) { a_rNewAtlasTexture.name = gameObject.name + "_AtlasTexture" + a_iAtlasEntryIndex; } else { a_rNewAtlasTexture.name = a_rCurrentAtlasTexture.name; } // Get the atlas texture path string oAtlasTexturePathLocal = a_oGeneratedDataPathLocal + a_rNewAtlasTexture.name + ".png"; string oAtlasTexturePathGlobal = Uni2DEditorUtils.LocalToGlobalAssetPath(oAtlasTexturePathLocal); // Save the atlas FileStream oFileStream = new FileStream(oAtlasTexturePathGlobal, FileMode.Create); BinaryWriter oBinaryWriter = new BinaryWriter(oFileStream); oBinaryWriter.Write(a_rNewAtlasTexture.EncodeToPNG( )); // Close IO resources oBinaryWriter.Close( ); oFileStream.Close( ); // If we had just created a new texture set the default import settings if (bNewTexture) { ImportNewAtlasTexture(oAtlasTexturePathLocal); } else { // or reimport AssetDatabase.ImportAsset(oAtlasTexturePathLocal, ImportAssetOptions.ForceUpdate); } // Destroy the runtime-created instance DestroyImmediate(a_rNewAtlasTexture); // Save a ref to new atlas texture (by instancing its Unity serialized model) a_rNewAtlasTexture = AssetDatabase.LoadAssetAtPath(oAtlasTexturePathLocal, typeof(Texture2D)) as Texture2D; // Mark the atlas texture Uni2DEditorUtils.MarkAsTextureAtlas(a_rNewAtlasTexture); return(a_rNewAtlasTexture); }