Пример #1
0
        /// <summary>
        /// Gets the custom event of an animation on the last frame.
        /// </summary>
        /// <returns>
        /// The event of the specified animation on the last frame. Null if not found.
        /// </returns>
        public SpriteAnimatorEvent GetCustomEventAtEnd(string animation)
        {
            SpriteAnimation anim = GetAnimation(animation);

            return(GetCustomEvent(anim, anim.FramesCount - 1));
        }
Пример #2
0
        private void Update()
        {
            // Calculate deltaTime
            float timeSinceStartup = (float)EditorApplication.timeSinceStartup;

            deltaTime           = timeSinceStartup - lastFrameEditorTime;
            lastFrameEditorTime = timeSinceStartup;

            if (animation == null || animation.FramesCount == 0)
            {
                animation = (SpriteAnimation)target;
            }

            if (frameList == null)
            {
                InitializeReorderableList();
            }

            // Check animation bounds
            if (currentFrame < 0)
            {
                currentFrame = 0;
            }
            else if (currentFrame > animation.FramesCount)
            {
                currentFrame = animation.FramesCount - 1;
            }

            // Check if playing and use the editor time to change frames
            if (isPlaying)
            {
                animationTimer += deltaTime;
                float timePerFrame = 1f / framesPerSecond;
                if (timePerFrame < animationTimer)
                {
                    frameDurationCounter++;
                    animationTimer -= timePerFrame;
                    if (frameDurationCounter >= animation.FramesDuration[currentFrame])
                    {
                        // Change frame and repaint the preview
                        currentFrame++;
                        if (currentFrame >= animation.FramesCount)
                        {
                            currentFrame = 0;

                            if (!loop)
                            {
                                isPlaying = false;
                            }
                        }

                        frameDurationCounter = 0;
                        Repaint();
                        forceRepaint = true;
                    }
                }
            }

            // Save preview FPS value on the editorPrefs
            if (framesPerSecond != loadedFPS)
            {
                loadedFPS = framesPerSecond;
                EditorPrefs.SetInt(FPS_EDITOR_PREFS, framesPerSecond);
            }
        }
Пример #3
0
 /// <summary>
 /// Adds a custom event to specified animation on the last frame.
 /// </summary>
 /// <returns>
 /// The event created. Null if the animation is null.
 /// </returns>
 public SpriteAnimatorEvent AddCustomEventAtEnd(SpriteAnimation animation)
 {
     return(AddCustomEvent(animation, animation.FramesCount - 1));
 }
Пример #4
0
        /// <summary>
        /// Gets the custom event of an animation on a certain frame.
        /// </summary>
        /// <returns>
        /// The event of the specified animation on the selected frame. Null if not found.
        /// </returns>
        public SpriteAnimatorEvent GetCustomEvent(string animation, int frame)
        {
            SpriteAnimation anim = GetAnimation(animation);

            return(GetCustomEvent(anim, frame));
        }
Пример #5
0
 /// <summary>
 /// Sets the fallback animation to play and its loop type
 /// </summary>
 public void SetFallbackAnimation(string animation, LoopType loopType)
 {
     fallbackAnimation = GetAnimation(animation);
     fallbackLoopType  = loopType;
 }
Пример #6
0
 /// <summary>
 /// Removes the fallback animation
 /// </summary>
 public void RemoveFallbackAnimation()
 {
     fallbackAnimation = null;
 }
Пример #7
0
 /// <summary>
 /// Plays an animation starting at the specified normalized time (between 0 and 1).
 /// </summary>
 public void PlayStartingAtNormalizedTime(SpriteAnimation animation, float normalizedTime, bool playOneShot = false, bool playBackwards = false, LoopType loopType = LoopType.repeat)
 {
     Play(animation, playOneShot, playBackwards, loopType);
     SetAnimationNormalizedTime(normalizedTime);
 }
Пример #8
0
 /// <summary>
 /// Plays an animation starting at the specified frame.
 /// </summary>
 public void PlayStartingAtFrame(SpriteAnimation animation, int frame, bool playOneShot = false, bool playBackwards = false, LoopType loopType = LoopType.repeat)
 {
     startingFrame = frame;
     Play(animation, playOneShot, playBackwards, loopType);
 }
Пример #9
0
        private void OnGUI()
        {
            // Style initialization
            if (!init)
            {
                Initialize();
                init = true;
            }

            // Create animation box
            NewAnimationBox();

            if (justCreatedAnim)
            {
                justCreatedAnim = false;
                return;
            }

            // Edit animation box
            EditorGUILayout.Space();
            EditorGUILayout.BeginVertical();
            {
                // Animation asset field
                if (selectedAnimation == null)
                {
                    EditorGUILayout.BeginVertical(box);
                    selectedAnimation = EditorGUILayout.ObjectField("Animation", selectedAnimation, typeof(SpriteAnimation), false) as SpriteAnimation;
                    EditorGUILayout.EndVertical();
                }
                else
                {
                    // Init reorderable list
                    if (frameList == null)
                    {
                        InitializeReorderableList();
                    }

                    // Add the frames dropped on the drag and drop box
                    if (draggedSprites != null && draggedSprites.Count > 0)
                    {
                        // TODO Record Undo/Redo for dragged sprites, currently not working, don't know why
                        //Undo.RecordObject(selectedAnimation, "Add Frames");

                        for (int i = 0; i < draggedSprites.Count; i++)
                        {
                            AddFrame(draggedSprites[i]);
                        }
                        draggedSprites.Clear();

                        SaveFile(true);
                    }

                    // Retrocompatibility check for the new frames duration field
                    if (selectedAnimation.FramesCount != selectedAnimation.FramesDuration.Count)
                    {
                        selectedAnimation.FramesDuration.Clear();
                        for (int i = 0; i < selectedAnimation.FramesCount; i++)
                        {
                            selectedAnimation.FramesDuration.Add(1);
                        }
                    }

                    // Config settings
                    ConfigBox();

                    EditorGUILayout.Space();

                    EditorGUILayout.BeginHorizontal();
                    {
                        // Preview window setup
                        Rect previewRect = EditorGUILayout.BeginVertical(lowPaddingBox, GUILayout.MaxWidth(position.width / 2));
                        PreviewBox(previewRect);
                        EditorGUILayout.EndVertical();

                        EditorGUILayout.BeginVertical();
                        {
                            // FPS
                            int fps = selectedAnimation.FPS;
                            EditorGUI.BeginChangeCheck();
                            {
                                fps = EditorGUILayout.IntField("FPS", selectedAnimation.FPS);
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                Undo.RecordObject(selectedAnimation, "Change FPS");
                                selectedAnimation.FPS = fps;
                                if (selectedAnimation.FPS < 0)
                                {
                                    selectedAnimation.FPS = 0;
                                }
                            }

                            EditorGUILayout.Space();

                            scrollWindowPosition = EditorGUILayout.BeginScrollView(scrollWindowPosition);
                            {
                                // Individual frames
                                frameList.displayRemove = (selectedAnimation.FramesCount > 0);
                                frameList.DoLayoutList();
                                EditorGUILayout.Space();
                            }
                            EditorGUILayout.EndScrollView();

                            EditorGUILayout.Space();
                        }
                        EditorGUILayout.EndVertical();

                        // Check Events
                        Event evt = Event.current;
                        switch (evt.type)
                        {
                        // Delete frames with supr
                        case EventType.KeyDown:
                            if (Event.current.keyCode == KeyCode.Delete &&
                                selectedAnimation.FramesCount > 0 &&
                                frameList.HasKeyboardControl() &&
                                frameListSelectedIndex != -1)
                            {
                                RemoveFrameListItem(frameList);
                            }
                            break;

                        // Zoom preview window with scrollwheel
                        case EventType.ScrollWheel:
                            if (spritePreview != null)
                            {
                                Vector2 mpos = Event.current.mousePosition;
                                if (mpos.x >= previewRect.x && mpos.x <= previewRect.x + previewRect.width &&
                                    mpos.y >= previewRect.y && mpos.y <= previewRect.y + previewRect.height)
                                {
                                    Repaint();
                                    spritePreview.Zoom = -evt.delta.y;
                                }
                            }
                            break;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            EditorGUILayout.EndVertical();

            if (GUI.changed && selectedAnimation != null)
            {
                SaveFile();
            }
        }
Пример #10
0
        /// <summary>
        /// Plays an animation.
        /// </summary>
        public void Play(string name, bool playOneShot = false, bool playBackwards = false)
        {
            SetActiveRenderer(true);

            oneShot   = playOneShot;
            backwards = playBackwards;

            // If it's the same animation but not playing, reset it, if playing, do nothing.
            if (currentAnimation != null && currentAnimation.Name == name)
            {
                if (!playing)
                {
                    Restart();
                    Resume();
                }
                else
                {
                    return;
                }
            }
            // Look for the animation only if its new or current animation is null
            else if (currentAnimation == null || currentAnimation.Name != name)
            {
                currentAnimation = GetAnimation(name);
            }

            // If we have an animation to play, flag as playing, reset timer and take frame count
            if (currentAnimation != null)
            {
                framesInAnimation = currentAnimation.FramesCount;

                // Check if the animation have frames. Show warning if not.
                if (framesInAnimation == 0)
                {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                    Debug.LogWarning("Animation '" + name + "' has no frames.", gameObject);
#endif
                    playing = false;
                    return;
                }

                Restart();

                // The first loop will have a random start frame if desired
                if (startAtRandomFrame && !randomStartFrameApplied)
                {
                    randomStartFrameApplied = true;
                    frameIndex = Random.Range(0, framesInAnimation - 1);
                }
                else if (startingFrame != -1)
                {
                    frameIndex = startingFrame;
                    if (frameIndex > framesInAnimation - 1)
                    {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                        Debug.LogWarning("Starting frame out of bounds.", gameObject);
#endif
                        frameIndex = 0;
                    }
                    startingFrame = -1;
                }

                onPlay.Invoke();
                playing = true;

                if (!waitingLoop)
                {
                    ChangeFrame(currentAnimation.GetFrame(frameIndex));
                }
            }
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            else
            {
                Debug.LogError("Animation '" + name + "' not found.", gameObject);
            }
#endif
        }
        private void OnEnable()
        {
            if (!enabled)
            {
                enabled = true;

                if (target == null)
                {
                    return;
                }

                if (animation == null)
                {
                    animation = (SpriteAnimation)target;
                    animation.Setup();
                }

                EditorApplication.update += Update;

                // Load last used settings
                loadedFPS = framesPerSecond = EditorPrefs.GetInt(FPS_EDITOR_PREFS, 30);

                // Setup preview object and camera
                go       = EditorUtility.CreateGameObjectWithHideFlags("previewGO", HideFlags.HideAndDontSave, typeof(SpriteRenderer));
                cameraGO = EditorUtility.CreateGameObjectWithHideFlags("cameraGO", HideFlags.HideAndDontSave, typeof(Camera));
                sr       = go.GetComponent <SpriteRenderer>();
                pc       = cameraGO.GetComponent <Camera>();

#if !UNITY_5
                // Colorspace correction is only needed after Unity 5 for some reasons
                linearMaterial  = Resources.Load <Material>("Spritedow");
                defaultMaterial = sr.sharedMaterial;
                if (PlayerSettings.colorSpace == ColorSpace.Linear)
                {
                    sr.sharedMaterial = linearMaterial;
                }
                else
                {
                    sr.sharedMaterial = defaultMaterial;
                }
#endif


                // Set camera
                pc.cameraType       = CameraType.Preview;
                pc.clearFlags       = CameraClearFlags.Depth;
                pc.backgroundColor  = Color.clear;
                pc.orthographic     = true;
                pc.orthographicSize = 3;
                pc.nearClipPlane    = -10;
                pc.farClipPlane     = 10;
                pc.targetDisplay    = -1;
                pc.depth            = -999;

                // Set renderer
                if (animation != null && animation.FramesCount > 0)
                {
                    sr.sprite = animation.Frames[0];
                    cameraGO.transform.position = Vector2.zero;
                }

                // Get preview culling layer in order to render only the preview object and nothing more
                pc.cullingMask = -2147483648;
                go.layer       = 0x1f;

                // Also, disable the object to prevent render on scene/game views
                go.SetActive(false);
            }
        }