当前位置:   article > 正文

unity3d load 图片 加载不到的问题_unity加载网络图片异常

unity加载网络图片异常

根据自己的加载类型,加载方式略有不同,如下自己比对。

You can't read the Resources directory with the StreamReader or the File class. You must use Resources.Load.

1.The path is relative to any Resources folder inside the Assets folder of your project.

2.Do not include the file extension names such as .txt.png.mp3 in the path parameter.

3.Use forward slashes instead of back slashes when you have another folder inside the Resources folder. backslashes won't work.

Text files:

  1. TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
  2. string tileFile = txtAsset.text;

Supported TextAsset formats:

txt .html .htm .xml .bytes .json .csv .yaml .fnt

Sound files:

AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;

Image files:

Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;

Sprites - Single:

Image with Texture Type set to Sprite (2D and UI) and

Image with Sprite Mode set to Single.

Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;

Sprites - Multiple:

Image with Texture Type set to Sprite (2D and UI) and

Image with Sprite Mode set to Multiple.

Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];

Video files (Unity >= 5.6):

VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;

GameObject Prefab:

GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;

3D Mesh (such as FBX files)

Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;

3D Mesh (from GameObject Prefab)

  1. MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
  2. Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh

3D Model (as GameObject)

  1. GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
  2. //MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
  3. //Mesh loadedMesh = meshFilter.sharedMesh;
  4. GameObject object1 = Instantiate(loadedObj) as GameObject;

Accessing files in a sub-folder:

For example, if you have a shoot.mp3 file which is in a sub-folder called "Sound" that is placed in the Resources folder, you use the forward slash:

AudioClip audio = Resources.Load("Sound/shoot", typeof(AudioClip)) as AudioClip;

Asynchronous Loading:

  1. IEnumerator loadFromResourcesFolder()
  2. {
  3. //Request data to be loaded
  4. ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));
  5. //Wait till we are done loading
  6. while (!loadAsync.isDone)
  7. {
  8. Debug.Log("Load Progress: " + loadAsync.progress);
  9. yield return null;
  10. }
  11. //Get the loaded data
  12. GameObject prefab = loadAsync.asset as GameObject;
  13. }

To useStartCoroutine(loadFromResourcesFolder());

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/125249
推荐阅读
相关标签
  

闽ICP备14008679号