yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
 
/// <summary>
/// UI帧动画管理器 - 负责帧动画资源的加载和管理
/// </summary>
public class UIFrameMgr {
    private static UIFrameMgr _inst = null;
    private static readonly object _lock = new object();
    
    public static UIFrameMgr Inst {
        get {
            if (_inst == null) {
                lock (_lock) {
                    if (_inst == null) {
                        _inst = new UIFrameMgr();
                    }
                }
            }
            return _inst;
        }
    }
 
    // 帧动画资源缓存
    private Dictionary<string, List<Sprite>> allFrameDic = new Dictionary<string, List<Sprite>>();
    // 是否已初始化
    private bool isInitialized = false;
 
    public UIFrameMgr()
    {
        Init();
    }
 
    /// <summary>
    /// 初始化管理器
    /// </summary>
    public void Init()
    {
        if (isInitialized)
            return;
            
        allFrameDic.Clear();
        isInitialized = true;
    }
 
    /// <summary>
    /// 加载指定帧动画资源
    /// </summary>
    private void LoadFrameSprites(FrameAnimationConfig cfg)
    {
        if (allFrameDic.ContainsKey(cfg.name))
            return;
 
        List<UniTask> uniTasks = new List<UniTask>();
            
        List<Sprite> spriteList = new List<Sprite>(new Sprite[cfg.frameCnt]);
        for (int i = 1; i <= cfg.frameCnt; i++)
        {
            int index = i;
            string spritePath = "Sprite/" + cfg.folder;
            string spriteName = StringUtility.Concat(cfg.name, "_", i.ToString());
 
            var task = ResManager.Instance.LoadAssetAsync<Sprite>(spritePath, spriteName).ContinueWith(sprite =>
            {
                if (sprite != null)
                {
                    spriteList[index - 1] = sprite;
                }
                else
                {
                    Debug.LogError($"加载帧动画资源失败: {spritePath}/{spriteName}");
                }
            });
 
            uniTasks.Add(task);
        }
 
        UniTask.WhenAll(uniTasks).ContinueWith(() =>
        {
            if (this == null) // 检查管理器是否已被销毁
                return;
                
            if (spriteList.Count > 0)
            {
                allFrameDic.Add(cfg.name, spriteList);
            }
        }).Forget();
        
 
    }
 
    /// <summary>
    /// 获取帧动画资源
    /// </summary>
    public List<Sprite> GetDynamicImage(string key)
    {
        // 按需加载资源
        if (!allFrameDic.ContainsKey(key))
        {
            LoadFrameSprites(FrameAnimationConfig.Get(key));
        }
        
        if (allFrameDic.TryGetValue(key, out var list))
        {
            return list;
        }
        return null;
    }
 
    /// <summary>
    /// 检查是否包含指定帧动画
    /// </summary>
    public bool ContainsDynamicImage(string key)
    {
        return FrameAnimationConfig.HasKey(key);
    }
    
    
    /// <summary>
    /// 预加载指定帧动画
    /// </summary>
    public void PreloadDynamicImage(string key)
    {
        if (!allFrameDic.ContainsKey(key))
        {
            LoadFrameSprites(FrameAnimationConfig.Get(key));
        }
    }
}