少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
    }
}