Unity Steam VR如何引用当前持有的对象?

时间:2019-03-05 21:52:19

标签: c# unity3d virtual-reality htc-vive

在附加到控制器的脚本中,我希望能够引用控制器当时持有的子对象,但是我不确定如何。 知道如何执行此操作,是否需要标记对象或沿这些内容的东西?

1 个答案:

答案 0 :(得分:0)

一种执行此操作的方法(至少我已经做过)是使用public MainViewModel() { var rng = new Random(); var timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(0.1); timer.Tick += (s, e) => { var row = this.PriceLevels[rng.Next(this.PriceLevels.Count())]; // get random row switch (rng.Next(4)) { case 0: row.BuyOrders.Add(1 + rng.Next(5)); break; case 1: row.SellOrders.Add(1 + rng.Next(5)); break; case 2: if (row.BuyOrders.Count() > 0) row.BuyOrders.RemoveAt(rng.Next(row.BuyOrders.Count())); break; case 3: if (row.SellOrders.Count() > 0) row.SellOrders.RemoveAt(rng.Next(row.SellOrders.Count())); break; } }; timer.Start(); } 事件。

您在控制器中创建了两个事件:

System

如果您设法在捡起物品的范围之内,则触发事件event EventHandler OnPickedUp; event EventHandler OnLetGo;

OnPickekUp

然后,无论需要关心您捡起或放下东西的事实,都可以这样做:

public class MyVRController
{
    public event EventHandler OnPickedup;
    public event EventHandler OnLetGo;
    private bool HasObject = false;
    ...
    private void SuccessfullyPickedUp(GameObject pickedUpGO)
    {
        if(OnPickedUp != null)
        {
            HasObject = true;
            OnPickedUp(pickedUpGO, null);
        }
    }
    ...
    private void OnLetGo()
    {
        if(OnLetGo != null)
        {
            HasObject = false;
            OnLetGo(this, null);
        }
    }
    ...
}

如果需要,您可以在public class MyGameManager { public void Start() { // However you reference the controllers, do it here. myRightVRController.OnPickedUp += SomeFunc1; myRightVRcontroller.OnLetGo += SomeFunc2; myLeftVRController.OnPickedUp += SomeFunc3; myLeftVRController.OnLetGo += SomeFunc4; // The rest of your initialization... } } 中指定事件的来源,该控制器可以传递(当前传递EventArgs)。

希望这会有所帮助!