- Home /
Animated GUI.DrawTexture while game is loading
Hi,
I want my game to display a screen with a simple animation while it loads. It's a full screen animation made with 4 frames, in loop. I want this animation to show for at least 30 seconds even if the following scene loads faster than that (the animation contains some text to be read). It has to be full screen in whichever screen resolution, with a fixed aspect ratio, without being cropped.
I did it with a single image, but being 30 seconds with no movement or change it seems that the game crashed or something, so now I want to add other 3 images so every 0.5 seconds a different one is shown in a sequence (all of them are the same image with the addition of a small drawing that mutates a bit on each frame). Here's the code that I'm using (C#), but my coding skills are very limited and have no idea how to implement the solutions that I've found online. Any help would be appreciated. Thanks.
using UnityEngine;
using System.Collections;
public class creditos : MonoBehaviour {
private float ContadorSaltar;
private float TiempoEstancia = 30.0f;
public GUIStyle estiloNegro;
public Texture2D[] fondos;
// Update is called once per frame
void Update () {
ContadorSaltar += 1 * Time.deltaTime;
if ( ContadorSaltar >= TiempoEstancia ){
Application.LoadLevel("Scene1");
}
}
// Update is called once per frame
void OnGUI () {
float anchura = Screen.width / 1.8f;
GUI.Button(new Rect(0,0,Screen.width, Screen.height),"",estiloNegro);
GUI.DrawTexture(new Rect(0,Screen.height / 2.0f - anchura / 2.0f,Screen.width,anchura),fondos[0]);
}
}
After some more research, I think that one approach in pseudocode would be something like:
Display a scene that contains the animation, start a counter in seconds, switch frames every 0.5 seconds
Load the following scene asynchronously and wait. $$anonymous$$eanwhile, continue to display the animation until the counter reaches 30 seconds
Then, jump to the following scene
Would anyone help me to translate this to actual code to achieve that please? Thanks.
Answer by phxvyper · May 11, 2015 at 03:40 AM
You can use a coroutine with this function:
http://docs.unity3d.com/ScriptReference/Application.LoadLevelAsync.html
Essentially, just pause your current scene and begin the 30-second loading sceen, or load your loading screen scene. Then begin the coroutine with that LoadLevelAsync function, and check to see if the AsyncOperation.isDone property that is returned returns true. If it is true then wait until the 30 second mark has passed. If the 30 second mark has already passed, then wait until isDone is true... or use Load() function on that scene since it's probably already mostly loaded.
Hi @phxvyper , thanks for your help.
I'm trying to do that, but cannot get it to work.
Ins$$anonymous$$d of an animation I'm using a fake progress bar now. But it doesn't display the bar, and it doesn't load the scene either.
using UnityEngine;
using System.Collections;
public class creditos2 : $$anonymous$$onoBehaviour {
private float ContadorSaltar;
private float TiempoEstancia = 30.0f;
public Texture2D[] background;
public Texture2D[] image;
public Texture2D[] bar;
AsyncOperation async;
private void StartLoading(){
AsyncOperation async = Application.LoadLevelAsync ("Scene1");
async.allowSceneActivation = false;
StartCoroutine ("activateScene");
}
// Update is called once per frame
void OnGUI () {
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background[0]);
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),image[0], Scale$$anonymous$$ode.ScaleToFit, true);
GUI.DrawTexture(new Rect(0,Screen.height-Screen.height/100,Screen.width/30*ContadorSaltar,Screen.height/100),bar[0]);
}
IEnumerator activateScene() {
ContadorSaltar += 1 * Time.deltaTime;
if (ContadorSaltar >= TiempoEstancia) {
async.allowSceneActivation = true;
yield return async;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613195056im_/https://answers.unity.com/themes/thub/images/avi.jpg)