ToggleButton

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Eliminated 'CSharp' category in favour of 'C Sharp'.)
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
Line 14: Line 14:
 
''Note that this script uses features only available in Unity 1.2. Read the comments in [[Button#C# - Button.cs|Button.cs]] for more information''
 
''Note that this script uses features only available in Unity 1.2. Read the comments in [[Button#C# - Button.cs|Button.cs]] for more information''
  
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using System.Collections;
 
using System.Collections;
  

Revision as of 20:44, 10 January 2012

Author: Keli Hlodverrson (freyr)

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 for this snippet to work. Note that this script uses features only available in Unity 1.2. Read the comments in Button.cs for more information

using UnityEngine;
using System.Collections;
 
[AddComponentMenu ("GUI/Toggle Button")] 
public class ToggleButton : Button {
 
    // A second set of textures for showing when the toggle button is selected
    public ButtonTextures selectedTextures; 
    public bool selected=false; // The selected/unselected state of the button
 
    // Override the SetButtonTexture method in Button to make it show a 
    // different set of textures when selected is true.
    protected override void SetButtonTexture(ButtonState state) {
        if(selected)
             myGUITexture.texture=selectedTextures[state];
        else
             myGUITexture.texture=textures[state];
    }
 
    public override void Reset() {
        selected=false;
        base.Reset(); // Calls the overridden Reset function.
    }
 
    // OnMouseUp needs to be overridden so we can flip the selected status 
    // when the user clicks the button.
    public override void OnMouseUp()
    {
 
        if (state == 2)
            selected = !selected;
 
        base.OnMouseUp(); // Do whatever the base class used to do...
    }
 
}
</csharp>
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox