Author Topic: Change Sprite image in Runtime  (Read 39033 times)

Zenneth

  • Guest
Change Sprite image in Runtime
« 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #1 on: July 30, 2012, 06:04:49 PM »
"spriteName" field is what you're looking for.
  1. 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.

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Change Sprite image in Runtime
« Reply #2 on: July 30, 2012, 06:12:54 PM »
A little extra help, in case you're not familiar with components:

  1. // assuming gObj is a variable containing a reference to the GameObject that has the UISprite attached
  2.  
  3. UISprite yourSprite = gObj.GetComponent<UISprite>();
  4. yourSprite.spriteName = "Your New Sprite";
  5.  

Zenneth

  • Guest
Re: Change Sprite image in Runtime
« Reply #3 on: July 31, 2012, 09:50:05 AM »
It works now, Thank you.

SkaiCloud

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Change Sprite image in Runtime
« Reply #4 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.

yourSprite.spriteName = "Sprite One"; //dimension 100x100

//do something

yourSprite.spriteName = "Sprite Two"; // dimension 125x150

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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #5 on: September 16, 2012, 11:33:30 PM »
Call yourSprite.MakePixelPerfect();

SkaiCloud

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Change Sprite image in Runtime
« Reply #6 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #7 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.

SkaiCloud

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Change Sprite image in Runtime
« Reply #8 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #9 on: September 16, 2012, 11:51:52 PM »

SkaiCloud

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Change Sprite image in Runtime
« Reply #10 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.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Change Sprite image in Runtime
« Reply #11 on: November 01, 2012, 10:50:19 AM »
Small update about this post, it happens sometimes that even when doing this :
  1. mySprite.spriteName = "myNewSpriteName";
  2. mySprite.MakePixelPerfect();
  3.  
the sprite is not displayed with the correct size.

Is there a reason for that ?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #12 on: November 01, 2012, 12:59:41 PM »
Sliced sprites handle MakePixelPerfect differently than regular sprites.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Change Sprite image in Runtime
« Reply #13 on: November 01, 2012, 01:19:49 PM »
No I mean, i'm using a normal sprite.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Sprite image in Runtime
« Reply #14 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.