【问题标题】:how to go from one scene to another scene by gazing an object in unity?如何通过统一凝视一个物体来从一个场景到另一个场景?
【发布时间】:2017-05-04 06:09:46
【问题描述】:

我正在使用Unity 开发一个应用程序,其中我创建了两个Scene's。如果用户凝视Scene 1 中的一个对象,它应该转到Scene 2。我有下面的代码,但我得到了错误。

源代码:-

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class time : MonoBehaviour {

    public float gazeTime = 2f;

    private float timer;

    private bool gazedAt;


    // Use this for initialization
    void Start () {

    }
    void update(){
        if (gazedAt)
        {
            timer += Time.deltaTime;

            if (timer >= gazeTime)
            {

                Application.LoadLevel (scenetochangeto);

                timer = 0f;
            }

        }

    }
    public void ss(string scenetochangeto)
    {
        gameObject.SetActive (true);
    }

    public void pointerenter()
    {



        //Debug.Log("pointer enter");
        gazedAt = true;
    }

    public void pointerexit()
    {
        //Debug.Log("pointer exit");
        gazedAt = false;
    }
    public void pointerdown()
    {
        Debug.Log("pointer down");
    }
}

【问题讨论】:

    标签: c# string unity3d gameobject gaze-buttons


    【解决方案1】:

    您应该使用正确的值初始化变量并使用scene manager to load new scene,如下所示 -

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.EventSystems;
    
    public class time : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
    
        public float gazeTime = 2f;
        private float timer = 0f;
        private bool gazedAt = false;
    
        // Use this for initialization
        void Start () {
    
        }
        void Update(){
            if (gazedAt)
            {
                timer += Time.deltaTime;
                if (timer >= gazeTime)
                {
                    SceneManager.LoadScene("OtherSceneName");
                    timer = 0f;
                }
            }
        }
        public void ss(string scenetochangeto)
        {
            gameObject.SetActive (true);
        }
    
        public void OnPointerEnter(PointerEventData eventData)
        {
            //Debug.Log("pointer enter");
            gazedAt = true;
        }
    
        public void OnPointerExit(PointerEventData eventData)
        {
            //Debug.Log("pointer exit");
            gazedAt = false;
        }
    }
    

    "OtherSceneName" 更改为您需要加载的场景名称 (scenetochangeto)。

    【讨论】:

    • 是的!非常感谢你 !!我尝试使用相同的脚本来处理按钮,但它不起作用。如何解决。
    • using UnityEngine.SceneManagement之后添加using UnityEngine.EventSystems;
    • 我已经针对画布中的按钮、图像和文本 UI 元素对其进行了测试。请解释一下您遇到的问题?
    • 是的!我已经添加了!已经!实际上我正在使用 gvrmain 和 gvr reticle。此代码适用于按钮,但问题是(“标线未指向按钮”)即(canvas-button-text)这里我已将脚本添加到“按钮”->添加组件->添加脚本->添加新事件触发器
    • @aishwariya 欢迎您!我没有在 gvr 上工作过,但是 看起来与实现指针事件回调方法不同。
    【解决方案2】:

    你没有指定你得到的错误,但是注意:Update()是Unity引擎的一个“特殊”功能,需要大写U。它永远不会像现在这样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      相关资源
      最近更新 更多