You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
111 lines
3.1 KiB
11 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
|
/**
|
||
|
* Helper component for loading a new scene.
|
||
|
* A fullscreen loading image is displayed while loading the new scene.
|
||
|
* All Rooms are destroyed and unused assets are released from memory before loading the new scene to minimize memory footprint.
|
||
|
* For streaming Web Player builds, the loading image will be displayed until the requested level has finished downloading.
|
||
|
*/
|
||
|
public class SceneLoader : MonoBehaviour
|
||
|
{
|
||
10 years ago
|
protected Texture2D loadingTexture;
|
||
|
protected string sceneToLoad;
|
||
|
protected bool displayedImage;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Asynchronously load a new scene.
|
||
|
* @param _sceneToLoad The name of the scene to load. Scenes must be added in project build settings.
|
||
|
* @param _loadingTexture Loading image to display while loading the new scene.
|
||
|
*/
|
||
10 years ago
|
static public void LoadScene(string _sceneToLoad, Texture2D _loadingTexture)
|
||
11 years ago
|
{
|
||
|
// Unity does not provide a way to check if the named scene actually exists in the project.
|
||
11 years ago
|
GameObject go = new GameObject("SceneLoader");
|
||
11 years ago
|
DontDestroyOnLoad(go);
|
||
|
|
||
|
SceneLoader sceneLoader = go.AddComponent<SceneLoader>();
|
||
|
sceneLoader.sceneToLoad = _sceneToLoad;
|
||
|
sceneLoader.loadingTexture = _loadingTexture;
|
||
|
}
|
||
|
|
||
10 years ago
|
protected virtual void Start()
|
||
11 years ago
|
{
|
||
10 years ago
|
StartCoroutine(DoLoadBlock());
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
IEnumerator DoLoadBlock()
|
||
11 years ago
|
{
|
||
|
// Wait until loading image has been displayed in OnGUI
|
||
|
while (loadingTexture != null &&
|
||
|
!displayedImage)
|
||
|
{
|
||
|
yield return new WaitForEndOfFrame();
|
||
|
}
|
||
|
|
||
10 years ago
|
// Sprites tend to take up most of the memory in a Fungus game, so destroy all sprite objects
|
||
|
// first to free up memory for loading in the next scene.
|
||
|
SpriteRenderer[] renderers = GameObject.FindObjectsOfType<SpriteRenderer>();
|
||
|
foreach (SpriteRenderer renderer in renderers)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (renderer != null)
|
||
|
{
|
||
|
DestroyImmediate(renderer.gameObject);
|
||
|
}
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Wait for objects to actually be destroyed at end of run loop
|
||
11 years ago
|
yield return new WaitForEndOfFrame();
|
||
|
|
||
11 years ago
|
// All Room assets should no longer be referenced now, so unload them.
|
||
|
yield return Resources.UnloadUnusedAssets();
|
||
11 years ago
|
|
||
|
// Wait until scene has finished downloading (WebPlayer only)
|
||
|
while (!Application.CanStreamedLevelBeLoaded(sceneToLoad))
|
||
|
{
|
||
|
yield return new WaitForEndOfFrame();
|
||
|
}
|
||
|
|
||
|
// Load the scene (happens at end of frame)
|
||
|
Application.LoadLevel(sceneToLoad);
|
||
|
|
||
|
yield return new WaitForEndOfFrame();
|
||
|
|
||
|
// Clean up any remaining unused assets
|
||
|
Resources.UnloadUnusedAssets();
|
||
|
|
||
|
// We're now finished with the SceneLoader
|
||
|
Destroy(gameObject);
|
||
|
}
|
||
|
|
||
10 years ago
|
protected virtual void OnGUI()
|
||
11 years ago
|
{
|
||
|
if (loadingTexture == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
GUI.depth = -2000;
|
||
|
|
||
|
float h = Screen.height;
|
||
|
float w = (float)loadingTexture.width * (h / (float)loadingTexture.height);
|
||
|
|
||
|
float x = Screen.width / 2 - w / 2;
|
||
|
float y = 0;
|
||
|
|
||
|
Rect rect = new Rect(x, y, w, h);
|
||
|
|
||
|
GUI.DrawTexture(rect, loadingTexture);
|
||
|
|
||
|
if (Event.current.type == EventType.Repaint)
|
||
|
{
|
||
|
// Flag that image is now being shown
|
||
|
displayedImage = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|