Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jimbbq on April 01, 2014, 08:33:11 AM

Title: turning a normal button into on-off switch button
Post by: jimbbq on April 01, 2014, 08:33:11 AM
Hi

I am trying to write a script to turn a button into an on-off switch button: (see below)

It works when my mouse pointer is within the button area, but as soon as my mouse pointer moves out of the button, the sprite changes back to the original sprite. How do I address this?



   public bool isOn; // Clicking will toggle the button on and off. Starts off
   public string spriteName_On; // The name of the sprite when it's on
   public string spriteName_Off; // The name of the sprite when it's off


   public void OnClick()
   {
      if (isOn)
      {
         //Is on, turn off
         this.GetComponent<UISprite>().spriteName = spriteName_Off;

      }
      else
      {
         // Is off, turn on
         this.GetComponent<UISprite>().spriteName = spriteName_On;
      }
      
      isOn = !isOn; // Toggle it so it flips between on and off
   }

Title: Re: turning a normal button into on-off switch button
Post by: ArenMook on April 01, 2014, 10:16:47 AM
Just use the Toggle component. Look at how it's used in the Tabs example.
Title: Re: turning a normal button into on-off switch button
Post by: jimbbq on April 01, 2014, 04:09:51 PM
thanks for the tips! i got it working now :)