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;
            }
        }
    }
Example #2
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;
            }
        }
    }