2017-10-17 179 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; 
    } 
}