Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

AsyncOperation.priority

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public int priority;

Description

Integer representing the execution order priority of this AsyncOperation.

When multiple asynchronous operations are queued, the operation with the higher priority executes first. A higher integer value represents a higher priority. For example, asynchronous operations execute in the order 3, 2, 1 and so on. The default priority is 0. Once an operation has been started on the background thread, changing the priority has no effect.

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AsyncPriorityDemo : MonoBehaviour { [Header("Scene names to load (must be in Build Profile > Scene List)")] [SerializeField] private string highPriorityScene = "GameplayLevel"; [SerializeField] private string mediumPriorityScene = "Lobby"; [SerializeField] private string lowPriorityScene = "Credits";

private void Start() { StartCoroutine(LoadScenesWithPriorities()); }

private IEnumerator LoadScenesWithPriorities() { Debug.Log("Starting concurrent scene loads with different priorities...");

AsyncOperation highOp = SceneManager.LoadSceneAsync(highPriorityScene, LoadSceneMode.Additive); AsyncOperation mediumOp = SceneManager.LoadSceneAsync(mediumPriorityScene, LoadSceneMode.Additive); AsyncOperation lowOp = SceneManager.LoadSceneAsync(lowPriorityScene, LoadSceneMode.Additive);

// Assign priorities. A higher value means the operation is given more // processing time relative to other running async operations, so it // tends to complete sooner. allowSceneActivation is left enabled (the default), // so each scene activates as soon as its load finishes. highOp.priority = 3; // Most urgent. mediumOp.priority = 1; // Normal urgency. lowOp.priority = 0; // Least urgent.

Debug.Log($"Priorities assigned -> " + $"{highPriorityScene}:{highOp.priority}, " + $"{mediumPriorityScene}:{mediumOp.priority}, " + $"{lowPriorityScene}:{lowOp.priority}");

while (!highOp.isDone || !mediumOp.isDone || !lowOp.isDone) { Debug.Log($"Progress -> " + $"High: {highOp.progress:P0}, " + $"Medium: {mediumOp.progress:P0}, " + $"Low: {lowOp.progress:P0}"); yield return null; }

Debug.Log("All scenes loaded. Load sequence complete."); } }