Author Topic: SetAsToggleButton() in 3.x  (Read 1959 times)

kamil.slawicki

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
SetAsToggleButton() in 3.x
« on: December 17, 2013, 08:22:28 AM »
Hi there.

I've recently upgraded to 3.x release, I'm on 3.0.7 f3 at the moment. I would like to know what is the new way of implementing SetAsToggleButton() of UIImageButton class. I have tried to search the forum but was unable to find the answer I was looking for.

what I have is a toggle button sprite that works sort of like so:

on state:

[O  ]

off state:

[  O]

Thanks.
Kamil Slawicki

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetAsToggleButton() in 3.x
« Reply #1 on: December 17, 2013, 09:45:49 AM »
Simple sprite with a collider on it and the following script:
  1. using UnityEngine;
  2.  
  3. public class MyToggle : MonoBehaviour
  4. {
  5.     public bool myState = true;
  6.  
  7.     void OnClick()
  8.     {
  9.         myState = !myState;
  10.         GetComponent<UISprite>().spriteName = myState ? "On Sprite" : "Off Sprite";
  11.     }
  12. }

kamil.slawicki

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: SetAsToggleButton() in 3.x
« Reply #2 on: December 17, 2013, 10:18:16 AM »
Thanks for a quick answer.
Is this a regression when comparing to 2.x?
Is it still possible to use the ImageButton to do this?
The reason I'm asking is so the designers can still use the editor to specify the sprites they want to use for each state.
Kamil Slawicki

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetAsToggleButton() in 3.x
« Reply #3 on: December 17, 2013, 11:30:42 AM »
No regression. I don't know what SetAsToggleButton is. The image button script hasn't changed in 18+ months.