IEnumerator LoadPhoto(){ string path =Application.dataPath+"/Resources/"; string[] strs = System.IO.Directory.GetFiles(path); foreach (string file in strs) { System.IO.FileInfo fi = new System.IO.FileInfo(file); if (fi.Extension == ".png") { string fileName = fi.Name.Substring(0,fi.Name.IndexOf('.')); if(!images.ContainsKey(fileName)) { WWW www = new WWW("file://"+path+fi.Name); yield return www.isDone; //Texture2D t2d = Resources.Load("Photos/"+fileName,typeof(Texture2D)) as Texture2D; Texture2D t2d = www.texture; iGUIImage iImage = listBox4.addElement<iGUIImage>(); iImage.image = t2d; iImage.name = fileName; iImage.setWidth(0.24f); iImage.clickCallback = on_ImageClick; images.Add(iImage.name,iImage); Debug.Log("Create name:"+iImage.name); Debug.Log("file name:"+iImage.name); Debug.Log("images count:"+images.Count); } } } }
先用了Resource.Load加載紋理,測試結果是在場景載入的時候可以正確加載,但是在場景運行過程中不能動態加載新增加的資源,后改為WWW加載解決問題。
Resource到底能否動態加載,稍后再做實驗