【问题标题】:Unity GVR Gaze events with IEnumerator使用 IEnumerator 的 Unity GVR Gaze 事件
【发布时间】:2017-10-30 18:20:45
【问题描述】:

我正在使用 IEnumerator 作为可以重置的计时器。我设置了一个航点系统,您可以在其中凝视一个航点 n 秒以传送到它。我使用 IEnumerator 作为一种控制时间的方法,并阻止意外的传送。它工作正常,但如果我凝视任何光线投射项目,它会在某处弄乱逻辑,您必须查看然后重新查看一个航点以重新启动它。这个失败也发生在我的航路点的所有实例上,看一个然后看别处再看另一个将无法传送。我正在使用 GVR build 1.70 中的 GVR Laser Pointer 和 GVR Reticle(1.100 构建失败)。

代码:

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);
}
}

【问题讨论】:

    标签: c# unity3d virtual-reality


    【解决方案1】:

    查看 MonoBehaviour.StartCoroutine 的不同调用

    1. public Coroutine StartCoroutine(IEnumerator routine);
    2. public Coroutine StartCoroutine(string methodName, object value = null);

    案例 1. - 未经测试但显然 您必须在协程中使用循环,例如

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

    案例 2. - 稍作修改但测试正常

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2017-06-02
      • 2020-02-11
      • 2017-04-27
      相关资源
      最近更新 更多