Search Unity

[Event System] - Add & Remove Event Handlers to Game Objects

Discussion in 'Scripting' started by madclouds, Apr 3, 2015.

  1. madclouds

    madclouds

    Joined:
    Nov 6, 2013
    Posts:
    31
    Hello,

    I've been working on a 2d project for a few months. Previously I've been using TouchScript to handle my mouse/touch input for all my game objects. I've run into a problem when I start using Unity's Canvas UI components where the Canvas events are not blocking the TouchScript events. I've done lots of searching on this topic and have not found an elegant solution.

    Instead, I've now started refactoring my project to use Unity's Event System but now I can't seem to figure out how to Add & Remove event handlers (event triggers) assigned to a gameobject.

    Creating an empty Game Object > Adding a Sprite Component and a 2D Box Collider. Then I add a script that implements the "IPointClickHanlder" interface. Everything works wonderfully and blocks touches as expected. But, after the gameobject is tapped or clicked, I'd like to remove the listener from the game object to prevent double (or triple) taps from happening? Is there an easy way to do this?

    What about dynamically assigning an event handler to a gameobject? I'd love something like gameObject.AddEventListner(EventSystem.IPointClickHandler());

    I've attached a sample Assets folder that contains a scene with 2 canvases and a gameobject to demonstrate how well the raycast blocking works among the different components.

    Any advice or assistance is appreciated.
     

    Attached Files:

  2. madclouds

    madclouds

    Joined:
    Nov 6, 2013
    Posts:
    31
    Any thoughts?
     
  3. madclouds

    madclouds

    Joined:
    Nov 6, 2013
    Posts:
    31
    Maybe I'm asking the wrong question. How are you handling your mobile input?