Esempio n. 1
0
        /// <summary>
        /// Forces serialization of the current animation
        /// </summary>
        /// <param name="toDisk">If true, it forces the asset database to save the file to disk. It causes little freeze, so I only use it on a few moments. Remember to save project before closing Unity!!</param>
        private void SaveFile(bool toDisk = false)
        {
            selectedAnimation.Setup();
            EditorUtility.SetDirty(selectedAnimation);

            if (toDisk)
            {
                AssetDatabase.SaveAssets();
            }
        }
Esempio n. 2
0
        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>();

                // 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);
            }
        }
Esempio n. 3
0
        public override void OnInspectorGUI()
        {
            saveToDisk = false;

            Undo.RecordObject(animation, "Change FPS");
            animation.FPS = EditorGUILayout.IntField("FPS", animation.FPS);

            if (frameList != null)
            {
                scrollWindowPosition = EditorGUILayout.BeginScrollView(scrollWindowPosition);

                // Individual frames
                frameList.displayRemove = (animation.FramesCount > 0);
                frameList.DoLayoutList();
                EditorGUILayout.Space();

                EditorGUILayout.EndScrollView();
            }

            if (animation.FramesCount > 0)
            {
                if (GUILayout.Button("Delete All Frames"))
                {
                    Undo.RecordObject(animation, "Delete All Frames");

                    animation.Frames.Clear();
                    animation.FramesDuration.Clear();
                    InitializeReorderableList();
                    saveToDisk = true;
                }
            }

            if (GUI.changed || saveToDisk)
            {
                animation.Setup();
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(animation);
                if (saveToDisk)
                {
                    AssetDatabase.SaveAssets();
                }
            }
        }
Esempio n. 4
0
        public override void OnInspectorGUI()
        {
            animation.FPS = EditorGUILayout.IntField("FPS", animation.FPS);


            // Individual frames
            if (frameList != null)
            {
                scrollWindowPosition    = EditorGUILayout.BeginScrollView(scrollWindowPosition);
                frameList.displayRemove = (animation.Frames != null && animation.Frames.Count > 0);
                frameList.DoLayoutList();
                EditorGUILayout.Space();
                EditorGUILayout.EndScrollView();
            }
            else
            {
                EditorGUILayout.LabelField("No frames");
            }

            if (eventList != null)
            {
                // Individual events
                scrollWindowPosition    = EditorGUILayout.BeginScrollView(scrollWindowPosition);
                eventList.displayRemove = (animation.Events != null && animation.Events.Count > 0);
                eventList.DoLayoutList();
                EditorGUILayout.Space();
                EditorGUILayout.EndScrollView();
            }
            else
            {
                EditorGUILayout.LabelField("No events");
            }

            if (GUI.changed)
            {
                animation.Setup();
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(animation);
            }
        }
Esempio n. 5
0
        private void StartPlay()
        {
            // If we have an animation to play, flag as playing, reset timer and take frame count
            if (currentAnimation != null)
            {
                // Failsafe for old animations without the total animation duration calculated.
                if (currentAnimation.AnimationDuration == -1)
                {
                    currentAnimation.Setup();
                }

                if (!useAnimatorFramerate)
                {
                    currentFramerate = currentAnimation.FPS;
                }
                timePerFrame         = 1f / currentFramerate;
                framesInAnimation    = currentAnimation.FramesCount;
                currentAnimationTime = (currentBackwards) ? currentAnimation.AnimationDuration * timePerFrame : 0;

                // 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 (CheckLastFrame())
                    {
#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));
                    CheckEvents();
                }
            }
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            else
            {
                Debug.LogError("Animation '" + name + "' not found.", gameObject);
            }
#endif
        }
        public override void OnInspectorGUI()
        {
            saveToDisk = false;

            Undo.RecordObject(animation, "Change FPS");
            animation.FPS = EditorGUILayout.IntField("FPS", animation.FPS);

            if (frameList != null)
            {
                scrollWindowPosition = EditorGUILayout.BeginScrollView(scrollWindowPosition);

                // Individual frames
                frameList.displayRemove = (animation.FramesCount > 0);
                frameList.DoLayoutList();
                EditorGUILayout.Space();

                EditorGUILayout.EndScrollView();
            }

            if (animation.FramesCount > 0)
            {
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                {
                    if (GUILayout.Button("Delete All Frames"))
                    {
                        Undo.RecordObject(animation, "Delete All Frames");

                        animation.Frames.Clear();
                        animation.FramesDuration.Clear();
                        InitializeReorderableList();
                        saveToDisk = true;
                    }

                    if (GUILayout.Button("Reverse Frames"))
                    {
                        Undo.RecordObject(animation, "Reverse Frames");

                        List <Sprite> prevFrames         = new List <Sprite>(animation.Frames);
                        List <int>    prevFramesDuration = new List <int>(animation.FramesDuration);

                        animation.Frames.Clear();
                        animation.FramesDuration.Clear();

                        for (int i = prevFrames.Count - 1; i >= 0; i--)
                        {
                            animation.Frames.Add(prevFrames[i]);
                            animation.FramesDuration.Add(prevFramesDuration[i]);
                        }

                        InitializeReorderableList();
                        saveToDisk = true;
                    }
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUI.changed || saveToDisk)
            {
                animation.Setup();
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(animation);
                if (saveToDisk)
                {
                    AssetDatabase.SaveAssets();
                }
            }
        }