lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  201 随笔 :: 0 文章 :: 12 评论 :: 21万 阅读

1.图文混排中的资源,主要是图片。

2.所谓的资源管理,可以分为资源对象池和资源加载这两部分。这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理。

 

RichTextResourceManager.cs

复制代码
 1 using UnityEngine;
 2 using System.Collections.Generic;
 3 using UnityEngine.UI;
 4 using System;
 5 #if UNITY_EDITOR
 6 using UnityEditor;
 7 #endif
 8 
 9 public enum RichTextResourceType
10 {
11     Image,
12 }
13 
14 public class RichTextResourceManager : CSharpSingletion<RichTextResourceManager> {
15 
16     private Dictionary<RichTextResourceType, List<GameObject>> typeGoDic = new Dictionary<RichTextResourceType, List<GameObject>>();
17     private DefaultControls.Resources resources = new DefaultControls.Resources();
18 
19     public void SetPoolObject(RichTextResourceType type, GameObject go)
20     {
21         if (!typeGoDic.ContainsKey(type))
22         {
23             List<GameObject> goList = new List<GameObject>();
24             goList.Add(go);
25             typeGoDic.Add(type, goList);
26         }
27         else
28         {
29             typeGoDic[type].Add(go);
30         }
31         go.SetActive(false);
32     }
33 
34     public GameObject GetPoolObject(RichTextResourceType type)
35     {
36         GameObject go = null;
37         if (typeGoDic.ContainsKey(type))
38         {
39             if (typeGoDic[type].Count > 0)
40             {
41                 go = typeGoDic[type][0];
42                 if (go)
43                 {
44                     go.SetActive(true);
45                     typeGoDic[type].Remove(go);
46                     return go;
47                 }
48             }
49         }
50         go = CreatePoolObject(type);
51         return go;
52     }
53 
54     private GameObject CreatePoolObject(RichTextResourceType type)
55     {
56         GameObject go = null;
57         if (type == RichTextResourceType.Image)
58         {
59             go = CreateImage();
60         }
61         return go;
62     }
63 
64     public void LoadSprite(string path, Action<Sprite> callback)
65     {
66 #if UNITY_EDITOR
67         Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/" + path + ".png");
68         if ((sprite != null) && (callback != null))
69         {
70             callback(sprite);
71         }
72 #endif
73     }
74 
75     public void LoadSprite(string path, Action<Sprite[]> callback)
76     {
77 #if UNITY_EDITOR
78         Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath("Assets/" + path + ".png") as Sprite[];
79         if ((sprites != null) && (callback != null))
80         {
81             callback(sprites);
82         }
83 #endif
84     }
85 
86     public void SetSprite(string path, Image image)
87     {
88         LoadSprite(path, (Sprite sprite) => { image.sprite = sprite; });
89     }
90 
91     private GameObject CreateImage()
92     {
93         GameObject go = DefaultControls.CreateImage(resources);
94         go.layer = LayerMask.NameToLayer("UI");
95         Image image = go.GetComponent<Image>();
96         image.raycastTarget = false;
97         return go;
98     }
99 }
复制代码

 

posted on   艰苦奋斗中  阅读(1013)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示