La pregunta fué respondida y una respuesta ha sido aceptada
Load level async
Hi, I've seen this video: https://www.youtube.com/watch?v=YMj2qPq9CP8& And I would like, instead of changing the scene showing a load bar pressing a ui button, was with a trigger. I have created two scripts, one that shows in the above mentioned video and another one that activates the other script when passing through the trigger:
Script 1 (script of the video):
public void LoadLevel (int sceneIndex) { StartCoroutine(LoadAsynchronously(sceneIndex));
} IEnumerator LoadAsynchronously (int sceneIndex) { AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex); while (!operation.isDone) { Debug.Log(operation.progress); yield return null; } }
}
Script 2 (that activates the above script with the trigger):
public class "2nd script name" : "1st script name" {
void OnTriggerEnter () { LoadLevel(); } }
I have worked by putting the value of the sceneIndex inside the script, but I do not want to repeat the same script for each scene I will do, which I want from the inspector to be able to edit "int sceneIndex" but, I have not gotten the form to do it.
I hope someone can help me. Thank you very much.
Answer by Kaskorian · Jul 15, 2017 at 05:21 PM
I don't understand the last part of your question. you want to load async in OnTriggerEnter() and you want to edit the sceneIndex via Inspector.
Is that right?
Ok so here is a solution.
You can do it in one script. And you put the script on the game object with the trigger
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class LoadScene : $$anonymous$$onoBehaviour { public int sceneIndex;
void OnTriggerEnter(Collider other)
{
Debug.Log("Hallo");
StartCoroutine(LoadAsynchronously());
}
IEnumerator LoadAsynchronously()
{
AsyncOperation operation = Scene$$anonymous$$anager.LoadSceneAsync(sceneIndex);
while (!operation.isDone)
{
Debug.Log(operation.progress);
yield return null;
}
}
}
It works! Thank you very much. I did not expect it to be that easy.
and be careful. One of the objects has to have a Rigidbody Component otherwise, the collision is not detected.
Follow this Question
Related Questions
Load Scene in Background and Load When a Button is Clicked 0 Answers
Async Level loading issue. 0 Answers
How to properly call Async Task 0 Answers