Button

From Unify Community Wiki
Revision as of 04:12, 9 December 2005 by KeliHlodversson (Talk | contribs)

Jump to: navigation, search

Author: Jonathan Czeck (aarku)

Description

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

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.)

JavaScript - Button.js

<javascript>

var normalTexture : FileTexture;
var hoverTexture : FileTexture;
var pressedTexture : FileTexture;
var messagee : GameObject;
var message = "ButtonPressed";

private var state = 0;
private var myGuiTexture : GuiTexture;
myGuiTexture = GetComponent(GuiTexture); 
function OnMouseEnter()
{
	state++;
	if (state == 1)
		myGuiTexture.texture = hoverTexture;
}

function OnMouseDown()
{
	state++;
	if (state == 2)
		myGuiTexture.texture = pressedTexture;
}

function OnMouseUp()
{
	if (state == 2)
	{
		state--;
		if (messagee)
			messagee.SendMessage(message, gameObject);
	}
	else
	{
		state --;
		if (state < 0)
			state = 0;
	}
	myGuiTexture.texture = normalTexture;
}

function OnMouseExit()
{
	if (state > 0)
		state--;
	if (state == 0)
		myGuiTexture.texture = normalTexture;
}

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox