I have been struggling with the new UI system trying to figure out a way to use C# to make a single button toggle an animation on and off with the sound. I currently can get the new UI system to work what I want with two buttons, one button turns off the animation and the other turns it back on but ideally I want to just use one button to toggle the animation and sound on and off.
This is the code I have been using for each button to call to. Is there a simple way to do this with just one button?
using UnityEngine;
using System.Collections;
public class UIManagerScript : MonoBehaviour {
public Animator LEDlights;
public void StopLEDLightsAnimation()
{
LEDlights.SetBool("isPlaying", true);
}
public void PlayLEDLightsAnimation()
{
LEDlights.SetBool("isPlaying", false);
}
}
Here’s a toggle function. Logical condtions can be passed as bools, so:
public void PlayLEDLightsAnimation()
{
LEDlights.SetBool("isPlaying", !LEDlights.GetBool("isPlaying") );
}
For anyone who doesn’t understand what happens here:
LEDlights.GetBool(“isPlaying”) returns either true or false. This can be passed as an argument to a function call just like an int or float or GameObject.
The ! at the begining reverses it. It is read as “not”, and functions like a logical NOT operation.
!true == false
!false == true
shriya
2
Hi,
Follow the below mentioned link
http://www.raywenderlich.com/79031/unity-new-gui-tutorial-part-2
It will help you understanding