How to use/implement `IMoveHandler`?
There are several GameObject
s in my scene that I want to move when the move event occurs (via keyboard for example). So I wrote a C# script implementing IMoveHandler like this and attached it to those GameObject
s:
public class MyMoveHandler : MonoBehaviour, IMoveHandler {
{
public void OnMove(AxisEventData eventData) {
Debug.Log("Hello Move!");
}
}
I have tried adding a Physics Raycaster to the Main Camera without any success.
I don't understance how BaseInputModule works with event handlers: how can a BaseInputModule
acquire those event-handling GameObjects
? The doc only says:
A base module that raises events and sends them to GameObjects. An Input Module is a component of the EventSystem that is responsible for raising events and sending them to GameObjects for handling.
In my case it seems that the input module sends no message to my objects. I know there's the `EventTrigger`, but I want to know why my script doesn't work.
So, what you have now is: "a script implemented I$$anonymous$$oveHandler", "an EventSystem object in the hierarchy tree", "Physics Raycaster component in$$anonymous$$ain Camera".
what you are expecting now is: "Press 'left', 'right', 'up', 'down' on keyboard, then On$$anonymous$$ove(AxisEventData)
in a script attached to game object get called, and do something there."
Correct?
Yes. That's what I expected. But it seems it doesn't work like that. When I press the arrow $$anonymous$$eys nothing happens. I have to write ExecuteEvents.Execute (Player, eventData, ExecuteEvents.moveHandler);
somewhere else to make the event fired on the object. So there's no easy way to notify all the GameObjects that can handle the $$anonymous$$ove event(say if I want to move several GameObjects at the same time)? I thought about using tags. But that will make the scene difficult to maintain. So I'm looking for a clean way to do this.
Answer by Verpous · Jul 16, 2018 at 07:23 PM
I'm sure you've solved it by now, but I just hit the same issue and realized what the problem was. a GameObject has to be selected to receive OnMove calls.
What do you mean under GameObject has to be selected. GameObject should be under pointer or what? Could you explain, please. I have NavigationController attached to Canvas Image. OnScroll and OnDrag events works well in this script but On$$anonymous$$ove doesn't. I thought may be event happens when the GameObject moves, but when I move Image nothing happens too.
Answer by IgorZ · Sep 03, 2019 at 05:21 AM
It doesn't work for me either. I declare the interface and get no updates. How do I "select" the object for the events? As of now my best option is defining Update
method, but I don't like the idea it's being executed every single frame even when the UI control is not focused.
Answer by melkior · Jul 11, 2020 at 06:36 PM
Was having some trouble with this today and googled and found this page; but no answer yet so I dug in a bit and came up with the following based on documentation here : https://docs.unity3d.com/540/Documentation/ScriptReference/EventSystems.AxisEventData.html .
public void OnMove(AxisEventData eventData)
{
if (eventData == null)
return;
var moveDir = eventData.moveDir;
var moveVector = eventData.moveVector;
var selectedObject = eventData.selectedObject;
}
You may need to ensure your scene and objects have the following:
an EventSystem in scene
PhysicsRaycaster on MainCamera
a collider (2d/3d) on gameobject
rigidbody on gameobject
So make sure you have your scene set up first - or nothing will ever call the event system!
Once you've done that you can make sure the event is not null and then access the AxisEventdata -- which in my case I care about the eventData.moveDir (tells me which direction its being moved in!).
I added a couple of other examples to some of the accessible event data for future reference in case it was unclear you can access more than one bit of the event data.
So depending on what you want just grab the proper item and then make some code based off of it like so:
if (moveDir == MoveDirection.Up) { //do something }
Your answer
Follow this Question
Related Questions
(Solved) OnPointerUp not called 1 Answer
Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android) 2 Answers
Make Standalone Input Module ignore mouse input? 2 Answers
2 or 3 OnClick Events Lost When Game Regains Focus 0 Answers
Unity 5.2 - How can I keep other players from using a menu? 0 Answers