C# n秒后凝视触发事件-统一
我有一个“指针输入”事件,但我只想在凝视对象n秒后触发该事件C# n秒后凝视触发事件-统一,c#,unity3d,virtual-reality,C#,Unity3d,Virtual Reality,我有一个“指针输入”事件,但我只想在凝视对象n秒后触发该事件 public void PointerEnter() { // change scene if the gaze point have been active for n seconds. } 不管怎样,要做到这一点 仅仅有一个超时是不行的,因为它仍然会执行,而指针是否会锁定在对象上。您可以使用一个布尔变量,通过将其设置为true和false来保持指针进入和退出的状态。然后可以在更新函数中检查此布尔变量。当它为真时,启动计时器
public void PointerEnter() {
// change scene if the gaze point have been active for n seconds.
}
不管怎样,要做到这一点
仅仅有一个超时是不行的,因为它仍然会执行,而指针是否会锁定在对象上。您可以使用一个布尔变量,通过将其设置为true和false来保持指针进入和退出的状态。然后可以在更新函数中检查此布尔变量。当它为真时,启动计时器,如果在计时器期间变为假,则将计时器重置为0。检查计时器是否超过x秒,然后加载新场景 下面的示例假设在指针指向时调用PointerEnter,在指针不再指向时调用PointerExit。根据您使用的VR插件,这些函数可能会有所不同,但其余代码是相同的
const float nSecond = 2f;
float timer = 0;
bool entered = false;
public void PointerEnter()
{
entered = true;
}
public void PointerExit()
{
entered = false;
}
void Update()
{
//If pointer is pointing on the object, start the timer
if (entered)
{
//Increment timer
timer += Time.deltaTime;
//Load scene if counter has reached the nSecond
if (timer > nSecond)
{
SceneManager.LoadScene("SceneName");
}
}
else
{
//Reset timer when it's no longer pointing
timer = 0;
}
}
您可以使用布尔变量,通过将其设置为true和false来保持指针进入和退出的状态。然后可以在更新函数中检查此布尔变量。当它为真时,启动计时器,如果在计时器期间变为假,则将计时器重置为0。检查计时器是否超过x秒,然后加载新场景 下面的示例假设在指针指向时调用PointerEnter,在指针不再指向时调用PointerExit。根据您使用的VR插件,这些函数可能会有所不同,但其余代码是相同的
const float nSecond = 2f;
float timer = 0;
bool entered = false;
public void PointerEnter()
{
entered = true;
}
public void PointerExit()
{
entered = false;
}
void Update()
{
//If pointer is pointing on the object, start the timer
if (entered)
{
//Increment timer
timer += Time.deltaTime;
//Load scene if counter has reached the nSecond
if (timer > nSecond)
{
SceneManager.LoadScene("SceneName");
}
}
else
{
//Reset timer when it's no longer pointing
timer = 0;
}
}
您根本不需要使用指针事件,而是使用简单的光线投射。这样做的好处是,你也可以将它与图层标记、标签或任何你想用来识别一组对象的东西结合使用,而不需要在你想让你的视线使用的每个对象上运行pointerevent。但是,你只需要在你的虚拟现实头或光线投射器上写一个脚本 在我的示例中,我将使用layermask。这将使凝视作用于同一层中的任何对象,如uiButton
public sceneIndex = 0; //build index of the scene you want to switch to
private Ray ray;
private RaycastHit hit;
[serializefield]
private LayerMask myMask;//serializing it will give a dropdown menu in the editor to select the mask layer form, can also use int to select the layer
private readonly float rayLength = 10;
private readonly float timerMax = 5f; //Higher timerMax is a longer wait, lower timerMax is shorter...
private float timer = 0f;
private void Update()
{
ray = Camera.main.ViewportPointToRay(Vector3.forward);
if (Physics.Raycast(ray, out hit, rayLength, myMask))
{
timer += Time.deltaTime;
if(timer >= timerMax)
{
SceneManager.LoadScene(sceneIndex);//load the scene with the build index of sceneIndex
}
}
else
{
timer = 0;
}
}
只要您查看与myMask timer位于同一层的光线投射器长度内的对象,该对象将不断增加,直到它大于或等于timerMax,当满足此条件时,它将改变场景。您根本不需要使用指针事件,而是使用简单的光线投射。这样做的好处是,你也可以将它与图层标记、标签或任何你想用来识别一组对象的东西结合使用,而不需要在你想让你的视线使用的每个对象上运行pointerevent。但是,你只需要在你的虚拟现实头或光线投射器上写一个脚本 在我的示例中,我将使用layermask。这将使凝视作用于同一层中的任何对象,如uiButton
public sceneIndex = 0; //build index of the scene you want to switch to
private Ray ray;
private RaycastHit hit;
[serializefield]
private LayerMask myMask;//serializing it will give a dropdown menu in the editor to select the mask layer form, can also use int to select the layer
private readonly float rayLength = 10;
private readonly float timerMax = 5f; //Higher timerMax is a longer wait, lower timerMax is shorter...
private float timer = 0f;
private void Update()
{
ray = Camera.main.ViewportPointToRay(Vector3.forward);
if (Physics.Raycast(ray, out hit, rayLength, myMask))
{
timer += Time.deltaTime;
if(timer >= timerMax)
{
SceneManager.LoadScene(sceneIndex);//load the scene with the build index of sceneIndex
}
}
else
{
timer = 0;
}
}
只要你观察与myMask timer位于同一层的光线投射器长度范围内的对象,该对象将不断增加,直到它大于或等于timerMax,当满足此条件时,它将改变场景。Taka仅需一个超时,当你看向别处时,事件将触发。-在此之前,你必须将注意力集中在游戏对象上n秒,-不仅仅是一个延迟。你可以取消PointerExit上的协同程序。只需一个超时就可以看到,当你移开视线时,事件将触发。-在这之前,你必须专注于游戏对象n秒,而不仅仅是延迟。你可以取消PointerExit上的协同程序。