| | |
| | | using System.Collections; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | |
| | | |
| | | public class UIFrameMgr { |
| | | /// <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) { |
| | | _inst = new UIFrameMgr(); |
| | | lock (_lock) { |
| | | if (_inst == null) { |
| | | _inst = new UIFrameMgr(); |
| | | } |
| | | } |
| | | } |
| | | return _inst; |
| | | } |
| | | } |
| | | |
| | | // 表中的所有记录 |
| | | // 帧动画资源缓存 |
| | | private Dictionary<string, List<Sprite>> allFrameDic = new Dictionary<string, List<Sprite>>(); |
| | | //只有聊天表情用到的 根据路径frame来划分 |
| | | private List<string> chatFaces = new List<string>(); |
| | | // 是否已初始化 |
| | | private bool isInitialized = false; |
| | | |
| | | public UIFrameMgr() |
| | | { |
| | | Init(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 初始化管理器 |
| | | /// </summary> |
| | | public void Init() |
| | | { |
| | | if (isInitialized) |
| | | return; |
| | | |
| | | allFrameDic.Clear(); |
| | | chatFaces.Clear(); |
| | | var dic = FaceConfig.dic.Values; |
| | | foreach (var cfg in dic) |
| | | isInitialized = true; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 加载指定帧动画资源 |
| | | /// </summary> |
| | | private void LoadFrameSprites(FaceConfig cfg) |
| | | { |
| | | if (allFrameDic.ContainsKey(cfg.name)) |
| | | return; |
| | | |
| | | List<Sprite> spriteList = new List<Sprite>(); |
| | | |
| | | for (int i = 1; i <= cfg.frameCnt; i++) |
| | | { |
| | | if (cfg.frameType == 1 && !chatFaces.Contains(cfg.name)) |
| | | string spritePath = "Sprite/" + cfg.folder; |
| | | string spriteName = StringUtility.Contact(cfg.name, "_", i); |
| | | |
| | | Sprite sprite = ResManager.Instance.LoadAsset<Sprite>(spritePath, spriteName); |
| | | if (sprite != null) |
| | | { |
| | | chatFaces.Add(cfg.name); |
| | | spriteList.Add(sprite); |
| | | } |
| | | for (int i = 1; i <= cfg.frameCnt; i++) |
| | | { |
| | | Sprite sprite = ResManager.Instance.LoadAsset<Sprite>("Sprite/" + cfg.folder, StringUtility.Contact(cfg.name, "_", i)); |
| | | if (sprite != null) |
| | | { |
| | | List<Sprite> list = null; |
| | | allFrameDic.TryGetValue(cfg.name, out list); |
| | | if (list != null) |
| | | { |
| | | list.Add(sprite); |
| | | } |
| | | else |
| | | { |
| | | list = new List<Sprite>(); |
| | | list.Add(sprite); |
| | | allFrameDic.Add(cfg.name, list); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (spriteList.Count > 0) |
| | | { |
| | | allFrameDic.Add(cfg.name, spriteList); |
| | | } |
| | | } |
| | | |
| | | //所有动态帧 |
| | | public Dictionary<string, List<UnityEngine.Sprite>> GetAllFrame() |
| | | /// <summary> |
| | | /// 获取帧动画资源 |
| | | /// </summary> |
| | | public List<Sprite> GetDynamicImage(string key) |
| | | { |
| | | return allFrameDic; |
| | | // 按需加载资源 |
| | | if (!allFrameDic.ContainsKey(key)) |
| | | { |
| | | LoadFrameSprites(FaceConfig.Get(key)); |
| | | } |
| | | |
| | | if (allFrameDic.TryGetValue(key, out var list)) |
| | | { |
| | | return list; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | //只有聊天表情 |
| | | public List<string> GetAllFace() |
| | | { |
| | | return chatFaces; |
| | | } |
| | | |
| | | public List<UnityEngine.Sprite> GetDynamicImage(string key) |
| | | { |
| | | List<UnityEngine.Sprite> list = null; |
| | | allFrameDic.TryGetValue(key, out list); |
| | | return list; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 检查是否包含指定帧动画 |
| | | /// </summary> |
| | | public bool ContainsDynamicImage(string key) |
| | | { |
| | | return allFrameDic.ContainsKey(key); |
| | | return FaceConfig.HasKey(key); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 预加载指定帧动画 |
| | | /// </summary> |
| | | public void PreloadDynamicImage(string key) |
| | | { |
| | | if (!allFrameDic.ContainsKey(key)) |
| | | { |
| | | LoadFrameSprites(FaceConfig.Get(key)); |
| | | } |
| | | } |
| | | } |