Unity2017及以上版本生成图集
加载图集
public class UnitySingleton<T> : MonoBehaviour where T:Component {
private static T _instance;
public static T GetInstance()
{
if(_instance == null)
{
//如果没有类型为T的类对象
_instance = FindObjectOfType(typeof(T)) as T;
if(_instance == null)
{
GameObject tempObject = new GameObject ();
tempObject.hideFlags = HideFlags.HideAndDontSave;
_instance = (T) tempObject.AddComponent(typeof(T));
Object.DontDestroyOnLoad(tempObject);
}
}
return _instance;
}
}
public class ResourceLoadManager:UnitySingleton<ResourceLoadManager>{
private SpriteAtlas m_spriteAtlas;
private T LoadResouceOfType<T>(string _resPath) where T:Object
{
T tempResource = null;
tempResource = Resources.Load<T>(_resPath);
return tempResource;
}
public Sprite LoadSprite(string _atlasName,string _spriteName)
{
Sprite tempSprite = null;
if(m_spriteAtlas != null)
{
tempSprite = m_spriteAtlas.GetSprite(_spriteName);
}else
{
m_spriteAtlas = LoadResouceOfType<SpriteAtlas>(_atlasName);
tempSprite = m_spriteAtlas.GetSprite(_spriteName);
}
return tempSprite;
}
}
使用:加载Resources文件夹下名叫"SpriteAtlas_UI"的图集,取出其中叫"common_icon_dkw"的图片。
Sprite tempNormalSprite = ResourceLoadManager.GetInstance().LoadSprite("SpriteAtlas_UI","common_icon_dkw");