#Documentation on InputActions & EventSystem currentselectedgameobject + graphics raycasting

1 messages · Page 1 of 1 (latest)

restive hull
#

Hey, looking for any and all information on InputActions in code and graphics raycasting to detect a UI element on click. Regular physics raycasting doesn't seem to work at all despite there being a collision on it, despite what I read online so graphics raycasting is what I'm looking at trying to work out for that.

Unity version 2021.3. Can't share code snippets but mainly just looking for actual documentation on how any of these systems work since the documentation on the site seems..... very thin.

zealous talon
restive hull
#

But how? I tried looking directly at the eventsystem class but couldn't work out the wanted parameters

zealous talon
#

some of them have examples , some don't sadly.
I think there was a better page somewhere, forgot the link

#

they work like any other interfaces, if you have a configured IDE it should generate the method for you

restive hull
zealous talon
#

no they come from the EventSystem because they can be used in other raycasters, like Physics one etc

restive hull
zealous talon
zealous talon
#

PointerEventData is where its storing information about the event that happened, such as a click, location etc

restive hull
#

public void RaycastAll(EventSystems.PointerEventData eventData, List<RaycastResult> raycastResults); this is it, Raycast all is what I was trying to use

#

The list is obvious, but yeah it was pointereventdata I couldn't work out what to give it

zealous talon
#

oh that one

#

if thats what you want to use, you need to create it and pass it info you want such as your mousePos
Vector2 screenPosition = Mouse.current.position.ReadValue();

 PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = screenPosition;```
restive hull
#

Oh I was half there then lol, I tried mouse.current.position lol

#

I'll probably wake up and think of a sane way of doing all of this but it's at least good to know how to use these functions

#

are the raycast results in it gameobjects? or can I easily get the object from it

zealous talon
#

ya depends what you're doing specifically, sometimes its easier to use the interfaces

zealous talon
#
 foreach (RaycastResult result in results){
            Debug.Log("Hit: " + result.gameObject.name);
        }```
restive hull
#

basically I have a button that when clicked with a mouse, it'll click it like a normal button
when a controller hits a or something on the same button, it needs to hold it down, a bar progresses to see how long you have held it for, then button does what button does.

#

I had to break clicking it with a mouse to stop the controller hitting the onclick event automatically, and thought just grabbing the button with the mouse would be simple but then went down the rabbithole of graphics raycasts lol

zealous talon
#

iirc onclick works with a default button

restive hull
#

wondering now I'm saying it all out if it'd just be easier to have the hold behaviour happen regardless of input device to make the experience consistent. Even if on a mouse it'd be a lil clunky as generally buttons don't work like that with mouse clicks

restive hull
zealous talon
#

you should be able to use IPointerDownHandler, IPointerUpHandler
for both

restive hull
#

for both mouse and controller?

zealous talon
#

something like

 public void OnPointerDown(PointerEventData eventData){
        StartHolding(); //start timer in this method
    }

    public void OnPointerUp(PointerEventData eventData){
        ResetHolding();
    }```
zealous talon
restive hull
#

so that, plus the graphics raycast to make sure it's on the button, then timer increases on the hold until a set time, and increasing the progress bar at the same time, do action when timer reaches number

#

I think is the order of operations

zealous talon
#

technically those interfacemethods go on a script that sits on button/graphic element itself so it already knows you're on that button

restive hull
#

does onpointerdown (etc) work if the main input thing is on a different object elsewhere?

restive hull
#

OH DOES THIS MEAN I CAN GET THOSE CALLBACKS ON DIFFERENT OBJECTS??

zealous talon
restive hull
#

oh that's perfect, that makes everything a million times easier

restive hull
#

Thank you, gonna head to bed then work on this tomorrow and hopefully now I understand all this way more it'll click lol

restive hull
#

Works for mouse but not gamepad

zealous talon