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到底能否动态加载,稍后再做实验

原文地址:https://www.cnblogs.com/88999660/p/2869845.html