Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 lunarwaterstudios · Apr 20 at 01:45 PM · 2duiraycastinggraphic

GraphicRaycaster not detecting UI Button only detecting UI Image

Hello

I've been working on this problem for awhile now and I just can't seem to figure it out. My GraphicRaycaster work fine but when I hover my custom cursor over a button object it will still think it's over a UI Image. This script is for my Xbox Controller because that's what my game uses. What I want is to have the GraphicRaycaster to stop getting the UI Image when my cursor is over a UI Button. My Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CusorInput : MonoBehaviour
 {
     //THIS SCRIPT IS ON THE CANVAS...
     public InternetCurser myIntCursor;
     public GameObject CurserObject;
     public Image CurserObjImage;//this will be used to get the image so that the sprite can change...
     public Sprite Curser1;//normal looking curser sprite...
     public Sprite Curser2;//can click on something sprite...
     bool firstTargetIgnord = false;
     public bool hoverdOver = 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>();
     }
     void Update()
     {
         //Debug.Log("WORK!!!");
         //Sets the new pointer event...
         myPointerEventData = new PointerEventData(myEventSystem);
         //sets the pointer event position to that of mouse position...
         //Main Cursor that will be used for everything else...
         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...
         //In order for it to work it must be in update...
         //once in update it will run..
         if (myIntCursor.ShowCurser)
         {
             foreach (RaycastResult hovered in results)
             {
                 if (hovered.gameObject.GetComponent<Button>())
                 {
                     //Debug.Log("I'm hovering over Button");
                     CurserObjImage.sprite = Curser2;
                 }
                 /*if (!hovered.gameObject.GetComponent<Button>())
                 {
                     Debug.Log("I'm not hovering over a button");
                     CurserObjImage.sprite = Curser1;
                 }*/
             }
         }
         if (Input.GetButtonUp("A Button"))
         {
             CurserObjImage.sprite = Curser1;
             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("INVOKED " + result.gameObject.name);
                         result.gameObject.GetComponent<Button>().onClick.Invoke();
                     }
                         /*if (result.gameObject.GetComponent<InputField>())
                         {
                             result.gameObject.GetComponent<InputField>().ActivateInputField();
                             myinternet.Keybourd.SetActive(true);
                         }*/
                 }
             }
         }
     }
 }

I thank anyone that comes to help. Thank You

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by lunarwaterstudios · Apr 22 at 07:27 PM

I have figured it out. Works really good. My Code:

 public class CusorInput : MonoBehaviour
 {
     public InternetCurser myIntCursor;
     public GameObject CursorObject;
     public Image CurserObjImage;
     public Sprite Curser1;
     public Sprite Curser2;
     public LayerMask ButtonLayerMask;
     public float RaycastHitDistance;
 
     //GraphicRaycaster
     bool firstTargetIgnord = false;
     GraphicRaycaster myRaycaster;
     PointerEventData myPointerEventData;
     EventSystem myEventSystem;
 
     void Start()
     {
         myRaycaster = GetComponent<GraphicRaycaster>();
         myEventSystem = GetComponent<EventSystem>();
     }
     void FixedUpdate()
     {
         RaycastHit2D Hit2D = Physics2D.Raycast(CursorObject.transform.position, -Vector2.up, RaycastHitDistance, ButtonLayerMask);
         if (myIntCursor.ShowCurser)
         {
             myPointerEventData = new PointerEventData(myEventSystem);
             myPointerEventData.position = CursorObject.transform.position;
             List<RaycastResult> results = new List<RaycastResult>();
             myRaycaster.Raycast(myPointerEventData, results);
             if (Hit2D.collider != null)
             {
                 CurserObjImage.sprite = Curser2;
             }
             if (Hit2D.collider == null)
             {
                 CurserObjImage.sprite = Curser1;
             }
             if (Input.GetButtonUp("A Button"))
             {
                 foreach (RaycastResult result in results)
                 {
                     if (!firstTargetIgnord)
                     {
                         firstTargetIgnord = true;
                     }
                     if (firstTargetIgnord)
                     {
                         if (result.gameObject.GetComponent<Button>())
                         {
                             result.gameObject.GetComponent<Button>().onClick.Invoke();
                         }
                     }   
                 }
             }
         }
     }
 }

You have to use both Raycast2D, and GraphicRaycaster.

Comment
Add comment · Share
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
0

Answer by Pangamini · Apr 20 at 04:34 PM

It's GraphicRaycaster, it hits graphics elements of the UI. The input messages are then sent up the hierarchy from the object that's hit by the raycaster(s), and are handled by a Button. Button itself is not a raycast target, it doesn't have any shape.

Comment
Add comment · Share
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

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

372 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Problem with Graphic Raycast and using it with Xbox Controller 0 Answers

Need help writing script 1 Answer

2D renderer with particles on UI 1 Answer

2D Collider larger than specified 0 Answers

Button OnClick() Missing Code Error, How do I fix this? 1 Answer


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