Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndrewBrown123 · Sep 24, 2019 at 06:59 PM · 2duicontrollerraycastingeventsystem

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndrewBrown123 · Sep 25, 2019 at 02:47 PM 0
Share

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();
                     }
                 }
             }
         }
     }
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

283 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges