天天看點

[Unity] 檔案夾圖像資源的讀取

[Unity] 檔案夾圖像資源的讀取

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

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

    }

}

本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366360,如需轉載請自行聯系原作者

繼續閱讀