Button

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
Line 8: Line 8:
  
 
==Usage==
 
==Usage==
Attach this script to a GuiTexture object. Add a ''ButtonPressed'' function to the same object to catch when the button has been pressed. (You can change the name of the function by changing the *message* variable.)
+
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 - Button.js==
 
<javascript>
 
<javascript>
  var normalMaterial : Material;
+
  var normalMaterial : FileTexture;
  var hoverMaterial : Material;
+
  var hoverMaterial : FileTexture;
  var pressedMaterial : Material;
+
  var pressedMaterial : FileTexture;
 
  var messagee : GameObject;
 
  var messagee : GameObject;
  var message = "ButtonPress";
+
  var message = "ButtonPressed";
 
   
 
   
 
  private var state = 0;
 
  private var state = 0;
Line 24: Line 24:
 
  state++;
 
  state++;
 
  if (state == 1)
 
  if (state == 1)
  guiText.material = hoverMaterial;
+
  guiTexture.texture = hoverMaterial;
 
  }
 
  }
 
   
 
   
Line 31: Line 31:
 
  state++;
 
  state++;
 
  if (state == 2)
 
  if (state == 2)
  guiText.material = pressedMaterial;
+
  guiTexture.texture = pressedMaterial;
 
  }
 
  }
 
   
 
   
Line 48: Line 48:
 
  state = 0;
 
  state = 0;
 
  }
 
  }
  guiText.material = normalMaterial;
+
  guiTexture.texture = normalMaterial;
 
  }
 
  }
 
   
 
   
Line 56: Line 56:
 
  state--;
 
  state--;
 
  if (state == 0)
 
  if (state == 0)
  guiText.material = normalMaterial;
+
  guiTexture.texture = normalMaterial;
 
  }
 
  }
 
</javascript>
 
</javascript>

Revision as of 05:12, 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;

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