Example #1
0
    // Copy clips
    private void CopyFrames(List <Uni2DAnimationFrame> a_rFramesSource, List <Uni2DAnimationFrame> a_rFramesDestination)
    {
        // Update asset table
        Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;
        string rClipGUID = Uni2DEditorUtils.GetUnityAssetGUID(this);

        foreach (Uni2DAnimationFrame rOldFrame in a_rFramesDestination)
        {
            string oTextureGUID = rOldFrame.textureContainer != null ? rOldFrame.textureContainer.GUID : null;
            if (!string.IsNullOrEmpty(oTextureGUID))
            {
                rAssetTable.RemoveClipUsingTexture(rClipGUID, oTextureGUID);
            }
        }

        a_rFramesDestination.Clear( );
        foreach (Uni2DAnimationFrame rFrameSource in a_rFramesSource)
        {
            a_rFramesDestination.Add(new Uni2DAnimationFrame(rFrameSource));

            string oTextureGUID = rFrameSource.textureContainer != null ? rFrameSource.textureContainer.GUID : null;
            if (!string.IsNullOrEmpty(oTextureGUID))
            {
                rAssetTable.AddClipUsingTexture(rClipGUID, oTextureGUID);
            }
        }

        rAssetTable.Save( );
    }
    // Fins the first texture atlas
    public static Uni2DTextureAtlas FindFirstTextureAtlas(string a_rTextureGUID)
    {
        Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;
        Uni2DTextureAtlas     rAtlas      = null;
        bool bSaveTable = false;

        string[] rAtlasGUIDs = rAssetTable.GetAtlasGUIDsUsingThisTexture(a_rTextureGUID);

        foreach (string rAtlasGUID in rAtlasGUIDs)
        {
            rAtlas = Uni2DEditorUtils.GetAssetFromUnityGUID <Uni2DTextureAtlas>(rAtlasGUID);

            if (rAtlas != null && rAtlas.Contains(a_rTextureGUID))
            {
                //rAtlas = null;
                break;
            }
            else
            {
                rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, a_rTextureGUID);
                bSaveTable = true;
            }
        }

        if (bSaveTable)
        {
            rAssetTable.Save( );
        }

        return(rAtlas);
    }
    // Apply settings
    public bool ApplySettings(bool a_bUpdateSprites = true)
    {
        bool bSuccess;

        Uni2DAssetPostprocessor.Enabled = false;
        {
            int         iContainerCount = textures.Length;
            Texture2D[] oTexturesToPack = new Texture2D[iContainerCount];
            for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex)
            {
                oTexturesToPack[iContainerIndex] = textures[iContainerIndex].Texture;
            }

            List <Uni2DTextureImporterSettingsPair> rTextureImporterSettings = Uni2DEditorSpriteBuilderUtils.TexturesProcessingBegin(oTexturesToPack);
            oTexturesToPack = null;

            // Look if the atlas is set properly regarding to texture sizes
            int iOversizedTextures = this.LookForOversizedTextures(textures, padding, maximumAtlasSize);
            if (iOversizedTextures == 0)
            {
                textures        = this.SanitizeInputTextures(textures);
                iContainerCount = textures.Length;

                string rAtlasGUID = Uni2DEditorUtils.GetUnityAssetGUID(this);
                Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;

                for (int iTextureIndex = 0, iTextureCount = m_rTextureContainers.Length; iTextureIndex < iTextureCount; ++iTextureIndex)
                {
                    rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, m_rTextureContainers[iTextureIndex].GUID);
                }

                m_rTextureContainers = new Texture2DContainer[iContainerCount];

                // Deep copy
                for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex)
                {
                    Texture2DContainer oTextureContainer = new Texture2DContainer(textures[iContainerIndex]);
                    m_rTextureContainers[iContainerIndex] = oTextureContainer;
                    rAssetTable.AddAtlasUsingTexture(rAtlasGUID, oTextureContainer.GUID);
                }

                rAssetTable.Save( );

                m_iPadding          = padding;
                m_eMaximumAtlasSize = maximumAtlasSize;

                bSuccess = Generate( );

                if (a_bUpdateSprites)
                {
                    Uni2DEditorSpriteBuilderUtils.UpdateSpriteInCurrentSceneAndResourcesAccordinglyToAtlasChange(this);
                }
            }
            else             // Some textures can't fit
            {
                bSuccess = false;
                Debug.LogWarning("Uni2D could not regenerate atlas '" + (this.gameObject.name) + "' properly: "
                                 + iOversizedTextures + " texture" + (iOversizedTextures > 1 ? "s are" : " is")
                                 + " too large to fit in the atlas.", this.gameObject);
            }

            Uni2DEditorSpriteBuilderUtils.TexturesProcessingEnd(rTextureImporterSettings);
        }
        Uni2DAssetPostprocessor.Enabled = true;

        EditorUtility.UnloadUnusedAssets( );

        return(bSuccess);
    }
    private static void OnPostprocessAllAssets(string[] a_rImportedAssets, string[] a_rDeletedAssets, string[] a_rMovedAssets, string[] a_rMovedFromPath)
    {
        if (ms_bEnabled)
        {
            bool bUpdateAssets       = false;
            bool bPostprocessPrefabs = false;
            bool bSaveTable          = false;

            Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;

            foreach (string rImportedAssetPath in a_rImportedAssets)
            {
                // Check if the asset isn't re-processed due to a call to GenerateTextureImportGUID
                if (ms_oImportedTexturesWithUpdatedUni2DGUIDs.Contains(rImportedAssetPath) == false)
                {
                    Texture2D rImportedTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Texture2D));

                    if (rImportedTexture != null)
                    {
                        //Debug.Log ( "Imported " + rImportedAssetPath );

                        string rImportedTextureGUID = AssetDatabase.AssetPathToGUID(rImportedAssetPath);
                        ms_oImportedTextureGUIDs.Add(rImportedTextureGUID);

                        if (Uni2DEditorUtils.ItIsTheFirstTimeWeUseTheTexture(rImportedTexture))
                        {
                            Uni2DEditorSpriteBuilderUtils.SetDefaultTextureImporterSettings(rImportedTexture, false);
                        }

                        // This call will produce an unvoidable import of the texture (didn't find out how to prevent it)
                        Uni2DEditorUtils.GenerateTextureImportGUID(rImportedTexture);

                        // ... so, save the path reference to avoid to re-process it again
                        ms_oImportedTexturesWithUpdatedUni2DGUIDs.Add(rImportedAssetPath);

                        bUpdateAssets = true;

                        rImportedTexture = null;
                        EditorUtility.UnloadUnusedAssets( );
                        continue;
                    }

                    Uni2DTextureAtlas rImportedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DTextureAtlas));
                    if (rImportedAtlas != null)
                    {
                        //Debug.Log ( "Imported atlas " + rImportedAssetPath );

                        bSaveTable = true;

                        rAssetTable.AddAtlasPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                        rImportedAtlas = null;
                        EditorUtility.UnloadUnusedAssets( );
                        continue;
                    }

                    Uni2DAnimationClip rImportedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DAnimationClip));
                    if (rImportedClip != null)
                    {
                        //Debug.Log ( "Imported clip " + rImportedClip );

                        bSaveTable = true;

                        rAssetTable.AddClipPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                        rImportedClip = null;
                        EditorUtility.UnloadUnusedAssets( );
                        continue;
                    }

                    GameObject rImportedGameObject = (GameObject)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(GameObject));
                    if (rImportedGameObject != null)
                    {
                        //Debug.Log ( "Imported game object " + rImportedAssetPath );
                        ms_oGameObjectGUIDsToPostProcess.Add(AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                        bPostprocessPrefabs = true;

                        rImportedGameObject = null;
                        EditorUtility.UnloadUnusedAssets( );
                    }
                }
                else
                {
                    // ... remove the asset path from our list if it was already processed
                    ms_oImportedTexturesWithUpdatedUni2DGUIDs.Remove(rImportedAssetPath);
                }
            }

            // Moved assets
            for (int iIndex = 0, iCount = a_rMovedAssets.Length; iIndex < iCount; ++iIndex)
            {
                //Debug.Log ( "Importing moved asset" );
                Uni2DTextureAtlas rMovedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DTextureAtlas));
                if (rMovedAtlas != null)
                {
                    rAssetTable.RemoveAtlasFromPath(a_rMovedFromPath[iIndex], false);
                    rAssetTable.AddAtlasPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex]));

                    bSaveTable = true;

                    rMovedAtlas = null;
                    EditorUtility.UnloadUnusedAssets( );
                    continue;
                }

                Uni2DAnimationClip rMovedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DAnimationClip));
                if (rMovedClip != null)
                {
                    rAssetTable.RemoveClipFromPath(a_rMovedFromPath[iIndex], false);
                    rAssetTable.AddClipPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex]));

                    bSaveTable = true;

                    rMovedClip = null;
                    EditorUtility.UnloadUnusedAssets( );
                }
            }

            // Deleted assets
            foreach (string rDeletedAsset in a_rDeletedAssets)
            {
                string[] rSpritePrefabGUIDs = rAssetTable.GetSpritePrefabGUIDsUsingThisAtlasPath(rDeletedAsset);

                if (rSpritePrefabGUIDs.Length > 0)
                {
                    bUpdateAssets = true;
                    ms_oSpritePrefabGUIDsToUpdate.UnionWith(rSpritePrefabGUIDs);
                }

                /*
                 * // TODO: mettre des paths au lieu d'IDs
                 * string[ ] rClipGUIDs = rAssetTable.GetClipGUIDsUsingThisTexturePath( rDeletedAsset );
                 * if( rClipGUIDs.Length > 0 )
                 * {
                 *      bUpdateAssets = true;
                 *      ms_oAnimationClipGUIDsToUpdate.UnionWith( rClipGUIDs );
                 * }
                 */

                bSaveTable = rAssetTable.RemoveAtlasFromPath(rDeletedAsset, true) || bSaveTable;
                bSaveTable = rAssetTable.RemoveClipFromPath(rDeletedAsset, true) || bSaveTable;
            }

            if (bSaveTable)
            {
                rAssetTable.Save( );
            }

            if (bUpdateAssets)
            {
                ms_oAtlasGUIDsToUpdate.UnionWith(rAssetTable.GetAtlasGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));
                ms_oAnimationClipGUIDsToUpdate.UnionWith(rAssetTable.GetClipGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));

                ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));
                ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseAtlases(ms_oAtlasGUIDsToUpdate));

                EditorApplication.delayCall += UpdateUni2DAssets;
            }

            if (bPostprocessPrefabs)
            {
                EditorApplication.delayCall += OnSpritePrefabPostprocess;
            }
        }
    }
    private static void UpdateUni2DAssets( )
    {
        EditorApplication.delayCall -= UpdateUni2DAssets;

        Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;

        try
        {
            Uni2DAssetPostprocessor.LockTo(false);

            // Update animation clips first, because they can change the atlases
            foreach (string rAnimationClipGUID in ms_oAnimationClipGUIDsToUpdate)
            {
                Uni2DAnimationClip rAnimationClip = Uni2DEditorUtils.GetAssetFromUnityGUID <Uni2DAnimationClip>(rAnimationClipGUID);
                if (rAnimationClip != null)
                {
                    //Debug.Log ( "Updating clip " + rAnimationClipGUID );
                    rAnimationClip.OnTexturesChange(ms_oImportedTextureGUIDs);

                    rAnimationClip = null;
                    EditorUtility.UnloadUnusedAssets( );
                }
                else
                {
                    // Clean asset table
                    foreach (string rTextureGUID in ms_oImportedTextureGUIDs)
                    {
                        rAssetTable.RemoveClipUsingTexture(rAnimationClipGUID, rTextureGUID);
                    }
                }
            }

            foreach (string rAtlasGUID in ms_oAtlasGUIDsToUpdate)
            {
                Uni2DTextureAtlas rAtlas = Uni2DEditorUtils.GetAssetFromUnityGUID <Uni2DTextureAtlas>(rAtlasGUID);

                if (rAtlas != null)
                {
                    //Debug.Log( "Updating atlas " + rAtlasGUID );
                    rAtlas.OnTextureChange( );

                    rAtlas = null;
                    EditorUtility.UnloadUnusedAssets( );
                }
                else
                {
                    // Clean
                    foreach (string rTextureGUID in ms_oImportedTextureGUIDs)
                    {
                        rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, rTextureGUID);
                    }
                }
            }

            foreach (string rSpritePrefabGUID in ms_oSpritePrefabGUIDsToUpdate)
            {
                GameObject rSpritePrefab = Uni2DEditorUtils.GetAssetFromUnityGUID <GameObject>(rSpritePrefabGUID);

                if (rSpritePrefab != null)
                {
                    //Debug.Log( "Updating sprite prefab " + rSpritePrefabGUID );
                    foreach (Uni2DSprite rSpritePrefabComponent in rSpritePrefab.GetComponentsInChildren <Uni2DSprite>(true))
                    {
                        Uni2DEditorSpriteSettings rSpriteSettings = rSpritePrefabComponent.SpriteSettings;
                        string rSpriteTextureGUID = rSpriteSettings.textureContainer.GUID;
                        string rSpriteAtlasGUID   = rSpriteSettings.atlas != null?Uni2DEditorUtils.GetUnityAssetGUID(rSpriteSettings.atlas) : null;

                        if (ms_oImportedTextureGUIDs.Contains(rSpriteTextureGUID) || (!string.IsNullOrEmpty(rSpriteAtlasGUID) && ms_oAtlasGUIDsToUpdate.Contains(rSpriteAtlasGUID)))
                        {
                            rSpritePrefabComponent.Regenerate(true);
                        }

                        EditorUtility.UnloadUnusedAssets( );
                    }

                    rSpritePrefab = null;
                    EditorUtility.UnloadUnusedAssets( );
                }
                else
                {
                    // Clean
                    foreach (string rTextureGUID in ms_oImportedTextureGUIDs)
                    {
                        rAssetTable.RemoveSpritePrefabUsingTexture(rSpritePrefabGUID, rTextureGUID);
                    }

                    foreach (string rAtlasGUID in ms_oAtlasGUIDsToUpdate)
                    {
                        rAssetTable.RemoveSpritePrefabUsingAtlas(rSpritePrefabGUID, rAtlasGUID);
                    }
                }
            }
        }
        finally
        {
            ms_oImportedTextureGUIDs.Clear( );

            ms_oAtlasGUIDsToUpdate.Clear( );
            ms_oAnimationClipGUIDsToUpdate.Clear( );
            ms_oSpritePrefabGUIDsToUpdate.Clear( );

            rAssetTable.Save( );

            Uni2DAssetPostprocessor.Unlock( );
            Uni2DAssetPostprocessor.Enabled = true;
        }
    }
Example #6
0
    private static void OnPostprocessAllAssets(string[] a_rImportedAssets, string[] a_rDeletedAssets, string[] a_rMovedAssets, string[] a_rMovedFromPath)
    {
        if (ms_bEnabled)
        {
            bool bUpdateAssets       = false;
            bool bPostprocessPrefabs = false;
            bool bSaveTable          = false;

            Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;

            foreach (string rImportedAssetPath in a_rImportedAssets)
            {
                Texture2D rImportedTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Texture2D));

                if (rImportedTexture != null)
                {
                    if (Uni2DEditorUtils.IsMarkedAsSourceTexture(rImportedTexture))
                    {
                        //Debug.Log ( "Imported " + rImportedAssetPath );

                        string rImportedTextureGUID = AssetDatabase.AssetPathToGUID(rImportedAssetPath);
                        ms_oImportedTextureGUIDs.Add(rImportedTextureGUID);

                        Uni2DEditorUtils.GenerateTextureImportGUID(rImportedTexture);

                        bUpdateAssets = true;

                        rImportedTexture = null;

                        MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                        continue;
                    }
                }

                Uni2DTextureAtlas rImportedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DTextureAtlas));
                if (rImportedAtlas != null)
                {
                    //Debug.Log ( "Imported atlas " + rImportedAssetPath );

                    bSaveTable = true;

                    rAssetTable.AddAtlasPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                    rImportedAtlas = null;
                    MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                    continue;
                }

                Uni2DAnimationClip rImportedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DAnimationClip));
                if (rImportedClip != null)
                {
                    //Debug.Log ( "Imported clip " + rImportedClip );

                    bSaveTable = true;

                    rAssetTable.AddClipPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                    rImportedClip = null;
                    MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                    continue;
                }

                GameObject rImportedGameObject = (GameObject)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(GameObject));
                if (rImportedGameObject != null)
                {
                    //Debug.Log ( "Imported game object " + rImportedAssetPath );
                    ms_oGameObjectGUIDsToPostProcess.Add(AssetDatabase.AssetPathToGUID(rImportedAssetPath));

                    bPostprocessPrefabs = true;

                    rImportedGameObject = null;
                    MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                }
            }

            // Moved assets
            for (int iIndex = 0, iCount = a_rMovedAssets.Length; iIndex < iCount; ++iIndex)
            {
                //Debug.Log ( "Importing moved asset" );
                Uni2DTextureAtlas rMovedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DTextureAtlas));
                if (rMovedAtlas != null)
                {
                    rAssetTable.RemoveAtlasFromPath(a_rMovedFromPath[iIndex], false);
                    rAssetTable.AddAtlasPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex]));

                    bSaveTable = true;

                    rMovedAtlas = null;
                    MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                    continue;
                }

                Uni2DAnimationClip rMovedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DAnimationClip));
                if (rMovedClip != null)
                {
                    rAssetTable.RemoveClipFromPath(a_rMovedFromPath[iIndex], false);
                    rAssetTable.AddClipPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex]));

                    bSaveTable = true;

                    rMovedClip = null;
                    MultiUnityVersionSupportUtility.UnloadUnusedAssets( );
                }
            }

            // Deleted assets
            foreach (string rDeletedAsset in a_rDeletedAssets)
            {
                string[] rSpritePrefabGUIDs = rAssetTable.GetSpritePrefabGUIDsUsingThisAtlasPath(rDeletedAsset);

                if (rSpritePrefabGUIDs.Length > 0)
                {
                    bUpdateAssets = true;
                    ms_oSpritePrefabGUIDsToUpdate.UnionWith(rSpritePrefabGUIDs);
                }

                /*
                 * // TODO: mettre des paths au lieu d'IDs
                 * string[ ] rClipGUIDs = rAssetTable.GetClipGUIDsUsingThisTexturePath( rDeletedAsset );
                 * if( rClipGUIDs.Length > 0 )
                 * {
                 *      bUpdateAssets = true;
                 *      ms_oAnimationClipGUIDsToUpdate.UnionWith( rClipGUIDs );
                 * }
                 */

                bSaveTable = rAssetTable.RemoveAtlasFromPath(rDeletedAsset, true) || bSaveTable;
                bSaveTable = rAssetTable.RemoveClipFromPath(rDeletedAsset, true) || bSaveTable;
            }

            if (bSaveTable)
            {
                rAssetTable.Save( );
            }

            if (bUpdateAssets)
            {
                ms_oAtlasGUIDsToUpdate.UnionWith(rAssetTable.GetAtlasGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));
                ms_oAnimationClipGUIDsToUpdate.UnionWith(rAssetTable.GetClipGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));

                ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs));
                ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseAtlases(ms_oAtlasGUIDsToUpdate));

                EditorApplication.delayCall += UpdateUni2DAssets;
            }

            if (bPostprocessPrefabs)
            {
                EditorApplication.delayCall += OnSpritePrefabPostprocess;
            }
        }
    }