- Home /
Problem with Graphic Raycast and using it with Xbox Controller
Hello
My problem is that I can't figure out how to get Graphic Raycast to click a button in my UI. I've trying for 3 days at this point and every time I find something it tells to raycast the object or just use the mouse. I'm trying to create a UI system that uses the Xbox joysticks and buttons to control the mouse.
Here is what I've got so far:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CusorInput : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
 {
     public GameObject CurserObject;
     GraphicRaycaster myRaycaster;
     PointerEventData myPointerEventData;
     EventSystem myEventSystem;
     GameObject myGameObject;
 
     void Start()
     {
         //Fetch the raycaster from the gameobject aka the canvas...
         myRaycaster = GetComponent<GraphicRaycaster>();
         //fetch the eventsystem from the scene...
         myEventSystem = GetComponent<EventSystem>();
         //cursorPosition = new Vector2(Screen.width / 2f, Screen.height / 2f);
     }
     public void OnPointerEnter(PointerEventData eventData)
     {
         //Code Here...
     }
     public void OnPointerClick(PointerEventData eventData)
     {
         if (eventData.button == PointerEventData.InputButton.Left)
         {
             Debug.Log("Hello");
         }
     }
     public void OnPointerExit(PointerEventData eventData)
     {
         //Code Here...
     }
     void Update()
     {
         if (Input.GetButtonUp("A Button"))
         {
             //Debug.Log("WORK!!!");
             //Sets the new pointer event...
             myPointerEventData = new PointerEventData(myEventSystem);
             //sets the pointer event position to that of mouse position...
             myPointerEventData.position = CurserObject.transform.position;
             //Create a list of raycast result...
             List<RaycastResult> results = new List<RaycastResult>();
             //raycast using the graphics raycaster and click position...
             myRaycaster.Raycast(myPointerEventData, results);
             //for every result returned, output the name of the 
             //gameobject on the canvas hit by the ray...
             foreach (RaycastResult result in results)
             {
                 Debug.Log("Clicked On " + name);
             }
         }
     }
 }
My CurserObject can move around the screen and I can press the A Button on my controller and it prints in the console what it hits (Right now it only hits the canvas). The nest thing I found I should do is to get OnPointerClick but it will not let me get anything that is not Left, Middle, or Right mouse clicks. 
I thank anyone that comes by to help. Any help is welcomed and appreciated.
Thank You.
I Got It.
Found the answer on another UA page here: https://answers.unity.com/questions/1506153/how-to-detect-click-events-on-overlapping-ui-eleme.html
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CusorInput : $$anonymous$$onoBehaviour
 {
     public GameObject CurserObject;
     bool firstTargetIgnord = false;
     GraphicRaycaster myRaycaster;
     PointerEventData myPointerEventData;
     EventSystem myEventSystem;
     GameObject myGameObject;
 
     void Start()
     {
         //Fetch the raycaster from the gameobject aka the canvas...
         myRaycaster = GetComponent<GraphicRaycaster>();
         //fetch the eventsystem from the scene...
         myEventSystem = GetComponent<EventSystem>();
         //cursorPosition = new Vector2(Screen.width / 2f, Screen.height / 2f);
     }
     void Update()
     {
         if (Input.GetButtonUp("A Button"))
         {
             //Debug.Log("WOR$$anonymous$$!!!");
             //Sets the new pointer event...
             myPointerEventData = new PointerEventData(myEventSystem);
             //sets the pointer event position to that of mouse position...
             myPointerEventData.position = CurserObject.transform.position;
             //Create a list of raycast result...
             List<RaycastResult> results = new List<RaycastResult>();
             //raycast using the graphics raycaster and click position...
             myRaycaster.Raycast(myPointerEventData, results);
             //for every result returned, output the name of the 
             //gameobject on the canvas hit by the ray...
             foreach (RaycastResult result in results)
             {
                 Debug.Log("Clicked On " + name);
                 // Ignore the first target with raycastTarget 
                 // as this will receive the normal click event trigger
                 // This prevents the first raycastTarget being invoked twice
                 if (!firstTargetIgnord)
                 {
                     firstTargetIgnord = true;
                 }
                 if (firstTargetIgnord)
                 {
                     if (result.gameObject.GetComponent<Button>())
                     {
                         Debug.Log("INVO$$anonymous$$ED " + result.gameObject.name);
                         result.gameObject.GetComponent<Button>().onClick.Invoke();
                     }
                 }
             }
         }
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
PointerEventData null reference expectation error 0 Answers
uUI - OnSelect: From Mouse/Pointer or Keyboard/Controller? 0 Answers
Is there a way to fire the inspector events from code? 0 Answers
Rotating game object using UI Joystick 0 Answers
GraphicRaycaster not detecting UI Button only detecting UI Image 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                