Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI Button transition from the code

Discussion in 'UGUI & TextMesh Pro' started by mgregoirelds, Jun 27, 2018.

  1. mgregoirelds

    mgregoirelds

    Joined:
    Feb 3, 2017
    Posts:
    22
    Hello,

    I am having a question about triggering button transition from the code.

    I have two buttons on my HUD to swap current equipped item (swap left and swap right). Both of them have Sprite Swap transitions with all 4 sprites linked in the editor. Is there any way for me, in the code, to trigger moving from one state to another?

    An example is that in an Update loop, I'm looking for button press on left or right. When I detect one of those, let's say left, I'm calling leftButton.onClick.Invoke(). I would have expected to see the sprite swapping being done upon the call Invoke().

    Since it didn't work, I thought that there could be a way to trigger the sprite swap manually on the button within my linked onClick event method. However, I can't find no method provided to do this. Is this something doable at all?

    My last solution would be to manually, in the onClick event method, change the sprite of the button to the one that should be displayed when the button is pressed.

    Any help regarding this would be appreciated! Thanks!
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    you can call
    Select()
    for the button... but I think this will just trigger the highlight state.
    What you can do is creating another class which derives from Button. In this class you can have a method which can call another method which is only visible for derived members. Like this:
    Code (CSharp):
    1.         public void SetPressedState()
    2.         {
    3.             base.DoStateTransition(SelectionState.Pressed, false);
    4.         }
    (please note that I did not test this code)
     
    Oyedoyin1 and mgregoirelds like this.
  3. mgregoirelds

    mgregoirelds

    Joined:
    Feb 3, 2017
    Posts:
    22
    Thanks a lot, I will test this out!
     
  4. leosteeves

    leosteeves

    Joined:
    Jul 31, 2018
    Posts:
    2
    Could you explain how to call the method? I've set this up so far but I can't find a way to call SetPressed() in a Monobehaviour.
    Code (CSharp):
    1.        
    2. public class SetTransition : Toggle
    3. {
    4.     public void SetPressed()
    5.     {
    6.         base.DoStateTransition(SelectionState.Pressed, false);
    7.     }
    8. }
    9.  
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    please explain when you want to call this method.
    you could either toggle it via an event which you define in the inspector or call it from some custom code you have.

    for the latter: just make a serialized field for your type and assign it in the inspector. Here an example:
    Code (CSharp):
    1. public class MyMonoBehaviour : MonoBehaviour
    2. {
    3.     [SerializedField] SetTransition _toggle; // <- assign via inspector
    4.  
    5.     public void MyMethod()
    6.     {
    7.         // some logic ...
    8.         if(_toggle != null)
    9.             _toggle.SetPressed();
    10.     }
    11. }
    EDIT: Instead of assigning the toggle, you could also get it via myGameObject.GetComponent<SetTransition>();
    However, I usually prefer the serialized field.
     
  6. leosteeves

    leosteeves

    Joined:
    Jul 31, 2018
    Posts:
    2
    Yeah I know how to do that. I meant that SetPressed() doesn't show up as a method for Toggle classes and figured there was some other way I was supposed to do it. Could you show me the whole script containing your SetPressedState() function because I must have set something up wrong.
     
  7. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Probably you have the variable declared as Toggle instead of your kind of class.
    Your class is called "SetTransition" (by the way: that is not a good name, because you wouldn't expect from the name that it is basically a toggle).

    so this line is important:
    Code (CSharp):
    1.     [SerializedField] SetTransition _toggle; // <- assign via inspector
    it must not be:
    Code (CSharp):
    1.     [SerializedField] Toggle _toggle; // <- not Toggle, but your class