private ReorderableList CreateAnimReorderableList()
        {
            ReorderableList reordList = new ReorderableList(m_target.GetAnimList(), typeof(DirectionalAnimData), true, true, true, true);

            reordList.drawHeaderCallback += (Rect rect) =>
            {
                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "Animations", EditorStyles.boldLabel);
                EditorGUI.TextArea(new Rect(rect.x - 6f, rect.y + EditorGUIUtility.singleLineHeight, rect.width + 12f, rect.height - EditorGUIUtility.singleLineHeight),
                                   "<size=11><i>Drag a sprite sheet into this box to create a new animation or into any animation to modify it</i></size>", Styles.Instance.richHelpBox);
                DoAnimationDragAndDrop(rect, -1);
            };
            reordList.drawElementCallback += (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                DirectionalAnimData anim = (DirectionalAnimData)reordList.list[index];
                anim.name = GUI.TextField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), anim.name);

                int   dirNb       = m_target.DirectionsPerAnim;
                float frameWidth  = rect.width / Mathf.Min(4, dirNb);
                int   rowNb       = 1 + (dirNb - 1) / 4;
                float frameHeight = (rect.height - EditorGUIUtility.singleLineHeight) / rowNb;
                for (int i = 0; i < dirNb; ++i)
                {
                    Vector2 dir    = Quaternion.Euler(Vector3.forward * (((float)i / dirNb) * 360f)) * Vector3.down;
                    Sprite  sprite = m_target.GetPreviewAnimSprite(dir, index);
                    if (sprite)
                    {
                        float aspectRatio = sprite.rect.width / sprite.rect.height;
                        Rect  uv          = new Rect()
                        {
                            position = Vector2.Scale(sprite.rect.position, sprite.texture.texelSize), size = Vector2.Scale(sprite.rect.size, sprite.texture.texelSize)
                        };
                        Rect rAnim = new Rect(rect.x + frameWidth * (i % 4), rect.y + EditorGUIUtility.singleLineHeight + (i / 4) * frameHeight, frameHeight * aspectRatio, frameHeight);
                        GUI.DrawTextureWithTexCoords(rAnim, sprite.texture, uv);
                    }
                    else
                    {
                        Rect rBox = new Rect(rect.x + frameWidth * i, rect.y + EditorGUIUtility.singleLineHeight, frameWidth, rect.height - EditorGUIUtility.singleLineHeight);
                        GUI.Box(rBox, "");
                    }
                }

                DoAnimationDragAndDrop(rect, index);
            };
            reordList.onAddCallback += (ReorderableList list) =>
            {
                Undo.RegisterCompleteObjectUndo(target, "Add Character Animation");
                DirectionalAnimData animToClone = m_target.GetAnim(list.index);
                m_target.AddAnim(animToClone);
                serializedObject.Update();
                GUI.changed = true;
            };
            return(reordList);
        }
Example #2
0
        public void UpdateAnim(float dt)
        {
            if (TargetSpriteRenderer == null || m_dirAnimCtrl == null)
            {
                return;
            }

            m_animIdx = (m_animIdx + m_dirAnimCtrl.GetAnimList().Count) % m_dirAnimCtrl.GetAnimList().Count;
            DirectionalAnimData anim = m_dirAnimCtrl.GetAnim(m_animIdx);

            if (IsPlaying)
            {
                if (IsPingPongAnim && (m_internalFrame == 0 || m_internalFrame == (anim.FramesPerDir - 1)))
                {
                    m_curFrameTime += dt * AnimSpeed * 2f; // avoid stay twice of the time in the first and last frame of the animation
                }
                else
                {
                    m_curFrameTime += dt * AnimSpeed;
                }
                while (m_curFrameTime >= 1f)
                {
                    m_curFrameTime -= 1f;
                    ++m_internalFrame; m_internalFrame %= anim.FramesPerDir;
                    if (m_internalFrame == 0)
                    {
                        if (OnAnimationLoopOver != null)
                        {
                            OnAnimationLoopOver(this);
                        }
                        m_isPingPongReverse = !m_isPingPongReverse;
                    }
                }
            }
            else
            {
                m_isPingPongReverse = false;
                if (m_stopFrameIdx >= 0)
                {
                    if (m_stopFrameIdx >= anim.FramesPerDir)
                    {
                        m_stopFrameIdx %= anim.FramesPerDir;
                    }
                    m_internalFrame = m_stopFrameIdx;
                }
            }

            TargetSpriteRenderer.sprite = GetCurrentSprite(AnimDirection);
        }