- Home /
How do you perform a Graphic Raycast?
I need to perform a raycast to detect a UI object with no collider. It appears this is how the event system works according to this page...
I've performed a physics raycast before, but this one is new for me. What is the syntax to perform a graphic raycast?
Thank you!
Sorry, I answered this before when I meant to comment.
Did you ever figure out how to do this?
Thanks, CmdrBond! This works great. Those demos you made are really cool too. Good luck with your $$anonymous$$orrowind game! I hope to hear about it when it comes out.
You're very welcome! I do hope it helped!
And I actually just submitted the update to my script package this morning, so it should be live on the store within a few days. You're welcome to check out the new demo here...
And yes! I hope to make a lot of $$anonymous$$orrowind-inspired content, so I will make sure to announce any release from the rooftops, haha! The more games like it in the world, the better! :-D
Answer by TOES2 · Jun 10, 2017 at 06:32 PM
//Code to be place in a MonoBehaviour with a GraphicRaycaster component
GraphicRaycaster gr = this.GetComponent<GraphicRaycaster>();
//Create the PointerEventData with null for the EventSystem
PointerEventData ped = new PointerEventData(null);
//Set required parameters, in this case, mouse position
ped.position = Input.mousePosition;
//Create list to receive all results
List<RaycastResult> results = new List<RaycastResult>();
//Raycast it
gr.Raycast(ped, results);
This question was already answered in the comment section of the thread, no need to bump up an old post.
Not for the topic owner. But in fact @batmobile is the one that said what i was looking to know, Shady. How to use this without EventSystem, it was be so simple..
There is an Add comment button below every post (even comments have a reply button).
The question did not ask how to perform a graphic raycast without the EventSystem. Since every Canvas is tighly linked to the EventSystem and "Graphics" can only exist in a Canvas it actually doesn't make much sense to not use the EventSystem. A specific Graphic implementation might even override it's internal Raycast method to perform further tests which may need the eventsystem to be set in the event that you pass in.
So the accepted answer is the preferred answer.
Please read this meta post on SE when to post answers, when to post comments and when to not post anything. In general you should never post comments / answers which just say:
Thank you
I have the same problem
It doesn't work for me
But how to do XYZ?
On SE thank you posts usually get deleted immediately. If you want to communicate that an answer is good, just upvote the answer, this is enough and doesn't bump the question.
"I have the same problem" in almost all cases is actually wrong. Even the symptomes might be the same the actual problem is specific to you. Do not try to hijack other peoples questions. In most of such questions more information about the problem is needed and the question belongs to the person who posted the question. If you have a similar or related question, post a seperate question. $$anonymous$$aybe add a link to the question you've found already.
ps: I converted your answer into a comment.
Answer by DCTShinobi · Feb 12, 2015 at 10:20 AM
No worries at all. Yes, I believe I figured it out. This is the code I'm using...
PointerEventData cursor = new PointerEventData(EventSystem.current); // This section prepares a list for all objects hit with the raycast
cursor.position = Input.mousePosition;
List<RaycastResult> objectsHit = new List<RaycastResult> ();
EventSystem.current.RaycastAll(cursor, objectsHit);
int count = objectsHit.Count;
int x = 0;
In fact, I already have a script package on the Asset Store. It allows for actions such as moving and resizing UI objects at runtime. The reason I needed the raycast was so multiple scripted objects would play well together... only the fore-most object's script would be affected by a mouse-drag. You can actually test it in the following demo. Just click the "Multiple Windows" button.
Here are two web demos the scripts in action so far...
I'm actually hoping to update it within a few days with the ability to use custom cursors when the cursor is over a moveable/resizable position... which also uses the raycasting. It would have been updated already, but I keep thinking up new features to add.
The next update will include a script to minimize, maximize, and restore an object. It will also include a script that allows one object to push or pull another object's position or specific edge. And I have also added the ability to restrain objects so they remain on the screen or inside their parent.
Here is the link to the asset... Move & Resize UI
I am still quite the newbie, so I'm sure the scripts are nowhere near perfect, but I've learned so much already. The whole reason for this project was because I'm an old-school Morrowind fan. I absolutely love that game! So I wanted a way to make a user interface that is so user-friendly and customizable. So with these scripts, I'm coming much closer to my goal. I actually ALMOST have a working replica of an inventory menu! :-D
Your answer
Follow this Question
Related Questions
Unity UI - Select with Raycast (Screenspace - Camera) 0 Answers
Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers
understand how "Raycast Target" works on UI elements 2 Answers
Help, Use event system IPointerClickHandler 1 Answer
Unity 4.6 UI - Ignore raycast -1 Answers