ToggleButton

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

Jump to: navigation, search

Author: Keli Hlodverrson (frayr)

Description

This script uses a GuiTexture and Unity mouse events to implement a regular toggle 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.)


C# ToggleButton.cs

The ToggleButton is created by extending the Button class. You'll need to have both files present in your project.

<csharp> using UnityEngine; using System.Collections;

[AddComponentMenu ("GUI/Toggle Button")] public class ToggleButton : Button {

   public ButtonTextures selectedTextures;
   public bool selected=false;
   protected override void SetButtonTexture(ButtonState state) {
       ButtonTextures textureSet=selected?selectedTextures:textures;
       switch(state) {
           case ButtonState.normal:
               myGUITexture.texture=textureSet.normal;
               break;
           case ButtonState.hover:
               myGUITexture.texture=textureSet.hover;
               break;
           case ButtonState.armed:
               myGUITexture.texture=textureSet.armed;
               break; 
       }
   }
   
   public override void Reset() {
       selected=false;
       base.Reset();
   }
   
   public override void OnMouseUp()
   {
       
       if (state == 2)
           selected = !selected;
       
       base.OnMouseUp();
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox