Button

Author: Jonathan Czeck (aarku)

Description
This script uses a GUITexture and Unity mouse events to implement a regular push button that behaves properly like Mac OS X.

Warning: As of Unity 3.0.0, this script does not work on iOS devices, as OnMouseUp functions and the like do not work on iOS devices. Use the iPhone compatible version further below.

Usage
Attach this script to a GUITexture object. Add a ButtonPressed function to the object pointed to by the messagee variable to catch when the button has been pressed. (You can change the name of the function by changing the message variable.)

C# - Button.cs
The C# version is a bit different from the JS version. It shows off some new features in Unity 1.2 for automatically attaching the GUITexture component when attaching the Button (using the RequireComponent attribute) and placing the Button in a custom submenu in the Components menu (using the AddComponentMenu attribute.)

It also groups the textures together into a separate object and has a separate method for changing the texture to ease customisation. Note that some properties and methods are marked protected and/or virtual to make it possible to customise the Button class by creating a new subclass. See the ToggleButton snippet for an example on how to subclass the Button class.

C# - Button.cs (iPhone Compatible)
Same as the C# version above, but added support for recognizing touch input from iPhone or Android devices. Also adds callback function for when the button is double-clicked/double-tapped.