Adventure Creator Wikia
Advertisement

As of v1.76.1, it is possible to delay the loading of a scene, to allow for e.g. additional data to be downloaded beforehand.

This script demonstrates this by having scenes be dependent on Asset Bundles.  If a scene is marked as requiring an Asset Bundle, then that bundle will be loaded before the scene is loaded.

To use it:

  1. Create one or more Asset Bundles and place them in an Asset folder named StreamingAssets.
  2. Create a new C# file named SceneDelayExample, and paste in the code below.
  3. Locate AC's PersistentEngine prefab inside Assets/AdventureCreator/Resources, and attach the new Scene Delay Example component.
  4. Inside the component's Inspector, add the name of any Asset Bundle as well as the scenes that depend on it.

SceneDelayExample.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using AC;

public class SceneDelayExample : MonoBehaviour
{

    #region Variables

	[SerializeField] private SceneDataSource[] sceneDataSources = new SceneDataSource[0];
	private readonly HashSet<string> loadedBundles = new HashSet<string> ();

	#endregion


	#region UnityStandards

	private void OnEnable ()
	{
		EventManager.OnDelayChangeScene += OnDelayChangeScene;
	}


	private void OnDisable ()
	{
		EventManager.OnDelayChangeScene -= OnDelayChangeScene;
	}

	#endregion


	#region CustomEvents

	private void OnDelayChangeScene (SceneInfo sceneInfo, System.Action callback)
	{
		string sceneName = sceneInfo.Filename;
		if (string.IsNullOrEmpty (sceneName))
		{
			callback.Invoke ();
			return;
		}

		string assetBundle = GetBundle (sceneName);
		if (string.IsNullOrEmpty (assetBundle) || loadedBundles.Contains (assetBundle))
		{
			callback.Invoke ();
			return;
		}

		StartCoroutine (LoadBundle (assetBundle, callback));
	}


	private IEnumerator LoadBundle (string assetBundle, System.Action callback)
	{
		string bundlePath = Path.Combine (Application.streamingAssetsPath, assetBundle);
		var bundleLoadRequest = AssetBundle.LoadFromFileAsync (bundlePath);

		yield return bundleLoadRequest;

		loadedBundles.Add (assetBundle);
		callback?.Invoke ();
	}


	private string GetBundle (string sceneName)
	{
		foreach (SceneDataSource sceneDataSource in sceneDataSources)
		{
			string assetBundle = sceneDataSource.GetBundle (sceneName);
			if (!string.IsNullOrEmpty (assetBundle))
			{
				return assetBundle;
			}
		}
		return string.Empty;
	}

	#endregion


	#region PrivateClasses

	[Serializable]
	private class SceneDataSource
	{
		
		[SerializeField] private string assetBundle;
		[SerializeField] private string[] sceneNames = new string[0];


		public string GetBundle (string sceneName)
		{
			foreach (string scene in sceneNames)
			{
				if (scene == sceneName)
				{
					return assetBundle;
				}
			}
			return string.Empty;
		}

	}

	#endregion

}
Advertisement