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);
    }