2017-10-17 66 views
0

我正在使用IEnumerator作爲可重置的計時器。我建立了一個航路點系統,你可以在這裏注視一個航路點,持續n秒以傳送到它。我使用IEnumerator作爲控制時間的方法,並且阻止意外的傳送。它可以正常工作,但是如果我注視任何光線播放項目,它會在某個地方混淆邏輯,並且必須重新審視一個航點,然後再次啓動它。這種失敗也會在我的航點的所有實例上出現,看看其中一個,然後看向另一個將無法傳送。我正在使用GVR版本1.70(1.100無法構建)的GVR激光筆和GVR分劃板。使用IEnumerator的Unity GVR Gaze事件

的代碼:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Gaze_Waypoints : MonoBehaviour { 

public int gazeTime = 3; //the amount of time needed to look to teleport 
public GameObject playerCam; //the players camera 
private Vector3 waypointPosition; //the position of the current waypoint 
private Vector3 playerCamPosition; //the current position of the players camera used for height information 
private IEnumerator waypointEnumerator; //my co-routine 
private bool startCoRoutine; //is the co-routine running 

void Start() { 
    waypointPosition = transform.position; //get the position of the owners waypoint 
    waypointEnumerator = waypointTimerEvent(); //set the ienumerator to the relevant function below 
    startCoRoutine = true; //testing this out as true or false 
} 

void Update() { 
    playerCamPosition = playerCam.transform.position; //keep track of the players camera, mainly for height info 
} 

// when I gaze a waypoint 
public void PointerEnter() { 
    Debug.Log("I entered."); 
    if (startCoRoutine) { 
     StartCoroutine (waypointEnumerator); 
    } else { 
     StopCoroutine (waypointEnumerator); 
    } 
    startCoRoutine = !startCoRoutine; 
} 

// when I look away 
public void PointerExit() { 
    Debug.Log("I exited."); 
    StopCoroutine (waypointEnumerator); 
} 

// if I look for 3 seconds teleport the user, if I look away reset the timer 
IEnumerator waypointTimerEvent() { 
    yield return new WaitForSeconds (gazeTime); 
    playerCam.transform.position = new Vector3 (waypointPosition.x, waypointPosition.y + playerCamPosition.y, waypointPosition.z); 
    StartCoroutine(waypointEnumerator); 
} 
} 

回答

0

看到MonoBehaviour.StartCoroutine

  1. 公共協程StartCoroutine(IEnumerator的例程)的不同調用;
  2. public coroutine StartCoroutine(string methodName,object value = null);

情況1 - 未經測試,但顯然 你必須使用一個循環,協程一樣

while (true){ 
    yield return new WaitForSeconds(waitTime); 
    debug.Log("WaitAndPrint"); 
} 

情況下2 - sligthly修改,但測試OK

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Gaze_Timer : MonoBehaviour { 

    public int gazeTime = 3; //the amount of time needed to look to teleport 
    public GameObject playerCam; //the players camera 
    private Vector3 startingPosition; 
    private Vector3 changePosition; 

    private bool startCoRoutine; //is the co-routine running 

    void Start() { 
     startingPosition = transform.localPosition; 
     changePosition = startingPosition; 
     startCoRoutine = false; //testing this out as true or false 
    } 

    void Update() { 
     //... 
    } 

    // when I gaze 
    public void PointerEnter() { 
     Debug.Log("I entered."); 
     if (startCoRoutine) { 
      StopCoroutine("waypointTimerEvent"); 
     } else { 
      StartCoroutine("waypointTimerEvent", gazeTime); 
     } 
     startCoRoutine = !startCoRoutine; 
    } 

    // when I look away 
    public void PointerExit() { 
     Debug.Log("I exited."); 
     if (startCoRoutine) { 
      StopCoroutine("waypointTimerEvent"); 
      startCoRoutine = false; 
     } 
    } 

    IEnumerator waypointTimerEvent(float waitTime) { 
     yield return new WaitForSeconds(waitTime); 
     Debug.Log("I waited " + waitTime + " seconds"); 
     GetComponent<Renderer>().material.color = Color.blue; 
     changePosition.y += 0.1f; 
     transform.localPosition = changePosition; 
    } 
}