A custom preloader in Unity part 2/3

The second way to do it resembles very much how it works in flash: first you load a small movie, which is just the preloader, which will load the big movie.

A completely empty unity scene compiles to about a 7kB .unity3d file. Very small and very nice for making a preloader. If you’re preloader gets larger, you might wanna consider having a look at part 1 of this tutorial to skin it with a back color and logo matching your website.

So, to use this method, I am assuming you have a working project, say myProject.unity3d which is 15MB in size. Now, make a new unity scene, create en empty game object named “application” and attach a behaviour script to it named “main.cs” or “main.js”. (Or whatever language you prefer). This scene will need to have some sort of animation to show the loading progress. In this example I will be fading a GUITexture from 0 to 100% opacity.

Here’s what it could look like:

public var fileUrl = "http://www.unity3d.com/webplayers/Lightning/lightning.unity3d";
public var progressImage:GUITexture;
 
function Start ()
{
	progressImage.color = new Color(.5, .5, .5, 0);
 
	var stream = new WWW (fileUrl);
	while (!stream.isDone)
	{
		progressImage.color = new Color(.5, .5, .5, stream.progress * .5);
		yield;
	}
	stream.LoadUnityWeb();
}

I used it for this preloader (shown when you click the image).

If you want to reuse the same preloader, you can choose to simply pass the name of the main unity file you wish to load as a parameter. The html embed will look something like this:

<param name="src" value="unity/preloader.unity3d?file=unity/main.unity3d" />

And your unity file will have a bit of code like this (in C# this time):

string[] url = Application.srcValue.Split('?');
if (url.Length > 1) {
   string[] args = url[1].Split('&');
   foreach (string arg in args) {
     string[] keyvalue = arg.Split('=');
     if (keyvalue[0] == "file") fileUrl = keyvalue[1];
   }
}

Tags: , , , , ,

Leave a Reply

Security Code: