溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[Unity] 文件夾圖像資源的讀取

發(fā)布時間:2020-06-28 10:19:15 來源:網(wǎng)絡(luò) 閱讀:377 作者:蓬萊仙羽 欄目:開發(fā)技術(shù)

[Unity] 文件夾圖像資源的讀取

注意文件以及文件夾必須寄宿在Resources目錄下,才能順利調(diào)用Resources.Load()和Resources.loadAll()這兩個函數(shù)得到所需要的圖像文件。

 

 

public class GUITest : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }


    private Texture2D texSingle;
    private Texture2D[] texAll;

    void OnGUI()
    {

        if (GUI.Button(new Rect(0,10,100,50),"加載一張貼圖"))
        {
            if (texSingle==null)
            {
                texSingle = Resources.Load("single/0") as Texture2D;   //這里不需要加后綴
            }
           
        }
        if (GUI.Button(new Rect(0,130,100,50),"加載一組貼圖"))
        {
            if (texAll==null)
            {                
                var textures = Resources.LoadAll("textures");
                int countAll=textures.Length;
                texAll=new Texture2D[countAll];
                for (int i = 0; i < countAll; i++)
                {
                    texAll[i] = textures[i] as Texture2D;
                }

            }
        }

        //繪制貼圖
        if (texSingle!=null)
        {
            GUI.DrawTexture(new Rect(110,10,80,80),texSingle,ScaleMode.ScaleToFit,true,0);
        }
        if (texAll!=null)
        {
            int countOfAll = texAll.Length;
            for (int i = 0; i < countOfAll; i++)
            {
                GUI.DrawTexture(new Rect(110+i*80,130,80,80),texAll[i],ScaleMode.ScaleToFit,true,0);
            }
        }
    }
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

un ty
AI