private static void ApplySettings(Uni2DAnimationClip[] a_rAnimationClips, AnimationClipRegeneration a_eRegenerate)
    {
        int iClipCount = a_rAnimationClips.Length;

        if (iClipCount == 1)
        {
            a_rAnimationClips[0].ApplySettings(a_eRegenerate);
        }
        else if (iClipCount > 1)
        {
            // Apply clip settings first, atlases will be generated only after
            // (Atlases can be shared accross clips, so prevent them to be uselessly regenerated several times)
            HashSet <Uni2DTextureAtlas> oAtlases = new HashSet <Uni2DTextureAtlas>( );

            for (int iClipIndex = 0; iClipIndex < iClipCount; ++iClipIndex)
            {
                Uni2DAnimationClip rAnimationClip = a_rAnimationClips[iClipIndex];
                Uni2DTextureAtlas  rGlobalAtlas   = rAnimationClip.globalAtlas;

                if (rGlobalAtlas != null)
                {
                    // Add the atlas if not already added
                    if (a_eRegenerate == AnimationClipRegeneration.RegenerateAll ||
                        (a_eRegenerate == AnimationClipRegeneration.RegenerateAlsoAtlasIfNeeded &&
                         (rGlobalAtlas.UnappliedSettings || rGlobalAtlas.Contains(rAnimationClip.GetAllFramesTextureGUIDs( )) == false)))
                    {
                        oAtlases.Add(rGlobalAtlas);
                    }
                }
                else                 // Null => atlas per frame
                {
                    for (int iFrameIndex = 0, iFrameCount = rAnimationClip.FrameCount; iFrameIndex < iFrameCount; ++iFrameIndex)
                    {
                        Uni2DAnimationFrame rFrame      = rAnimationClip.frames[iFrameIndex];
                        Uni2DTextureAtlas   rFrameAtlas = rFrame.atlas;

                        if (rFrameAtlas != null &&
                            (a_eRegenerate == AnimationClipRegeneration.RegenerateAll ||
                             (a_eRegenerate == AnimationClipRegeneration.RegenerateAlsoAtlasIfNeeded &&
                              (rFrameAtlas.UnappliedSettings || rFrameAtlas.Contains(rFrame.textureContainer.GUID) == false))))
                        {
                            oAtlases.Add(rFrameAtlas);
                        }
                    }
                }

                // Regenerate clips only
                rAnimationClip.ApplySettings(AnimationClipRegeneration.RegenerateAnimationClipOnly);
            }

            // Then, regenerate atlases
            foreach (Uni2DTextureAtlas rAtlas in oAtlases)
            {
                rAtlas.ApplySettings( );
            }
        }
    }
Пример #2
0
    public void Rebuild( )
    {
        //Debug.Log( "Rebuilding Uni2D Asset Table..." );

        m_oAtlasGUIDSpritePrefabGUIDsMultiDict.Clear( );
        m_oTextureGUIDAtlasGUIDsMultiDict.Clear( );
        m_oTextureGUIDClipGUIDsMultiDict.Clear( );
        m_oTextureGUIDSpritePrefabGUIDsMultiDict.Clear( );

        m_oAtlasPathGUIDDict.Clear( );
        m_oClipPathGUIDDict.Clear( );

        // Iterate project's assets
        string[] rAssetPaths = AssetDatabase.GetAllAssetPaths( );

        int   iAssetCount      = rAssetPaths.Length;
        float fInvAssetCount   = 1.0f / (float)iAssetCount;
        int   iProcessedAssets = 0;

        try
        {
            foreach (string rPath in rAssetPaths)
            {
                EditorUtility.DisplayProgressBar("Uni2D - Asset Table Rebuilding Progress",
                                                 iProcessedAssets + " out of " + iAssetCount + " asset(s) processed...",
                                                 fInvAssetCount * iProcessedAssets);

                Object rAssetObject = null;

                // Might be an atlas or a clip
                if (rPath.EndsWith(".prefab"))
                {
                    rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(Uni2DTextureAtlas));
                    if (rAssetObject != null)                           // It's an atlas
                    {
                        Uni2DTextureAtlas rAtlasAsset = (Uni2DTextureAtlas)rAssetObject;
                        string            rAtlasGUID  = AssetDatabase.AssetPathToGUID(rPath);

                        foreach (string rTextureGUID in rAtlasAsset.GetTextureGUIDs( ))
                        {
                            this.AddAtlasUsingTexture(rAtlasGUID, rTextureGUID);
                        }

                        m_oAtlasPathGUIDDict.Add(rPath, rAtlasGUID);

                        rAtlasAsset = null;
                        EditorUtility.UnloadUnusedAssets( );
                    }

                    rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(Uni2DAnimationClip));
                    if (rAssetObject != null)                           // It's an animation clip
                    {
                        Uni2DAnimationClip rAnimationClipAsset = (Uni2DAnimationClip)rAssetObject;
                        string             rAnimationClipGUID  = AssetDatabase.AssetPathToGUID(rPath);

                        foreach (string rTextureGUID in rAnimationClipAsset.GetAllFramesTextureGUIDs( ))
                        {
                            this.AddClipUsingTexture(rAnimationClipGUID, rTextureGUID);
                        }

                        m_oClipPathGUIDDict.Add(rPath, rAnimationClipGUID);

                        rAnimationClipAsset = null;
                        EditorUtility.UnloadUnusedAssets( );
                    }

                    rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(GameObject));
                    if (rAssetObject != null)                           // It's a sprite prefab
                    {
                        GameObject    rPrefabAsset            = (GameObject)rAssetObject;
                        string        rPrefabGUID             = AssetDatabase.AssetPathToGUID(rPath);
                        Uni2DSprite[] rSpritePrefabComponents = rPrefabAsset.GetComponentsInChildren <Uni2DSprite>(true);

                        foreach (Uni2DSprite rSpritePrefabComponent in rSpritePrefabComponents)
                        {
                            Uni2DEditorSpriteSettings rSpriteSettings = rSpritePrefabComponent.SpriteSettings;

                            this.AddSpritePrefabUsingTexture(rPrefabGUID, rSpriteSettings.textureContainer.GUID);

                            if (rSpriteSettings.atlas != null)
                            {
                                this.AddSpritePrefabUsingAtlas(rPrefabGUID, Uni2DEditorUtils.GetUnityAssetGUID(rSpriteSettings.atlas));
                            }
                        }

                        rPrefabAsset            = null;
                        rSpritePrefabComponents = null;
                        EditorUtility.UnloadUnusedAssets( );
                    }
                }

                ++iProcessedAssets;
            }
        }
        finally
        {
            this.Save( );

            EditorUtility.UnloadUnusedAssets( );

            EditorUtility.SetDirty(this);
            EditorUtility.ClearProgressBar( );
        }

        //Debug.Log( "Uni2D Asset Table Rebuild: Done." );
    }