Forum Discussion

0ctipusPrime's avatar
0ctipusPrime
Explorer
2 years ago

Making buttons react to MouseClicks as well as Poke/Ray cast

Hello, I am trying to make buttons react to both poking and mouse clicks in Play Mode in Unity. I got poking working through prefabs, but am struggling to add the functionality where they are triggered by mouse clicks as well. Has anyone done this? 

The reason is so that I speed up development on Mac since currently I have to build the program, put on headset, test it, attempt to fix issues, repeat. Each round taking way too long. 

Thank you for your help. 

3 Replies

    • 0ctipusPrime's avatar
      0ctipusPrime
      Explorer

      Hi, yes, I ended up adding the normal Unity Button functionality onto the meta prefab. I implemented it as custom script (below) on the Button Panel game object (under Button Visual). I have to put the reference to the actual function at two places but that is a small price to pay for the benefit. 

       

       

      using UnityEngine;
      using UnityEngine.Events;
      using UnityEngine.EventSystems;
      
      public class EventClick : MonoBehaviour, IPointerClickHandler
      {
      public GameObject parent;
      public UnityEvent onClick;
      public void OnPointerClick(PointerEventData eventData)
      {
      Debug.Log("Clicked: " + parent.name);
      
      if(onClick != null)
      onClick.Invoke();
      }
      }