Author Topic: turning a normal button into on-off switch button  (Read 4097 times)

jimbbq

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
turning a normal button into on-off switch button
« 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
   }


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: turning a normal button into on-off switch button
« Reply #1 on: April 01, 2014, 10:16:47 AM »
Just use the Toggle component. Look at how it's used in the Tabs example.

jimbbq

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: turning a normal button into on-off switch button
« Reply #2 on: April 01, 2014, 04:09:51 PM »
thanks for the tips! i got it working now :)