unity3d 動態加載紋理


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到底能否動態加載,稍后再做實驗


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM