Resources resource synchronous loading and asynchronous loading

Resources resources are loaded synchronously

The role of dynamic loading of Resources resources

1. Dynamically load the specified path resources under the Resources folder through code
2. Avoid cumbersome dragging operations

Common resource types

预设体对象——GameObject
 音效文件——AudioClip
 文本文件——TextAsset
 图片文件——Texture
 其它类型——需要什么用什么类型
  注意:预设体对象加载需要实例化,其它资源加载一般直接用。

Resource synchronous loading common method

Use the Resources.Load() method to load resources under the Resources folder, where the parameter is the resource name (if there are folders under Resources, use "/" to separate the file name and resource name) Note: In a project,
Resources There can be multiple folders. When loading through the API, it will go to these resources folders with the same name to find resources. When packaging, the contents of the Resources folder will be packaged together

(1) Load the prefab object
Step 1: Load the prefab resource file (essentially load the configuration data of the prefab into the memory)
Object obj = Resources.Load("Cube");
Second Step: If you want to create this prefab on the scene, you need to instantiate
Instantiate(obj) after loading;

(2) Load audio resources
Step 1: Load data

Object BGM = Resources.Load("Music/BackgroundMusic");//Music是文件夹名

Step 2: To use audio data, we don't need to instantiate the loaded AudioClip, we just need to assign the data to the corresponding AudioSource music player component.

audioSource.clip = BGM as AudioClip; 
 audioSource.Play();

(3) Load text resources
Resources.Load can load text file formats: .txt, xml, bytes, json, html, csv
Load data:
TextAsset ta = Resources.Load("Txt/Test") as TextAsset;
get text content ta.text
get text byte array ta.bytes

(4) What to do if the resource has the same name?
When Resources.Load loads a resource with the same name, it cannot accurately load the content you want, but you can use the second parameter to specify the type of resource, such as

		tex = Resources.Load("Tex/TestJPG", typeof(Texture)) as Texture;//加载名为TestJPG的图片资源
		
		ta = Resources.Load("Tex/TestJPG", typeof(TextAsset)) as TextAsset; /加载名为TestJPG的文本资源

(5) Load all resources with the specified name

        Object[] objs = Resources.LoadAll("Tex/TestJPG");

Resource synchronous loading generic method

In the previous content, the resource types loaded by Resources.Load are all Object types, and you need to use as to perform forced conversion when using them.
This way is clumsy. We can use the generic synchronous loading method to specify the type of loaded resource with generic parameters when the method is called.

  		 TextAsset ta2 = Resources.Load<TextAsset>("Tex/TestJPG");

The above code loads the resource TestJPG whose type is TextAsset. The generic program will help us find the resource of the specified type, and the returned data is also the type you specified. This saves the step of casting with as. So this method will be more commonly used.

Resources are loaded asynchronously

What is Resources asynchronous loading,

In the synchronous loading in the previous section, if we load too large resources, it may cause the program to freeze. The reason for the lag is that reading data from the hard disk into the memory requires calculation. The larger the resource, the longer it will take, which will cause frame drops and freezes.
Asynchronous loading of Resources means opening a new thread internally to load resources without causing the main thread to freeze.

Resources asynchronous loading method

Note: Resources that cannot be loaded immediately after asynchronous loading must wait at least one frame.
There are two methods for asynchronous loading:
(1) Use the loaded resources by listening to the completion event in asynchronous loading
insert image description here

private void LoadOver(AsyncOperation rq){
	print("加载结束");
	//asset是资源对象 加载完毕国有就能够得到它
	tex=(rq as ResourcesRequest).asset as Texture;
}

(2) Use loaded resources through coroutines

void Start(){
	StartCoroution(Load());
}

insert image description here
Using the coroutine function to load resources can do some processing on resource loading (such as displaying the progress of loading, etc.) in each frame of the loading process.
insert image description here

Advantages and disadvantages of the two asynchronous loading methods
1. Completion of event monitoring asynchronous loading
Benefits: Simple writing
Disadvantages: Only after the end of resource loading, it can be processed
, similar to linear loading

 2.协程异步加载
    好处:可以在协程中处理复杂逻辑,比如同时加载多个资源,比如进度条更新
    坏处:写法稍麻烦
    类似于并行加载

Resource unloading

Will Resources waste memory by repeatedly loading resources?

In fact, after Resources loads a resource, the resource is always stored in memory as a cache. When loading for the second time, it is found that the resource exists in the cache, and it will be taken out directly for use. So, repeated loading multiple times will not waste memory. But it will waste performance (every time it is loaded, it will be searched and retrieved, always accompanied by some performance consumption).

How to manually release resources in the cache

(1) Unload the specified resource:
Resources.UnloadAsset method
Note:
This method cannot release the GameObject object. Because it will be used to instantiate the object.
It can only be used for some content that does not need to be instantiated. Such as pictures, sound effects, text, etc.
Under normal circumstances, we rarely use it alone.
(2) Unload unused resources
Resources.UnloadUnusedAssets
Note: Generally, use Resources.UnloadUnusedAssets()
together with GC when passing the scene ; hands-on practice: Ctrl+7 to open the Unity debugging panel, check the memory size after resource loading and resource unloading memory size.

Guess you like

Origin blog.csdn.net/shadowsghost/article/details/130156148