1.图文混排中的资源,主要是图片。
2.所谓的资源管理,可以分为资源对象池和资源加载这两部分。这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理。
RichTextResourceManager.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif public enum RichTextResourceType
{
Image,
} public class RichTextResourceManager : CSharpSingletion<RichTextResourceManager> { private Dictionary<RichTextResourceType, List<GameObject>> typeGoDic = new Dictionary<RichTextResourceType, List<GameObject>>();
private DefaultControls.Resources resources = new DefaultControls.Resources(); public void SetPoolObject(RichTextResourceType type, GameObject go)
{
if (!typeGoDic.ContainsKey(type))
{
List<GameObject> goList = new List<GameObject>();
goList.Add(go);
typeGoDic.Add(type, goList);
}
else
{
typeGoDic[type].Add(go);
}
go.SetActive(false);
} public GameObject GetPoolObject(RichTextResourceType type)
{
GameObject go = null;
if (typeGoDic.ContainsKey(type))
{
if (typeGoDic[type].Count > )
{
go = typeGoDic[type][];
if (go)
{
go.SetActive(true);
typeGoDic[type].Remove(go);
return go;
}
}
}
go = CreatePoolObject(type);
return go;
} private GameObject CreatePoolObject(RichTextResourceType type)
{
GameObject go = null;
if (type == RichTextResourceType.Image)
{
go = CreateImage();
}
return go;
} public void LoadSprite(string path, Action<Sprite> callback)
{
#if UNITY_EDITOR
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/" + path + ".png");
if ((sprite != null) && (callback != null))
{
callback(sprite);
}
#endif
} public void LoadSprite(string path, Action<Sprite[]> callback)
{
#if UNITY_EDITOR
Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath("Assets/" + path + ".png") as Sprite[];
if ((sprites != null) && (callback != null))
{
callback(sprites);
}
#endif
} public void SetSprite(string path, Image image)
{
LoadSprite(path, (Sprite sprite) => { image.sprite = sprite; });
} private GameObject CreateImage()
{
GameObject go = DefaultControls.CreateImage(resources);
go.layer = LayerMask.NameToLayer("UI");
Image image = go.GetComponent<Image>();
image.raycastTarget = false;
return go;
}
}