Toggle => select target graphic 'asset' and 'scene' windows are empty...

Hi FNG new to Unity here, coming from Flash where I was a solid intermediate user comfortable with loops, arrays, booleans etc… It’s been an adventure moving to Unity to say the least (though C# feels similar enough). I find this development platform a little buggy coming in fresh but that impression may change as I become more familiar with it; using 2019.1.14f1…

I want a custom toggle using my own SVGs for up/down states.

[ISSUE]
I’m trying to select the sprites in the inspector for target/selected (within the context of sprite swap transition); when the ‘select graphic’ window appears none of my imported sprites (SVGs) appear under the ‘assets’ tab and the ‘scene’ tab has only 3: Background, Checkmark and Label… what in the world am I doing wrong here?

I’ve been delving through the documentation have not found any solutions. Searching for ‘select graphic’ does me no good in the manual and Googling ‘Unity toggle select graphic’ (and variations thereof) brings up nothing pertinent to my issue…

My sprites appear under the Project/Assets window just fine (SVGs exported from Ps with gradients and simple strokes… boy did that take DAYS to figure out…) and I can drag them to the ‘stage’ or ‘scene’ or whatever… I just need to figure out how to make them available to be used in toggles!

HEEEEELP!
LOL


I think I made a step forward in figuring this out: I think you have to ‘wrap’ your Sprite inside the Toggle>Background then select “Background(Image)” and then ‘assets’ will populate for the ‘selected’ and other states for sprite swap… :face_with_spiral_eyes:

No that ^ didn’t work; it appears SVG support in UI is not implemented even though the developers of Vector Graphics package claimed it is back in 2018…

Hack fix: I set up a boolean to swap between 2 sprites depending on the truthfulness of the var and check for that in update. Probably not ideal, but this is the best I got after 2 days of trying to figure it out… Now I just need to figure out how to add/remove sprites from the stage…

Well, my explanation would be very long, but I’ll shorten it to where at least you get the general idea. Why not create a list and every sprite you put into your scene add to that list using Add()? Then when you want to remove that sprite, (Assuming it’s clicked, focused on, or highlighted) use a for loop to detect which element it is in the list. Then you use Remove() to remove it from the list. Should be fairly simple.

I suppose a dictionary key/pair would be the best bet. So each element you add to the list: Sprite1:0, Sprite2:1, Sprite2:2, etc… So when you click on the sprite you can reference its key value within the dictionary. You’d only need to declare an int variable name key (or something) and increment it each time you add a sprite and decrement it each time you remove an element. so it would actually be: Sprite1:key, Sprite2:key, Sprite3:key, etc…

Just a thought :slight_smile:

1 Like

you can store sprites in the script and put them into the sprite renderer (or other component that can render sprites)

public Sprite spriteOne, spriteTwo;
    public bool isSomethingTrue;

    SpriteRenderer sr;
    private void Start()
    {
        sr = GetComponent<SpriteRenderer>();
        ChangeTheSprite();
    }

    void ChangeTheSprite ()
    {
        if (isSomethingTrue)
        {
            sr.sprite = spriteOne;
        }
        else
        {
            sr.sprite = spriteTwo;
            //sr.sprite = null;
        }
    }
2 Likes

Thank you both! Okay I got this working…kiiiiiinda… and now I am beginning to think Unity doesn’t like SVGs…

So, I get the renderer to add the ‘btn1_upSprite’ AND swap it with the ‘btn1_downSprite’ (hallelujah); BUT, the sprite is malformed in that there are no gradients and an ugly white tick-mark in the corner. I’ve done some google searching but again come up with nothing pertinent to my issue. See the one on the left? (The thin white box on the gray is on the background image…) That’s the one that comes through with the script-controlled sprite renderer… the one on the right is dragged/dropped onto the stage.

5124803--506351--Screen Shot 2019-10-30 at 8.32.40 PM.png

Not sure what gives… I have AA set at 8X and quality on Ultra…

On another note, interesting that Unity has a kind of built-in loop game engine like onEnterFrame from AS3… neat!

[EDIT] When I import these images as PNGs they work much better. I am pretty sure Unity hates SVGs which is really REALLY unfortunate. What a bummer.