Version: 2023.2

Application.backgroundLoadingPriority

切换到手册
public static ThreadPriority backgroundLoadingPriority ;

描述

后台加载线程的优先级。

可用于控制异步加载数据所需的时间以及在后台加载时对游戏的性能影响。

Asynchronous load functions that load objects (Resources.LoadAsync, AssetBundle.LoadAssetAsync, AssetBundle.LoadAllAssetAsync), scenes (SceneManager.LoadSceneAsync) do data read and deserialization on a separate background loading thread and object integration on a main thread. Integration depends on an object type and for textures, meshes means uploading data to the GPU, audio clips prepare data for playing.

为了避免帧率不稳,我们根据 backgroundLoadingPriority 值来限制主线程的集成时间:
- ThreadPriority.Low - 2ms
- ThreadPriority.BelowNormal - 4ms
- ThreadPriority.Normal - 10ms
- ThreadPriority.High - 50ms\ 这是主线程的单个帧内所有异步操作可耗费的最长时间。

后台加载线程直接使用 backgroundLoadingPriority

The Profiler marker Application.Integrate Assets in Background lets you optimize background loading.

using UnityEngine;

public class ExampleScript : MonoBehaviour { void Example1() { // Load as much data as possible, as a result frame rate will drop. // Good for fast loading when showing progress bars.

Application.backgroundLoadingPriority = ThreadPriority.High; }

void Example2() { // Load data very slowly and try not to affect performance of the game. // Good for loading in the background while the game is playing.

Application.backgroundLoadingPriority = ThreadPriority.Low; } }

另请参阅:ThreadPriority 枚举、AsyncOperation.priority