Author Topic: Change UISprite graphics on runtime  (Read 2419 times)

hellwalker

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Change UISprite graphics on runtime
« on: August 03, 2014, 04:26:19 PM »
Hi, I have sort of shop where I dynamically add items.
I have template NGUI prefab, where I insert string for name, level etc.

I also have sort of database of objects that should appear on shop. This is array of struct class and you just fill out it's child variables, and this data gets copied into NGUI shop template.

now this template also has icon field, icon is ngui sprite. So basically what I want to do is, to be able to set icon image in database, then insert this icon image into shop template. So when shop items are dynamically generated, each item has appropriate icon, name text, descr etc.

Now because ngui sprite has two field, Atlass and Sprite in Atlass, Like this

I have two problems here
1) I'm not sure how to add those Atlas/Sprite fields to my database(adding atlas/sprite as public variables in another class), so I can assign correct icon there
2) I'm not sure how to pass this variable into actual ngui sprite and thus change its icon image.

To sum up, I want to have array of blueprints, based on which I fill out data in template and thus build shop items at runtime.
And the problem I have is assigning icon variable in blueprint, and setting this variable in template.

as an alternative, I have done this with NGUI unity2D sprite, because here it's simple. sprite is just 1 variable, and you can pass it to another object easily, but this generates extra draw calls and I would rather use nguis own sprite.

Thanks and I hope I made some sense.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Change UISprite graphics on runtime
« Reply #1 on: August 03, 2014, 04:46:12 PM »
Premake all the graphics and put them in the same atlas. In your template (I assume prefab?), set to one of the icons in that atlas. In your data, use the names of the sprites instead of picture information. Set the .spriteName on the UISprite when you load the template.

hellwalker

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Change UISprite graphics on runtime
« Reply #2 on: August 03, 2014, 06:48:06 PM »
sprite name as a string?
and feed this string into UISprite?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change UISprite graphics on runtime
« Reply #3 on: August 04, 2014, 09:57:08 AM »
GetComponent<UISprite>().spriteName = "Some Sprite";