1

目前我正在使用 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()功能似乎启动了计时器,但立即停止了它。

我怎样才能解决这个问题?

4

2 回答 2

3

对象抓取函数可能只调用一次(当您抓取时)。它不会启动计时器,它只是将自上一帧以来的时间添加到您的变量中。如果只调用一次,它只会将一帧的时间添加到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());
    }
}
于 2018-03-15T14:13:15.320 回答
2

可能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());
}
于 2018-03-15T14:15:15.880 回答