Blog

Collider Button

Buttons should be easy and now they are. Collider Button is a simple component that allows any collider to become a button for use in your scene. Whether its a simple prototype that doesn’t need a GUI system in place quite yet or if you simply want an easy to use 3D-based button, Collider Buttons aims to make things simple.

Sender and receiver messages are visualized for easier debugging.[/frame_left]Collider Button fits into any workflow by allowing two approaches for use: inspector broadcasting and event listening. For inspector use without coding, simply drag a GameObject into the Event Target slot and any and all methods in every component on the GameObject will be available from the Function Name pulldown to be fired when the object is clicked. For event listening simply make a reference to a Collider Button and register for the OnPress and OnRelease events.

Collider Button needs to know which camera is rendering the GameObject it is attached to so it can correctly RayCast against the collider. If you leave Detect Rendering Camera to true (default), Collider Button will automatically set up the correct reference to the first camera that is rendering this object. If you’d like specific control just set the renderingCamera to whatever you need.

Collider Button works for touch and mouse environments and all camera raycasting is combined to maximize performance. As always, report bugs and stay tuned for enhancements.

Download

(The car in this example is from the Unity Car Tutorial freely available in the Asset Store – I only added a custom shader to it but the credits go to Unity Studios for the car model and textures)

13 Comments

  1. October 17, 2012

    I love the editor trick you’ve done with the drop down function box for selecting the proper function on the target object. I need to put that on my proximity detector code! Currently, you have to do it by literal string, which is tedious.

  2. October 17, 2012

    How about a Gizmo option where it draws a line to it’s connected GameObject in the scene? Could be helpfull to see the wiring, and should be quite easy.

    • October 17, 2012

      This is a great idea. I just added it and there is now lines displayed that show connections. Since there’s no simple arrow available with Unity Gizmos it simply draws lines that go from red to green so you can easily see the direction of the messages.

      Thanks for the idea!

  3. October 18, 2012

    Nice functionality, great looking car 😉
    Well done
    Best
    T

  4. Thomas
    November 5, 2012

    Hi, I’m having a problem where the Function Name selection in the editor isn’t sticking. The list shows up ok, but when you click off and back on the GameObject, or run the scene, it switches back to (No Function Selected). Have tried on a new empty scene, same behaviour. Any thoughts? Perhaps it’s refreshing the list and not remembering the selection?
    I’m using Unity 3.5.6f4 Pro, targetting iOS.
    Thanks.

    • Thomas
      November 5, 2012

      Just tried the example scene, and interestingly it works until I select one of the arrows – at this point the Function Name link is lost.

    • November 5, 2012

      I updated things last week to fix this. Can you clear your cache, re-download and let me know if this is still occurring for you?

    • Thomas
      November 5, 2012

      It seems I had the old version. Working now, thanks 🙂
      Nice little component!

  5. January 9, 2013

    I think I’ll slightly modify this so that also if my finger passes through the object when Leap is used it will also act as a trigger. 3D hud with oculus and leap is going to be amazing. My leap is on it’s way now. Awesome!

  6. January 24, 2013

    Thanks for the script! Just wanted to share a couple gotchas I ran into.

    In ColliderButton.cs, OnDestroy() is misspelled at OnDestory(), at least in the version I downloaded.

    Also, I ran into a problem where button clicks were duplicated when I switch the GameObject active on and off. To fix that I added to ColliderButton.cs:

    void OnDisable(){
    ColliderButtonManager.UnRegister( this );
    }

    Peace!

    • January 24, 2013

      Thanks a ton for those catches and thanks for the support! I’ve fixed these and replaced the package.

    • January 24, 2013

      Thanks for the quick response!

      I have a question. Is there a simple way of adding events for MouseOver and MouseOut? Ideally I think I’d like to add listeners to the ColliderButton script, then drop components on that’ll listen for those events and have different actions, like scaling, tinting or playing a sound. I’m not sure if there’s something about how the script is setup to make that difficult.