在Unity - Google Cardboard插件中,场景之间淡入/淡出不起作用

时间:2015-10-28 16:39:04

标签: c# unity3d fade transitions google-cardboard

我正在使用Google CardbBoard插件在Unity中开发一个应用程序,我尝试在场景之间传递时淡入/淡出屏幕,我使用此示例在GUI对象中绘制纹理:

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);        
}

我使用的代码来自此页面:Video TutorialExample downloads,它在没有Cardboard插件的Unity游戏中运行良好,但在我当前的项目中使用此代码的方式相同不管用。唯一的区别是使用Cardboard插件。

我必须使用任何特定的Cardboard对象而不是GUI或其他方式来绘制纹理吗?

2 个答案:

答案 0 :(得分:1)

As per the Google Cardboard docs,您需要在相机前面的3D空间中存在GUI元素,以便在每只眼睛中复制它们。

我将分享我如何做到这一点的解决方案。请注意,我所做的是在我的游戏开始时有一个Cardboard Player Prefab产生的实例,并通过DontDestoryOnLoad()在我的所有关卡中保持,而不是在每个关卡中都有一个单独的实例。 这允许将设置转移到每个加载的级别,并在屏幕中淡出和淡入。

我通过创建一个世界空间画布完成了一个屏幕推子,该画面是纸板预制件的主要原因&#34; Head&#34;对象,所以它遵循凝视,并放置一个黑色精灵图像覆盖整个画布,当黑色精灵可见时阻止玩家视图。

这个附加到我的播放器预制件的脚本允许我先淡出屏幕(调用FadeOut()),加载一个新级别(将LevelToLoad设置为你想要加载的级别索引),然后在新屏幕后淡入屏幕级别已加载。

默认情况下,它使用异步方式加载级别,允许加载条形图,但您可以将UseAsync设置为false以通过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;
}

}

答案 1 :(得分:0)

GUIGUILayoutGraphics在VR中不起作用。没有2d直接到屏幕将正常工作。

你应该用3D渲染,最简单的方法是在镜头周围放一个球体(或者更好的是,每只眼睛周围有两个球体)并设置它们的不透明度。

相关问题