如何通过统一注视一个物体从一个场景转到另一个场景?

how to go from one scene to another scene by gazing an object in unity?

我正在使用 Unity 开发一个应用程序,我在其中创建了两个 Scene's.if 用户注视着 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");
    }
}

您应该使用适当的值初始化您的变量并按如下方式使用 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)。

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