2017-05-04 153 views
0

我正在开发一个应用程序,使用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"); 
    } 
} 

回答

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 )。

+0

是啊!非常感谢你 !!我尝试使用同一个脚本来处理按钮,但它不起作用。如何解决它。 –

+0

是啊!非常感谢你 !!我尝试使用同一个脚本来处理按钮,但它不起作用。如何解决它。 –

+0

在'使用UnityEngine.SceneManagement'后添加'使用UnityEngine.EventSystems'' –

0

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

相关问题