- Home /
Set AsyncOperation.allowSceneActivation from another script ?
Hi guys ! I'd like to be able to delay the scene activation but I just can't get to call alloSceneActivation from another script :/
I guess I just don't know how to get a reference to the proper async operation that is waiting for activation here !
Any help would be great :D
Answer by ExtinctSpecie · Mar 20, 2017 at 03:07 PM
if you need any help leave a comment below so basically i have a separate scene (loading scene) which has the below script attached to it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadingSceneController : MonoBehaviour {
[Header("Loading Visuals")]
public Image background;
public Image progressBar;
public Image fadeOut;
public Text loadingText;
[Header("Timing Settings")]
public float waitOnLoadEnd;
[Header("Loading Settings")]
public ThreadPriority loadThreadPriority;
AsyncOperation operation;
public static int loadingSceneIndex = 1;
public static string sceneNameToLoad = "";
public static int sceneIndexToLoad = -1;
void Start()
{
GetComponent<AudioSource> ().volume = Volume_Controller.get_sfx_volume();
Debug.Log (sceneNameToLoad);
Debug.Log ("Loading scene is loaded");
StartCoroutine (CoLoadSceneAsyncByName ());
}
public static void LoadSceneByName(string sceneName)
{
Application.backgroundLoadingPriority = ThreadPriority.High;
SceneManager.LoadScene(loadingSceneIndex);
sceneNameToLoad = sceneName;
}
public static void LoadSceneByIndex(int sceneIndex)
{
Application.backgroundLoadingPriority = ThreadPriority.High;
SceneManager.LoadScene(loadingSceneIndex);
sceneIndexToLoad = sceneIndex;
}
IEnumerator CoLoadSceneAsyncByName()
{
InitializeVisuals ();
yield return null;
Application.backgroundLoadingPriority = loadThreadPriority;
operation = SceneManager.LoadSceneAsync (sceneNameToLoad);
while (!operation.isDone)
{
progressBar.fillAmount = operation.progress;
yield return null;
}
yield return new WaitForSeconds (waitOnLoadEnd);
LoadingDone ();
yield return new WaitForSeconds (0.1f);
operation.allowSceneActivation = true;
}
public void InitializeVisuals()
{
progressBar.fillAmount = 0f;
loadingText.text = "Loading...";
}
public void LoadingDone()
{
progressBar.fillAmount = 1f;
loadingText.text = "Loading Done.";
}
}
LoadSceneByIndex wont work it needs its own Co-Routine this will throw an error if you try to call it
This doesn't actually answer my problem :$
I want to be able to wait once LoadingDone(); and actually set allowsceneActivation to true FRO$$anonymous$$ ANOTHER SCRIPT. :/
so create a function and call it on button click or get access to the component(script) or create a listener public void ActivateScene() { operation.allowSceneActivation = true; }