Get Name of UI gameobject with raycast
I wont the name of UI gameobject with help of Raycast.
For example if my finger is on any button with name "Cartoon" then i need that name with help of raycast.
maybe try GraphicRaycaster.Raycast http://docs.unity3d.com/462/Documentation/ScriptReference/UI.GraphicRaycaster.html
have not used it before, so i'm not sure if it solves your issue
Answer by meat5000 · Mar 28, 2016 at 07:11 PM
Physics.Raycast doesn't work on a Canvas Graphic but a GraphicRaycaster does.
You can fire one off manually using
myGRaycaster.Raycast(data, rayResults);
where myGRaycaster is your Graphics Raycaster component on your Canvas, data is PointerEventData and rayResults is a List.<RaycastResult>
.
Using the pointer event data supplied the GRaycaster will find all the UI elements touched by the ray and will append them all to the List you supplied.
However, I discovered this can be a needless step if you have included the Pointer Event interfaces in your script.
For example, if you have implemented OnPointerDown or OnDrag etc you can use the PointerEventData populated in that Method to obtain the Raycast information associated with that event without performing an extra raycast.
#pragma strict
import UnityEngine.UI;
import UnityEngine.EventSystems;
public class FiringAGraphicsRay extends MonoBehaviour implements IPointerDownHandler
{
function OnPointerDown(data : PointerEventData)
{
Debug.Log(data.pointerCurrentRaycast.gameObject.name);
}
}
The key here is pointerCurrentRaycast member of the PointerEventData which is generated in our interface.
Answer by Zeoli · Mar 18, 2016 at 03:42 PM
you can use raycasthit object to get what was hit. http://docs.unity3d.com/ScriptReference/RaycastHit.html
The example of how to use this is at the bottom of this page. http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
i know this answer is a bit scarce but I do want to show people HOW to find this info just so they know how to resolve another issue next time.
fyi: the "out" keyword basically fills in that object from within the method called. so out hit gives you a plethora of info regarding what was hit. that you can utilise after the method has been called. May be a good idea to put an if statement on it like if(hit != null) just to make sure that you actually hit something otherwise referencing a property of hit would fire an exception.
I hope this helps, if you need further help, just ask. (make sure you use @Zeoli so i get an email that you mentioned me).
hey @Zeoli
I think this will not work on canvas.. I have try this.
I have try 2d also.. maybe I have done some mistake
$$anonymous$$ay be possible you can share answer.
just to confirm, this is a touch interface I think you are doing? if so, and you are on a canvas which handles UI, cant you just add the button component to it and put the action you wish to happen in there? you could even get the button to change a bool in the gamemanager to say that it is being pressed. then just run a series of Ifs in the game manager. not the most efficient way but it should do the trick.
I would need more info as to exactly what you are trying to achieve, maybe theres a better way?
Answer by markncolleen1985 · Dec 27, 2017 at 11:43 PM
Thanks, this is the answer why vr samples does not work with a canvas ui menu, any idea how to make the menu buttons function wiht a collision as a mouse0 would ? Thanks