Author Topic: [SOLVED] create a Sprite in a GO outside a panel & then add it to a panel?  (Read 1860 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Hello,

I'm still working on my inventory system - I keep finding better ways of doing stuff.

My items in the scene are just gameObjects, with a hierarchy of:

Item: (Has Item script)
-- Model
-- Sprite (Has UISprite)

Of course, one should add widgets to a panel otherwise it wouldn't look right - I was wondering if there's a way to create a sprite just like that (outside a panel) - And then when the item is picked, it gets added to my inventory (which has a panel) - I was hoping I could do that, and the sprite would correct itself- Since the item's parent now is a slot in the inventory - But it didn't seem to correct itself too well.

I tried:
  1. sprite.ParentHasChanged();
  2.  

and:
  1. sprite.panel.Refresh();
  2.  

But none worked :(

Again, I'm creating the sprite in a gameObject (the item) outside any panel - and then adding the item to the inventory which has a panel. How can I tell the item's sprite to correct itself?

If I can't do that, what's the right approach?

Thanks! :)

PS: Currently, I have an ItemTemplate reference in my inventory, whenever I add an item, I instantiate that template and add it as a child to the item and then set the atlas and sprite accordingly all in code. But I found that to be a bit of a hassle - I'm really hoping to get it working the way I want (ie create the sprite outside)
« Last Edit: November 22, 2013, 12:37:49 PM by vexe »

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Sorry - My bad. Something else was affecting the item's scale - thus affecting the sprite. I just had to reset the main item's scale to Vector3.one. sprite.ParentHasChanged(); works :)