G.Soria
G.Soria

Reputation: 71

Fade in/out between scenes is not working in Unity - Google Cardboard plugin

I'm developing an application in Unity with the Google CardbBoard Plugin, and I tried to fade in/out the screen when passing between scenes, I've worked with this example drawing a texture in the GUI object:

GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);

Texture2D myTex;
myTex = new Texture2D (1, 1);
myTex.SetPixel (0, 0, fadeColor);
myTex.Apply ();

GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), myTex);
if (isFadeIn)
    alpha = Mathf.Lerp (alpha, -0.1f, fadeDamp * Time.deltaTime);
else
    alpha = Mathf.Lerp (alpha, 1.1f, fadeDamp * Time.deltaTime);

if (alpha >= 1 && !isFadeIn) {
    Application.LoadLevel (fadeScene);      
    DontDestroyOnLoad(gameObject);      
} else if (alpha <= 0 && isFadeIn) {
    Destroy(gameObject);        
}

The code I worked with is from this page: Video Tutorial, Example downloads, and it worked fine in a Unity game without the Cardboard plugin, but in my current project the same way to use this code is not working. The only difference is the use of the Cardboard plugin.

Is there any specific Cardboard object I must use instead of GUI or another way to draw a texture?

Upvotes: 1

Views: 3446

Answers (2)

Matt3D
Matt3D

Reputation: 11

As per the Google Cardboard docs, You need to have GUI elements exist in 3D space infront of the camera so they are replicated in each eye.

I'll share my solution of how I did it. Note that What I've done is have a single instance of the Cardboard Player Prefab spawn when my game starts and persist throughout all my levels via DontDestoryOnLoad(), rather than have a seperate instance in each level. This allows for settings to be carried over to each loaded level and Fade out and Fade in the screen.

I accomplished a screen fader by creating a World Space Canvas that is parented to the Cardboard prefab's "Head" object so it follows gaze, And put a Black Sprite image that covers the entire Canvas which blocks the players view when the Black Sprite is visible.

This script attached to my Player Prefab allows me to first fade out the screen (call FadeOut()), Load a new level (set LevelToLoad to the level index you want to load), then Fade in the screen after the new level is loaded.

By default it uses the Async way of loading levels, To allow for loading Bars, But you can set UseAsync to false to load levels via Application.LoadLevel()

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LoadOperations: MonoBehaviour {

public Image myImage;
// Use this for initialization
public bool UseAsync;
private AsyncOperation async = null;
public int LevelToLoad;

public float FadeoutTime;
public float fadeSpeed = 1.5f; 
private bool fadeout;
private bool fadein;    


public void FadeOut(){
    fadein= false;
    fadeout = true;
    Debug.Log("Fading Out");
}

public void FadeIn(){
    fadeout = false;
    fadein = true;
    Debug.Log("Fading In");
}

void Update(){

    if(async != null){
        Debug.Log(async.progress);
        //When the Async is finished, the level is done loading, fade in the screen
        if(async.progress >= 1.0){
            async = null;
            FadeIn();
        }
    }

    //Fade Out the screen to black
    if(fadeout){
        myImage.color = Color.Lerp(myImage.color, Color.black, fadeSpeed * Time.deltaTime);

        //Once the Black image is visible enough, Start loading the next level
        if(myImage.color.a >= 0.999){
            StartCoroutine("LoadALevel");
            fadeout = false;
        }
    }

    if(fadein){
        myImage.color = Color.Lerp(myImage.color, new Color(0,0,0,0), fadeSpeed * Time.deltaTime);

        if(myImage.color.a <= 0.01){
            fadein = false;
        }
    }
}

public void LoadLevel(int index){
    if(UseAsync){
        LevelToLoad= index;
    }else{
        Application.LoadLevel(index);
    }
}

public IEnumerator LoadALevel() {
    async = Application.LoadLevelAsync(LevelToLoad);
    yield return async;
}

}

Upvotes: 1

Krzysztof Bociurko
Krzysztof Bociurko

Reputation: 4661

The GUI, GUILayout and Graphics do not work in VR. No 2d direct to screen will work properly.

You should render in 3d, easiest thing to do is to put a sphere around the camera (or even better, two spheres around each eye) and animate their opacity.

Upvotes: 0

Related Questions