Button

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - Button.js)
Line 19: Line 19:
 
   
 
   
 
  private var state = 0;
 
  private var state = 0;
  private var guiTexture : GuiTexture;
+
  private var myGuiTexture : GuiTexture;
  
  guiTexture = GetComponent(GuiTexture);  
+
  myGuiTexture = GetComponent(GuiTexture);  
  
 
  function OnMouseEnter()
 
  function OnMouseEnter()
Line 27: Line 27:
 
  state++;
 
  state++;
 
  if (state == 1)
 
  if (state == 1)
  guiTexture.texture = hoverMaterial;
+
  myGuiTexture.texture = hoverMaterial;
 
  }
 
  }
 
   
 
   
Line 34: Line 34:
 
  state++;
 
  state++;
 
  if (state == 2)
 
  if (state == 2)
  guiTexture.texture = pressedMaterial;
+
  myGuiTexture.texture = pressedMaterial;
 
  }
 
  }
 
   
 
   
Line 51: Line 51:
 
  state = 0;
 
  state = 0;
 
  }
 
  }
  guiTexture.texture = normalMaterial;
+
  myGuiTexture.texture = normalMaterial;
 
  }
 
  }
 
   
 
   
Line 59: Line 59:
 
  state--;
 
  state--;
 
  if (state == 0)
 
  if (state == 0)
  guiTexture.texture = normalMaterial;
+
  myGuiTexture.texture = normalMaterial;
 
  }
 
  }
 
</javascript>
 
</javascript>

Revision as of 05:18, 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 myGuiTexture : GuiTexture;
myGuiTexture = GetComponent(GuiTexture); 
function OnMouseEnter()
{
	state++;
	if (state == 1)
		myGuiTexture.texture = hoverMaterial;
}

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

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

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

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox