Search Unity

ToggleGroup combined with SpriteSwap doesn't initialze "Pressed Image" when "isON"

Discussion in 'UGUI & TextMesh Pro' started by Starman001, Jun 10, 2019.

  1. Starman001

    Starman001

    Joined:
    Oct 22, 2016
    Posts:
    26
    Hi all,

    i have been googling around the entire afternoon for something that I thought was a very simple fix.

    What I want :
    I'm creating a shop menu with 4 categories, which I want to "memorize" which was opened last while the game plays, and simply use one as default when I start the game / scene the first time. I have created a toggle group with 4 toogles. "Allow switch off" is FALSE, which means that one item MUST be active. I just want to use the first one as default when the scene loads (the menu itself will not deactivate in gameplay, I just use a canvasgroup to set it's alpha to 0 and make it not interactable, that way the last selected shop-subsection will stay active.

    For the visual effect I am using the SpriteSwap Transition, and 4 different coloured images. Technically it works, except that I can't manage to initialize an item in the menu with the "pressed Sprite" active, even though it is "isOn" = true.

    I set the value in the StartMethod of a controller script, which doesn't swap the sprite, but also setting it active on the toggle in the inspector from the very start to "isOn" is not displaying the "pressed Sprite" at runtime, it only displays the image.SourceImage. Since that is the same image as being used for the "currently not selected toggle", that's certainly part of the problem and why a simple image flip doesn't work. Disabled Toggle / Sprite isn't used in my case, that might be while it stopped changing at all then.

    When I start clicking the toggles at runtime, also the initialized one, everything works fine once I switched between them, the "initialzed one" starts working once I switched on another one first. It's just the "pressed image" that refuses to initialize.

    Unfortunately I'm stll a beginner, and I have some troubles setting up an efficient code for the sprite swapping thing, i don't really understand how to deal with that spriteSwap function in the code. I tried creating an additional SpriteSwap and then changing it after the menu opens, and I tried changing out the original image. But the problem is, when I change the background image to the "pressed state" in the default, the sprite later on isn't working for this toggle, it's always pressed and doesn't spriteSwap anymore.

    Am I missing something here ? Surely there must be a reasonable way to initialize something like that when using SpriteSwap. What code is required to "trigger" the sprite swapping. I know there is something called "OnValueChanged", though I also didn't find a solution how to code this for my needs.

    I would appreciate any help here, I can't imagine that I'm the only one trying something like this. Curently using Unity 2018.3.9f1
     
    Last edited: Jun 10, 2019
  2. Starman001

    Starman001

    Joined:
    Oct 22, 2016
    Posts:
    26
    Let me add this :

    From my understanding of the Problem, the event-system is not registering the call to swap the sprites. I found this in the unity docs, but it didn't do the trick, most likely because the synthax is not correct.
    ...
    initialToggle.transition = Selectable.Transition.SpriteSwap;

    (initialToggle is my variable for the toggle gameObject)
     
  3. Starman001

    Starman001

    Joined:
    Oct 22, 2016
    Posts:
    26
    Never mind, found the issue myself.... Since i didn't wanted to use the checkbox image, i removed all subobjects. But the "is checked" functions needs a graphic object on the toggle. Re-added it, and now it works.

    @admin : Feel free to delete the question if you think it doesn't belong here anymore.