Unity 3D – Loading and Downloading Screen – Basic
Loading Screen
How to create a simple Loading Screen and using it with the Web Player
Create e scenes:
– level2 (it is the second scene)
– Main Camera
– Plane
-> attach a 4 MPix Texture
-> select the Texture> Inspector> Override for Web>
– Max Size 4096
– Format True Color
(it will be 20 MB into final build)
– level1 (it is the first scene)
– Main Camera
-> attach ‘GameController.js’
– Plane
-> attach a small Texture with a loading message
-> attach ‘LoadingScreen.js’
– GUI Text
-> attach ‘DownloadingText.js’
DownloadingText.js
#pragma strict
// Attach this script to a GuiText object
// Print on a guiText how much has been streamed level at index 1
// When finished streaming, print "Level 1 has been fully streamed!"
var percentageLoaded : float = 0;
function Start () {
}// ENd Start()
function Update() {
if(Application.GetStreamProgressForLevel(1) == 1) {
guiText.text = "Level at index 1 has been fully streamed!";
} else {
percentageLoaded = Application.GetStreamProgressForLevel(1) * 100;
guiText.text = percentageLoaded.ToString();// it displays: 54.569856... 100, you can use it to drive a progress bar!
}
}// END Update
GameController.js:
#pragma strict
function Start () {
}// END Start
function Update () {
}// END Update()
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "Load Next Level")) {
Application.LoadLevel ("level2");
}
}// END OnGUI()
LoadingScreen.js
#pragma strict
function Start () {
// Hide the object
renderer.enabled = false;
}// END START
function Update () {
// It checks if the application is loading a level. If yes, then show the splash screen.
if(Application.isLoadingLevel)
Show();
}// END Update
function Show () {
// Show the object
renderer.enabled = true;
}// END Show()
MAIN TOP MENU> File> Build Settings>
– Scenes In Build> Add first level1.unity (0), then level2.unity(1)
– Platform Web Player
– Web Player> Streamed
Build, upload, play.
How does it work?
Really easy!
0. Watch the GUIText, with the percentage of download, you will see that web player is downloading level2.unity in background.
1. When the game starts the web player downloads level1.unity, after level2.unity and so on…
2. Press “Load Next Level”
3. NOTHING HAPPENS if the download of level2.unity is not complete.
4. ONLY when the download of level2.unity is completed, web player loading level2.unity scene
5. LoadingScreen.js checks if the application is loading a level. If yes, then show the splash screen.
Downloading Screen
For italian people:
‘Application.isLoadingLevel’ sarà TRUE solo nel momento in cui il livello sarà completamente scaricato e sarà iniziato il suo effettivo caricamento.
‘Application.isLoadingLevel’ NON ‘sente’ la fase di download della scena.
Per creare un loader durante lo streaming, prima dell’avvio dell’istruzione ‘Application.LoadLevel’ visualizzare una schermata di loading nella scena corrente. La procedura sarà:
1. Il livello1 viene terminato
2. Visualizzare una schermata per il download level2…
3. Avviare ‘Application.LoadLevel’ per iniziare il caricamento del livello2
4. Una volta scaricato completamente il livello2, inizierà il processo di caricamento del livello2
5. ‘Application.isLoadingLevel’ visualizzerà una schermata per il loading level2…
6. Il livello2 sarà avviato
Best streaming practice
50 KB display the logo and menu (4 seconds)
320 KB let the user play a tiny tutorial level or let him do some fun interaction in the menu (20 seconds)
800 KB let the user play the first small level (50 seconds)
Finish downloading the entire game within 1-5 MB (1-5 minutes)
Externalize the music and load it via the WWW class
Loads the level asynchronously in the background
Unity will completely load all assets and all objects in the scene in a background loading thread. You can create a completely streaming world where you constantly load and unload different parts of the world based on the player position.
Sample Code:
function Start () {
// Load the level named "MyBigLevel".
var async : AsyncOperation = Application.LoadLevelAsync ("MyBigLevel");
yield async;
Debug.Log ("Loading complete");
}
Reference: http://docs.unity3d.com/ScriptReference/Application.LoadLevelAsync.html