Button

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 19: Line 19:
 
   
 
   
 
  private var state = 0;
 
  private var state = 0;
   
+
  private var guiTexture : GuiTexture;
 +
 
 +
guiTexture = GetComponent(GuiTexture);
 +
 
 
  function OnMouseEnter()
 
  function OnMouseEnter()
 
  {
 
  {

Revision as of 05:16, 7 December 2005

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 normalMaterial : FileTexture;
var hoverMaterial : FileTexture;
var pressedMaterial : FileTexture;
var messagee : GameObject;
var message = "ButtonPressed";

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

function OnMouseDown()
{
	state++;
	if (state == 2)
		guiTexture.texture = pressedMaterial;
}

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

function OnMouseExit()
{
	if (state > 0)
		state--;
	if (state == 0)
		guiTexture.texture = normalMaterial;
}

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox