using System.Collections;
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
using UnityEngine.UI;
|
[RequireComponent(typeof(Image))]
|
public class UIFrame : MonoBehaviour {
|
[SerializeField]
|
private string frameKey = string.Empty;
|
|
private List<Sprite> sprites = null;
|
|
private int index = 0;
|
|
private bool run = false;
|
|
private Image m_Image;
|
|
private float m_Interval = 0.1f;
|
|
private void Awake()
|
{
|
m_Image = GetComponent<Image>();
|
}
|
|
private void OnEnable()
|
{
|
Run();
|
}
|
|
public void ResetFrame(string key)
|
{
|
TimeMgr.Instance.UnRegister(m_Image);
|
frameKey = key;
|
Run();
|
}
|
|
void Run()
|
{
|
if (m_Image==null||frameKey.Equals(string.Empty)) return;
|
if (!gameObject.activeInHierarchy) {
|
return;
|
}
|
sprites = UIFrameMgr.Inst.GetDynamicFace(frameKey);
|
if (sprites == null||sprites.Count<1) {
|
DebugEx.LogError(string.Format("没有{0}对应的帧动画", frameKey));
|
enabled = false;
|
return;
|
}
|
|
run = true;
|
|
m_Image.sprite = sprites[0];
|
index = 1;
|
|
FaceConfig cfg = FaceConfig.Get(frameKey);
|
if (cfg != null&& cfg.speed > 0) {
|
m_Interval = 1.0f / cfg.speed;
|
}
|
|
|
TimeMgr.Instance.Register(m_Image, OnSpriteAnim, m_Interval, 0);
|
}
|
|
void OnSpriteAnim(Component comp)
|
{
|
if (!run) return;
|
if(sprites==null) {
|
TimeMgr.Instance.UnRegister(m_Image);
|
return;
|
}
|
if (index < 0 || index >= sprites.Count) {
|
index = 0;return;
|
}
|
m_Image.sprite = sprites[index];
|
index++;
|
if (index >= sprites.Count) index = 0;
|
}
|
|
private void OnDisable()
|
{
|
run = false;
|
TimeMgr.Instance.UnRegister(m_Image);
|
}
|
}
|