//-------------------------------------------------------- // [Author]: 玩个游戏 // [ Date ]: Sunday, December 10, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.UI; using System; public class FrameEffect : MonoBehaviour { [SerializeField] Image m_Behaviour; [SerializeField] Sprite[] m_Sprites; float interval = 0.1f; int index = 0; float timer = 0f; float tempSumTime = 1f; [SerializeField] float onceTime = 1f; [SerializeField] float sumTime = 1f; [SerializeField] int aniIndex = 0; public bool loop = true; public event Action CompleteAct; private void OnEnable() { if (m_Sprites != null && m_Sprites.Length > 0) { interval = onceTime / m_Sprites.Length; index = 0; timer = 0f; tempSumTime = sumTime; m_Behaviour.overrideSprite = m_Sprites[index]; } } private void LateUpdate() { timer += Time.deltaTime; tempSumTime -= Time.deltaTime; if (m_Sprites == null || m_Sprites.Length == 0) { return; } if(tempSumTime > 0) { if (timer > interval) { m_Behaviour.overrideSprite = m_Sprites[index]; index = (++index) % m_Sprites.Length; timer -= interval; } } else { if(loop) { tempSumTime = sumTime; } if(CompleteAct != null) { CompleteAct(aniIndex); } } } public void SetSprites(Sprite[] sprites,float _totalTime) { m_Sprites = sprites; onceTime = _totalTime; sumTime = _totalTime; if (m_Sprites != null && m_Sprites.Length > 0) { interval = onceTime / m_Sprites.Length; index = 0; timer = 0f; tempSumTime = sumTime; m_Behaviour.sprite = m_Sprites[index]; m_Behaviour.overrideSprite = m_Sprites[index]; } } }