UGUI-Unity2017生成圖集,加載圖集

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");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章