首页 文章

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

提问于
浏览
1

我正在使用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

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

    您应该在3d中渲染,最简单的方法是在相机周围放置一个球体(甚至更好,每只眼睛周围有两个球体)并设置它们的不透明度 .

  • 1

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

    我完成的是当我的游戏开始时有一个Cardboard Player Prefab产生的实例,并且通过DontDestoryOnLoad()持续存在于我的所有关卡中,而不是在每个关卡中都有一个单独的实例 . 这允许将设置转移到每个加载的级别,并在屏幕中淡出和淡入 .

    我通过创建一个世界空间画布来完成一个屏幕推子,该画面是Cardboard预制件的“Head”对象的父级,因此它遵循凝视,并且放置一个覆盖整个画布的黑色精灵图像,当黑色精灵可见时阻挡玩家视图 .

    附加到我的Player Prefab的脚本允许我先淡出屏幕(调用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;
    }
    

    }

相关问题