2017-05-04 152 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.它不會像現在這樣工作。

相關問題