preface :
What's the most annoying thing about playing games ? Of course, the game loading interface
* But you know what , Loading interface is indispensable in game making , It can make our scene completely loaded before entering the game , Make sure that the user does not get stuck loading .
How to realize a simple loading interface ? Come and learn from me
design sketch
UI establish :
First, create a start interface , by Button Button to add events to complete the scene conversion :
Then create a loading interface UI, The first thing you need is a game progress bar , It can be used directly Slider To complete the creation , his UI The hierarchical relationship is :
Its element action is :
* Background: That is, the background of the whole progress bar is brown
* Fill: Is the filling of the loaded progress bar , This is the blue area
* Hander: It's the white square in the middle , Is the critical point of loading
The above three elements can be changed by modifying the Image Component to modify the shape , and Hander The location of this is through the Slider
Interface on Value To do it , Again, we can call it in the script API:
Secondly , You need to add a Text Text box , To output the loading percentage
Asynchronous loading scripting :
To implement asynchronous scene loading, you need to AsyncOperation To do it , Check official documents , It can be found that AsyncOperation
It's a class , Its basic attributes are as follows ( Add black for this use ):
* allowSceneActivation: After loading , Allow scene jump
* isDone: Is the scene loaded
* priority: You can adjust the priority of asynchronous loading
* progress: Scene loading progress , The value is from 0 reach 1
We know that the direct conversion of the scene is through the SceneManager.LoadScene() To jump directly , For asynchronous loading, the jump of the scene is through the
SceneManager.LoadSceneAsync() To do it .
The difference between the two :
see Unity Official Handbook
And in order to make sure that the scene is not loaded completely , No scene jump , You need to use a coroutine to control its transformation :
public GameObject image; // Loading interface public Slider slider; // progress bar public Text text;
// Load progress text public void LoadNextLeaver() { image.SetActive(true); StartCoroutine(
LoadLeaver()); } IEnumerator LoadLeaver() { AsyncOperation operation =
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
// Get the current scene and add one //operation.allowSceneActivation = false; while(!operation.isDone)
// When the scene is not loaded { slider.value = operation.progress; // The progress bar corresponds to the scene loading progress text.text = (
operation.progress * 100).ToString()+"%"; yield return null; } }
complete , Seeking the third company !
Technology