如何获取抓取对象的时间



目前,我正在使用Unity和虚拟现实工具包(VRTK(进行一个项目。我想记录抓取对象的时间(用户抓取对象的时间(。

到目前为止,我得到了这个:

using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class System_2_Script_Slider_X : MonoBehaviour
{
    float Timer;
    void Start()
    {
        Timer = 0;
        if (GetComponent<VRTK_InteractableObject>() == null)
        {
            Debug.LogError("Team3_Interactable_Object_Extension is required to be attached to an Object that has the VRTK_InteractableObject script attached to it");
            return;
        }
        GetComponent<VRTK_InteractableObject>().InteractableObjectGrabbed += new InteractableObjectEventHandler(ObjectGrabbed);
        GetComponent<VRTK_InteractableObject>().InteractableObjectUngrabbed += new InteractableObjectEventHandler(ObjectUngrabbed);
    }
    private void ObjectGrabbed(object sender, InteractableObjectEventArgs e)
    {
        //Some Code
        Timer += Time.deltaTime;
    }
    private void ObjectUngrabbed(object sender, InteractableObjectEventArgs e)
    {
        //Some Code
        File.AppendAllText("PATH/Timer.txt", Timer.ToString());
        Timer = 0;
    }
}

不幸的是,这不起作用,因为即使我抓住物体的时间更长,我也会得到大约 2 毫秒的时间。因此,似乎ObjectGrabbed()函数会启动计时器,但会立即停止它。

我该如何解决这个问题?

对象

抓取函数可能只调用一次(当你抓取时(。它不会启动计时器,它只是将自上一帧以来的时间添加到变量中。如果只调用一次,则只会将一帧的时间添加到Time变量中。

相反,您应该做的是记录何时抓取对象以便您可以在释放对象时计算总长度:

using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class System_2_Script_Slider_X : MonoBehaviour
{
    private float _timeWhenGrabbed;
    void Start()
    {
        if (GetComponent<VRTK_InteractableObject>() == null)
        {
            Debug.LogError("Team3_Interactable_Object_Extension is required to be attached to an Object that has the VRTK_InteractableObject script attached to it");
            return;
        }
        GetComponent<VRTK_InteractableObject>().InteractableObjectGrabbed += new InteractableObjectEventHandler(ObjectGrabbed);
        GetComponent<VRTK_InteractableObject>().InteractableObjectUngrabbed += new InteractableObjectEventHandler(ObjectUngrabbed);
    }
    private void ObjectGrabbed(object sender, InteractableObjectEventArgs e)
    {
        //Some Code
        _timeWhenGrabbed = Time.time;
    }
    private void ObjectUngrabbed(object sender, InteractableObjectEventArgs e)
    {
        //Some Code
        var grabDuration = Time.time - _timeWhenGrabbed;
        File.AppendAllText("PATH/Timer.txt", grabDuration.ToString());
    }
}

可能 ObjectGrabbed 方法仅在发生Grab操作时触发,而不是抓取对象的每一帧。因此,您可能应该使用一些TimerStopWatch来设置代码(秒表具有更高的精度(。通过将计时器启动到 ObjectGrabbed 方法并将计时器停止到 ObjectUngrabbed 方法。
像这样:

using System.Diagnostic.Stopwatch;
...
private StopWatch Watcher = new StopWatch();
...
private void ObjectGrabbed(object sender, InteractableObjectEventArgs e)
{
    Watcher.Start();
}
private void ObjectUngrabbed(object sender, InteractableObjectEventArgs e)
{
    Watcher.Stop();
    long millis = Watcher.ElapsedMilliseconds;
    Watcher.Reset();
    File.AppendAllText("PATH/Timer.txt", millis.ToString());
}

相关内容

  • 没有找到相关文章

最新更新