- Home /
Preloader for standalone
is there a way to make a preloader for a standalone build for windows, all the solutions i see are for web players
Answer by flamy · Feb 06, 2012 at 04:10 PM
public int numberOfObjectsToLoad=4000;
public int objectLoaded=0;
public Texture tex;
public bool nextLoad=true;
// Use this for initialization
void Start () {
InvokeRepeating("nextLoadReset",0,0.1f);
StartCoroutine("Load");
}
void OnGUI()
{
float temp=objectLoaded; // using temp variable because of some issues with accessing a variable from thread
print((temp/numberOfObjectsToLoad));
GUI.DrawTexture(new Rect(50,400,(float)500*(temp/numberOfObjectsToLoad)/* 500 is the width and the next calc is the percentage*/,50),tex);
}
void nextLoadReset()
{
nextLoad=true;
}
public IEnumerator Load()
{
for(int i=0;i<numberOfObjectsToLoad;i++)
{
yield return nextLoad; //instead do your loading here and btw using nextload for wait because wait for seconds wont work in thread (corotine)
objectLoaded++;
nextLoad=false;
}
}
this is just a sample of how you can do loading ! instead of using thread you can use different game states and check too but i prefer doin like this because it is simple...
if you are loading a 3d world make the renderer of all assets to false at the beginning (until everything else is loaded) Hope the code is clear !!
I just want to post that you can't use a Thread to load assets since Unity isn't thread-safe, but now you posted a coroutine solution with is of course the only way to reduce loading spikes. I wouldn't call it a preloader. It's just a load distributer. Unity loads all assets within a scene at once. All you can do is to use prefabs you instantiate one by one, but anyway all the prefabs are loaded at start.
Btw. i don't get the "nextLoad" stuff... There's no difference in yielding true or false. Both will wait exactly one frame. Why do you think that WaitForSeconds doesn't work in coroutines?
Finally: coroutines != thread
Coroutines are a kind of cooperative multitasking and not preemptive like threads.
wait for seconds is my bad i did a typo whn i coded it seems and checked the documentation (dang forgot default will be in js :P) and ya i know the difference between Coroutines and Thread.
and from his reply it seems like he needs a "loading bar" or progress bar so i thought this would be fine!! and obviously you got to do this with prefabs. It doesn't matter if prefabs are loaded at the beginning, but anyway copies could be created using this function, still it isn't too bad if you have to instantiate 100's of copies for each prefab!!. And pretty sure the reason he wants to go for a progress bar is because he gets stuck at splash screen for sometime (when actually unity loads the level), this would certainly help clearing that issue to an extend!!
Answer by Bunny83 · Feb 06, 2012 at 01:24 PM
That doesn't make much sense. In a standalone build all data is available immediately. Preloader just make sense if you have to load the data from a slow connection. Even in Flash a preloader doesn't make sense if you run the swf-file local
may be if he has numerous assets (like really lot!) he can use a thread to load it and give the user update on the loading bar!!
yes i have a lots of gameobjects that's why i need a progress bar