Unity3D resource loading Resources

                            Unity3D resource loading Resources


table of Contents

1. Blog introduction

2. Content

3. Push

4. Conclusion


1. Blog introduction

This blog introduces the resource loading class Resources


2. Content

FindObjectsOfTypeAll Returns a list of objects of this type
Load Load a path of resources from the Resources folder
LoadAll Load all resources under a path from the Resources folder
LoadAsync Asynchronously load resources in the path from the Resources folder
UnloadAsset Unload resources from memory
UnloadUnusedAssets Uninstall unused resources

FindObjectsOfTypeAll : The objects to be searched include the objects in the scene directory and the objects in the resource list. In the following demonstration, I output all objects with the Test.cs script. I mount the Test under the Main Camera and make the Main Camera into a prefab. Body, cut off the connection of the prefab in the scene, and modify the name, and both objects are output.

Load : load is very simple, you can load it by specifying a path under the Resources directory, and you can also specify the type of loaded resource when loading. The writing method is different.

	        var cube = Resources.Load("Prefab/Cube");

		GameObject cube = Resources.Load<GameObject>("Prefab/Cube");
		
		GameObject cube = Resources.Load("Prefab/Cube") as GameObject;
		
		Instantiate(cube);

LoadAll : Load all objects in the specified directory, you can specify the type to load

	        //加载Prefab目录下的所有对象
		var prefabs = Resources.LoadAll("Prefab");
		
		//加载Prefab目录下的所有GameObject对象
		GameObject[] prefabs1 = Resources.LoadAll<GameObject>("Prefab");

LoadAsync : Load resources asynchronously, load resources through coroutines, and return a ResourcesRequest, and the loaded resources are in the asset.

	
	StartCoroutine(LoadPrefab());

	IEnumerator LoadPrefab()
	{
		ResourceRequest request = Resources.LoadAsync("Prefab/Cube");

		yield return request;
		
		Instantiate(request.asset);
	}

UnloadAsset : directly unload the resources loaded in the memory.

Tested it, it's useless. . . . . . . . . . .

UnloadUnusedAssets : Unload all unused resources in memory

The test is very easy to use

Resources.UnloadUnusedAssets();

3. Push

Blogger Github:  https://github.com/KingSun5


4. Conclusion

       If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/94147069