- Home /
OnTrigger Down, Call the Button's OnClicked event
This is incredibly simple but it's impossible to find any help on it. I have an HTC vive controller and I'd like to call my button's onClicked event (which I set in the buttons inspector) on the triggerDown function. The onClicked event I have set for my button is simply to enable and disable a panel but I can't find any scripts to effectively turn my TriggerDown event into a Click.
Please help.
Comment
Best Answer
Answer by BrandonSeifker · Jun 19, 2017 at 04:30 PM
Answering my own question but here is what Ive found to work
using UnityEngine;
2 using System.Collections;
3 using UnityEngine.UI;
4 using System;
5
6 public class LaserButtonClicker : MonoBehaviour
7 {
8 SteamVR_LaserPointer laserPointer;
9 Button btn;
10 private int deviceIndex = -1;
11 private SteamVR_Controller.Device controller;
12 bool pointerOnButton = false;
13 GameObject myEventSystem;
14 // Use this for initialization
15 void Start()
16 {
17 myEventSystem = GameObject.Find("EventSystem");
18 laserPointer = GetComponent<SteamVR_LaserPointer>();
19 laserPointer.PointerIn += LaserPointer_PointerIn;
20 laserPointer.PointerOut += LaserPointer_PointerOut;
21 }
22 private void SetDeviceIndex(int index)
23 {
24 deviceIndex = index;
25 controller = SteamVR_Controller.Input(index);
26 }
27 private void LaserPointer_PointerOut(object sender, PointerEventArgs e)
28 {
29
30 if (btn != null)
31 {
32 pointerOnButton = false;
33 myEventSystem.GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
34 btn = null;
35 }
36 }
37
38 private void LaserPointer_PointerIn(object sender, PointerEventArgs e)
39 {
40
41 if (e.target.gameObject.GetComponent<Button>() != null && btn == null)
42 {
43
44 btn = e.target.gameObject.GetComponent<Button>();
45 btn.Select();
46
47 pointerOnButton = true;
48
49 }
50 }
51
52 void Update()
53 {
54 if (pointerOnButton)
55 {
56 if (controller.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))
57 {
58 btn.onClick.Invoke();
59 }
60 }
61 }
62
63
64
65
66 }
i got it from this github, you will need to add a rigid body and box collider to your controller as well
Answer by FlaSh-G · Jun 19, 2017 at 03:22 PM
You can invoke any Unity event using UnityEvent.Invoke.
someButton.onClick.Invoke();