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( );
            }
        }
    }
Beispiel #2
0
    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>( ));
    }
    private static bool DoDisplayAnimationClipHeader(Uni2DAnimationClip a_rAnimationClip, Uni2DAnimationPlayer a_rAnimationPreviewPlayer, out AnimationGUIAction a_eAction, bool a_bCompactMode, bool a_bFolded, int iClipIndex = -1)
    {
        a_eAction = AnimationGUIAction.None;

        ///// Clip header /////
        EditorGUILayout.BeginVertical(EditorStyles.textField);
        {
            if (a_bCompactMode)
            {
                ///// Top toolbar /////
                EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.ExpandWidth(true));
                {
                    // ^
                    if (GUILayout.Button("\u25B2" /*"\u2191"*/, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                    {
                        a_eAction = AnimationGUIAction.MoveUp;
                    }

                    // v
                    if (GUILayout.Button("\u25BC" /*"\u2193"*/, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                    {
                        a_eAction = AnimationGUIAction.MoveDown;
                    }

                    if (iClipIndex > -1)
                    {
                        if (GUILayout.Button("Clip #" + iClipIndex, EditorStyles.toolbarButton, GUILayout.ExpandWidth(true)))
                        {
                            EditorGUIUtility.PingObject(a_rAnimationClip.gameObject);
                        }
                    }
                    else
                    {
                        GUILayout.FlexibleSpace( );
                    }

                    // X
                    if (GUILayout.Button("X", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                    {
                        a_eAction = AnimationGUIAction.Close;
                    }
                }
                EditorGUILayout.EndHorizontal( );
            }
            ///////////////

            EditorGUILayout.Space( );

            EditorGUILayout.BeginHorizontal( );
            {
                Rect oClipTextureRect = GUILayoutUtility.GetRect(64.0f, 64.0f, 64.0f, 64.0f, GUILayout.ExpandWidth(false));

                // Animation clip preview
                Uni2DEditorGUIUtils.DrawAnimationClipPreview(oClipTextureRect, a_rAnimationClip, a_rAnimationPreviewPlayer);

                // Clip settings
                EditorGUILayout.BeginVertical( );
                {
                    //string rName;
                    float fFrameRate;
                    Uni2DAnimationClip.WrapMode eWrapMode;

                    // Name
                    //rName = EditorGUILayout.TextField( "Clip Name", a_rAnimationClip.name );
                    EditorGUILayout.LabelField(a_rAnimationClip.name, EditorStyles.boldLabel);

                    EditorGUI.BeginChangeCheck( );
                    {
                        EditorGUI.BeginChangeCheck( );
                        {
                            // Frame rate
                            fFrameRate = EditorGUILayout.FloatField("Frame Rate", a_rAnimationClip.frameRate);
                            // Wrap mode
                            eWrapMode = (Uni2DAnimationClip.WrapMode)EditorGUILayout.EnumPopup("Wrap Mode", a_rAnimationClip.wrapMode);
                        }
                        if (EditorGUI.EndChangeCheck( ))
                        {
                            // Update animation players settings
                            a_rAnimationPreviewPlayer.FrameRate = fFrameRate;
                            a_rAnimationPreviewPlayer.WrapMode  = eWrapMode;
                        }
                    }
                    if (EditorGUI.EndChangeCheck( ))
                    {
                        //a_rAnimationClip.name      = rName;
                        a_rAnimationClip.frameRate = fFrameRate;
                        a_rAnimationClip.wrapMode  = eWrapMode;

                        if (a_bCompactMode)
                        {
                            a_rAnimationClip.ApplySettings(Uni2DAnimationClip.AnimationClipRegeneration.RegenerateNothing);
                        }

                        EditorUtility.SetDirty(a_rAnimationClip);
                    }

                    // Clip length infos
                    // TODO: refactor with AnimationPlayer
                    int iClipFrameCount    = a_rAnimationClip.FrameCount;
                    int iWrappedFrameCount = iClipFrameCount;
                    if (a_rAnimationClip.wrapMode == Uni2DAnimationClip.WrapMode.PingPong && iWrappedFrameCount > 2)
                    {
                        iWrappedFrameCount = (iWrappedFrameCount * 2 - 2);
                    }

                    float fClipLength = Mathf.Abs(iWrappedFrameCount / a_rAnimationClip.frameRate);

                    EditorGUILayout.BeginHorizontal( );
                    {
                        EditorGUILayout.LabelField(iClipFrameCount + " frame(s) = "
                                                   + (iClipFrameCount != iWrappedFrameCount
                                                                ? (iWrappedFrameCount + " wrapped frame(s) = ")
                                                                : null
                                                      )
                                                   + fClipLength + " sec. @ "
                                                   + a_rAnimationClip.frameRate + " FPS",
                                                   EditorStyles.miniLabel, GUILayout.ExpandWidth(false));

                        if (a_bCompactMode && GUILayout.Button("Edit", EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
                        {
                            Selection.activeObject = a_rAnimationClip;
                        }
                    }
                    EditorGUILayout.EndHorizontal( );
                }
                EditorGUILayout.EndVertical( );
            }
            EditorGUILayout.EndHorizontal( );

            // Frame foldout
            if (!a_bCompactMode)
            {
                EditorGUILayout.BeginHorizontal( );
                {
                    a_bFolded = EditorGUILayout.Foldout(a_bFolded, GUIContent.none);
                }
                EditorGUILayout.EndHorizontal( );
            }
            else
            {
                EditorGUILayout.Space( );
            }
        }
        EditorGUILayout.EndVertical( );

        return(a_bFolded);
    }