如何从另一个类调用VRInput类事件?
0 252
0
该提问暂无详细描述
收藏
2021-01-08 15:58 更新 gitvrar •  459
共 1 个回答
高赞 时间
2

事实上,您可以从另一个类触发这些事件。 首先声明一个具有两个共享相同内存空间的字段的类:

[StructLayout(LayoutKind.Explicit)]
public class OverlapEvents
{
    [FieldOffset(0)]
    public VRInput Source;

    [FieldOffset(0)]
    public EventCapture Target;
}

然后,您可以声明一个新类,该类将拦截并调用另一个事件:

public class EventCapture
{
    public event Action OnClick;

    public void SimulateClick()
    {
        InvokeClicked();
    }

    // This method will call the event from VRInput!
    private void InvokeClicked()
    {
        var handler = OnClick;
        if (handler != null)
            handler();
    }
}

最后注册并调用它: public static void Main()

{
    input = GetComponent<VRInput>();

    // Overlap the event
    var o = new OverlapEvents { Source = input };

    // You can now call the event! (Note how Target should be null but is of type VRInput)
    o.Target.SimulateClick();
}

回答转载自https://stackoverflow.com/questions/49865143/how-to-call-vrinput-class-events-onclick-ondoubleclick-from-another-clas

收藏
2021-01-08 16:08 更新 同步 •  1366

这个回答很有用

🍊小桔子 •  1922 2021-01-08 17:59