Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by CanisLupus · Oct 31, 2014 at 05:22 PM · uiraycast

(Unity 4.6) How to raycast against uGUI objects from an arbitrary screen/canvas position

We wanted to do a raycast from an arbitrary position over uGUI objects. As far as we can tell, we seem to need an instance of GraphicRaycaster. As one comes attached to the default Canvas object, we could easily get its reference (let's call it "raycaster") and then call raycaster.Raycast(...).

Now, Raycast receives two arguments: a PointerEventData (common event object to all mouse/touch events) and a List. Currently, the documentation for Raycast does not describe what it does exactly, but we assume that the List of RaycastResults is filled by Raycast itself. From each RaycastResult we could then access the gameObject it hit (it's documented here).

But what about PointerEventData? Isn't it supposed to come from a pointer event? Here, we have no mouse or touch, and simply want to raycast from an arbitrary position. How should we proceed to create one and set its (screen/world) position? Or is there another way for uGUI raycasts altogether?

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
24
Best Answer

Answer by CanisLupus · Nov 21, 2014 at 11:46 PM

It turns out that simply creating a new PointerEventData and then setting its position to wherever you want (in 2D) works. For instance, in the following example I raycast from someWorldPosition, getting a list of the uGUI elements that are behind that screen point.

 var pointer = new PointerEventData(EventSystem.current);
 // convert to a 2D position
 pointer.position = someCamera.WorldToScreenPoint(someWorldPosition);

 var raycastResults = new List<RaycastResult>();
 EventSystem.current.RaycastAll(pointer, raycastResults);

 if (raycastResults.Count > 0) {
     // Do anything to the hit objects. Here, I simply disable the first one.
     raycastResults[0].gameObject.SetActive(false);
 }
Comment
Add comment · Show 3 · 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 ChrisSch · Mar 28, 2015 at 06:10 PM 3
Share

Thanks! Had no idea it was that simple. The new UI is quite different how it works. :D

I wanted to make my icons follow the mouse cursor (for inventory) so this is my version of the above, on mouse button, in C#.

 if(Input.Get$$anonymous$$ouseButton(0))
         {
             PointerEventData pointer = new PointerEventData(EventSystem.current);
             pointer.position = Input.mousePosition;
 
             List<RaycastResult> raycastResults = new List<RaycastResult>();
             EventSystem.current.RaycastAll(pointer, raycastResults);
 
             if(raycastResults.Count > 0)
             {
                 if(raycastResults[0].gameObject.tag == "InventoryItem")
                 raycastResults[0].gameObject.transform.position = Input.mousePosition;
             }
         }

EDIT: Don't forget to add "using UnityEngine.EventSystems;" at the top, and ofc "using System.Collections.Generic;" for lists.

avatar image DevsGoingViral · Apr 13, 2015 at 02:15 AM 0
Share

Hello people,this is exactly solution to save my life/game but i can't translate this into java.

i can do only fist line :

var pointer : PointerEventData; pointer = new PointerEventData(EventSystem.current);

Any help is appreciated.

avatar image kolboch · Nov 27, 2018 at 04:03 PM 0
Share

Hey I have a question about RaycastAll. Is it raycasting just against UI elements or it does take into considerations other raycasts (ex. Physics Raycaster) too ? I'm asking because the documentation states that EventSystem by default has 3 raycasters if I read and understood it well.

avatar image
0

Answer by Bitpocketer · Feb 06, 2016 at 12:48 PM

One thing i would like to know though, how one would make the associated button with the pointer send the submit event to the eventsystem?

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

28 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

Related Questions

uGUI clicks going into gameobject raycasts 0 Answers

Blocking and not blocking input in new uGUI 4.6 1 Answer

Negative Positions Breaks Raycasts 1 Answer

How to touch select 3D objects 2 Answers

How to detect the position of a texture that is clicked 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