private void ImportSpriteSheetIntoAnimation(Texture2D spriteSheet, Sprite[] sprites, int animIdx = -1, string name = "")
        {
            DirectionalAnimData anim = animIdx >= 0 ? m_target.GetAnimList()[animIdx] : null;

            if (anim == null)
            {
                DirectionalAnimData animToClone = m_target.GetAnim(animIdx);
                anim      = m_target.AddAnim(animToClone);
                anim.name = name;
            }

            int spriteCount = m_target.DirectionsPerAnim * m_target.FramesPerAnim;

            if (sprites.Length == spriteCount)
            {
                for (int gy = 0, spriteIdx = 0; gy < m_target.DirectionsPerAnim; ++gy)
                {
                    int mappedDir = (int)m_target.DirectionMapping[gy];
                    mappedDir >>= (int)(eDirectionalAnimType.Eight - m_target.DirectionType);
                    for (int gx = 0; gx < m_target.FramesPerAnim; ++gx, ++spriteIdx)
                    {
                        anim.SetAnimFrame(mappedDir, gx, sprites[spriteIdx]);
                    }
                }
            }

            serializedObject.Update();
        }
        public DirectionalAnimData AddAnim(DirectionalAnimData animToClone = null)
        {
            DirectionalAnimData newAnim = animToClone != null ? new DirectionalAnimData(animToClone) : new DirectionalAnimData(DirectionsPerAnim, m_fpa);

            m_anims.Add(newAnim);
            return(newAnim);
        }
 public DirectionalAnimData(DirectionalAnimData anim)
 {
     m_dirNb     = anim.m_dirNb;
     m_frameNb   = anim.m_frameNb;
     animSprites = new Sprite[m_dirNb * m_frameNb];
     System.Array.Copy(anim.animSprites, animSprites, animSprites.Length);
 }
        private ReorderableList CreateAnimReorderableList()
        {
            ReorderableList reordList     = new ReorderableList(m_target.DirectionalAnimController.GetAnimList(), typeof(DirectionalAnimData), true, true, true, true);
            float           elementHeight = m_target.DirectionalAnimController.DirectionsPerAnim <= 4 ? 64f : 128f;

            reordList.displayAdd          = reordList.displayRemove = false;
            reordList.elementHeight       = 0;
            reordList.draggable           = reordList.elementHeight > 0f;
            reordList.drawHeaderCallback += (Rect rect) =>
            {
                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "Animations", EditorStyles.boldLabel);
                Texture2D btnTexture = reordList.elementHeight == 0f ? EditorGUIUtility.FindTexture("winbtn_win_max_h") : EditorGUIUtility.FindTexture("winbtn_win_min_h");
                if (GUI.Button(new Rect(rect.x + rect.width - EditorGUIUtility.singleLineHeight, rect.y, EditorGUIUtility.singleLineHeight, EditorGUIUtility.singleLineHeight), btnTexture, EditorStyles.label))
                {
                    reordList.elementHeight = reordList.elementHeight == 0f ? elementHeight : 0f;
                    reordList.draggable     = reordList.elementHeight > 0f;
                }
            };
            reordList.drawElementCallback += (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (reordList.elementHeight == 0f)
                {
                    return;
                }
                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.DirectionalAnimController.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.DirectionalAnimController.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, "");
                    }
                }
            };
            reordList.onSelectCallback += (ReorderableList list) =>
            {
                m_target.AnimIndex = m_animReordList.index;
                GUI.changed        = true;
            };
            return(reordList);
        }
        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);
        }
Beispiel #6
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);
        }
 public DirectionalAnimData AddAnim( DirectionalAnimData animToClone = null )
 {
     DirectionalAnimData newAnim = animToClone != null ? new DirectionalAnimData(animToClone) : new DirectionalAnimData(DirectionsPerAnim, m_fpa);
     m_anims.Add(newAnim);
     return newAnim;
 }
 public DirectionalAnimData(DirectionalAnimData anim)
 {
     m_dirNb = anim.m_dirNb;
     m_frameNb = anim.m_frameNb;
     animSprites = new Sprite[m_dirNb * m_frameNb];
     System.Array.Copy(anim.animSprites, animSprites, animSprites.Length);
 }