Support => NGUI 3 Support => Topic started by: Zenneth on July 30, 2012, 04:20:43 PM
Title: Change Sprite image in Runtime
Post by: Zenneth on July 30, 2012, 04:20:43 PM
I'm currently making a card game at the moment, and the thing is that I wanted the card to able to switch to another sprite at run time. However, I couldn't find any reference or example on how to achieve this. Could anyone help?
Also, could it be possible to use a sprite (a portion of an Atlas) as texture on a gameobject instead the entire Atlas?
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on July 30, 2012, 06:04:49 PM
"spriteName" field is what you're looking for.
yourSprite.spriteName="Your New Sprite";
You can use a part of an atlas on your game object if you set up its material, but if there is any tiling involved then it won't work.
Title: Re: Change Sprite image in Runtime
Post by: JRoch on July 30, 2012, 06:12:54 PM
A little extra help, in case you're not familiar with components:
// assuming gObj is a variable containing a reference to the GameObject that has the UISprite attached
Title: Re: Change Sprite image in Runtime
Post by: Zenneth on July 31, 2012, 09:50:05 AM
It works now, Thank you.
Title: Re: Change Sprite image in Runtime
Post by: SkaiCloud on September 16, 2012, 10:11:18 PM
Hello, I gave in and finally registered after 6 months of using NGUI. I love it. GJ ArenMook! I was able to find most of my answer just by searching but unfortunately I have to ask this one.
Ask you can see I switch from sprite one to sprite 2 during run-time, however sprite two still uses sprite one local scale of 100x100.
how can I access the sprite size during runtime and change it?
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on September 16, 2012, 11:33:30 PM
Call yourSprite.MakePixelPerfect();
Title: Re: Change Sprite image in Runtime
Post by: SkaiCloud on September 16, 2012, 11:42:25 PM
Hi ArenMook Ty for the reply. I honestly have tried that and it didn't work out for me. I think its because of my lack of understanding of how to build a GUI with NGUI properly.
Is there a method that can access the sprite width and height data? If I have access to that data I honestly think I can fix my issue but unfortunately I can't access the Sprite Size data in the atlas.
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on September 16, 2012, 11:43:58 PM
It won't work if you're using a sliced sprite. Make sure you're using a regular UISprite, not a UISlicedSprite.
Title: Re: Change Sprite image in Runtime
Post by: SkaiCloud on September 16, 2012, 11:51:05 PM
TY! Kind Sir ;D I must admit I never really understood the different between a slice sprite an a basic sprite.
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on September 16, 2012, 11:51:52 PM
Title: Re: Change Sprite image in Runtime
Post by: SkaiCloud on September 16, 2012, 11:56:35 PM
Ohh I see! Slice sprite are better used for panels, buttons, mostly gui stuff. I was using slice sprite for my game icons like "Superword Flame" kinda stuff. Not only did you solve my issue but you educated me as well. Much appreciated, Now I really don't feel bad paying for NGUI full price lol....dang me.
Title: Re: Change Sprite image in Runtime
Post by: nah0y on November 01, 2012, 10:50:19 AM
Small update about this post, it happens sometimes that even when doing this :
mySprite.spriteName="myNewSpriteName";
mySprite.MakePixelPerfect();
the sprite is not displayed with the correct size.
Is there a reason for that ?
Thanks.
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on November 01, 2012, 12:59:41 PM
Sliced sprites handle MakePixelPerfect differently than regular sprites.
Title: Re: Change Sprite image in Runtime
Post by: nah0y on November 01, 2012, 01:19:49 PM
No I mean, i'm using a normal sprite.
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on November 02, 2012, 02:35:45 AM
Then it has proper size. Keep in mind some pixels get cut off when a texture gets packed in the atlas, and get replaced with padding instead. When this happens, the actual "scale" of the sprite may seem smaller than it should be, but it is in fact correct.
Title: Re: Change Sprite image in Runtime
Post by: nah0y on November 04, 2012, 01:25:14 PM
I see what you mean, but this is not my problem. I actually see the sprite that is stretched. The problem does not appears on the Editor but on iOS...
Title: Re: Change Sprite image in Runtime
Post by: ArenMook on November 04, 2012, 01:50:34 PM
I can't tell you what's wrong then. The behaviour should be exactly identical. NGUI doesn't do anything different on iOS.
Title: Re: Change Sprite image in Runtime
Post by: nah0y on November 04, 2012, 02:39:31 PM
Yup I know... but I have what's attached :(
(http://i.imgur.com/uGbE5.jpg)
In some other projects, I had to create a coroutine and do :