Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by LSHi · Apr 25, 2017 at 01:34 PM · inputeventsystemevent-handlingevent handlers

How to use/implement `IMoveHandler`?

There are several GameObjects 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 GameObjects:

 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.

Comment
Add comment · Show 2
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 wonson · Apr 26, 2017 at 06:36 AM 0
Share

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?

avatar image LSHi wonson · Apr 27, 2017 at 02:31 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 HedgehogNSK · Jul 18, 2018 at 06:10 PM 0
Share

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.

avatar image
0

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.

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
avatar image
0

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 }

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

114 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 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 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 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 avatar image avatar image

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


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