Author Topic: change Sprite in UISlicedSprite  (Read 9126 times)

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
change Sprite in UISlicedSprite
« on: July 05, 2012, 04:43:11 AM »
Before posting I searched in the forum, but I dind't find an answer to my question, so I'm asking it here =P

A have a button prefab. I instantiate that prefab, and its background has a UISLicedSprite script on it. I use a manager to instantiate that, and to assign the right atlas to use.
So I reach the gameobject ->
  1. Button.transform.GetChild(0).GetComponent<UISlicedSprite>().atlas
Now, after that, I've to assign the right current sprite wich is in the atlas.spritelist. I tried like this:

  1. Button.transform.GetChild(0).GetComponent<UISlicedSprite>().sprite = myaltas.spritelist[i]  //(this is in a for loop)

But all the button instantiated have all the same sprite (spritelist[0] i think). How can i change that sprite after instantiation?
UISlicedsprite.sprite can change the sprite, or can only be read?



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #1 on: July 05, 2012, 07:33:50 AM »
You change the sprite via the "spriteName" field.

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #2 on: July 06, 2012, 05:22:25 AM »
I can assign the current sprite in use just using the string name? mh mh... I've to change a couple of things..

Thank you =)

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #3 on: July 06, 2012, 06:05:49 AM »
A little more question..

atlas.spritelist[0] isnt' my first sprite. How can i know the order it considers to create this list? I need to move in that list using the index, but i need to know the order it creates.

When i select a sprite to use clicking on "Sprite" in the ispector, they are all in alphabetic order. Evidently it's not the real order in the list. O.o

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #4 on: July 06, 2012, 08:26:11 AM »
I'm not sure why you need the indices. You have a list of sprites, why does it matter what order they are in internally? If you want to run through the list and match the sprite names, you can do that easily.

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #5 on: July 06, 2012, 10:46:15 AM »
Ok, here's my situation:

I have a for loop, I use it to create a selected number of buttons (wich are prefabs instantiated). I set al the parameters in them, and, last thing, I have to set the background's sprite for each one (always inside the loop). So i want to assign to every button created a sprite inside the spritelist of the Atlas in use.

Example:

I know my first 5 buttons must have the first 5 sprites in the altas. So i want to assign button's background -> atlas.spritelist. But it seems to not work. I need to know the order, because i can't assign the sprite using the string's name, it would be too heavy.

Why can't I use the index in the atlas.spritelist[index] as a normal list? Wich order does it use to create the atlas? alphabetic order, First-In order, or just random one?
I used:

  1. button.transform.GetChild(0).GetComponent<UISlicedSprite>().spriteName = atlasTableObjects.spriteList[i].name;

It works, well it changes the sprite, but i need to know the index wich I'm using in that phase of the loop.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #6 on: July 06, 2012, 10:59:29 AM »
The order it uses is the order in which the sprites were added. Indices are meaningless, and can change. Relying on indices is not a good idea.

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: change Sprite in UISlicedSprite
« Reply #7 on: July 06, 2012, 11:20:24 AM »
Ok, I solved using the string name as you said. Maybe is a little cleaner too. I just have to do some changes in my project, but np =P

Thank you for your answers, quick as ever =)