Hello Guest

Author Topic: Problem with changing a sprite at runtime  (Read 6916 times)

mosquito

  • Newbie
  • *
  • Posts: 10
    • View Profile
Problem with changing a sprite at runtime
« on: August 13, 2013, 02:02:05 pm »
I have a problem with displaying a loaded sprite at runtime. This is my code:

tk2dSprite background_sprite = background.GetComponent<tk2dSprite>();
      
string collection_name;
string sprite_name;

if (Level == 1)
{
   collection_name = "SpaceBackground_02";
   sprite_name    = "space_02";
}
else
{
   collection_name = "SpaceBackground_01";
   sprite_name    = "space_01";
}

background_sprite.Collection   = Resources.Load(collection_name, typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
background_sprite.spriteId = background_sprite.Collection.GetSpriteIdByName(sprite_name);

Anyway, I start with level 1 and this is a test code to see if dynamic loading will work. I've set up space_01 background in unity editor, but on level start I have this code above executing and it should change the background to space_02 but that doesn't happen. The funny thing is that I see my change in the inspector, but the background stays the same in the game...

I'm obviously missing something here, please help :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #1 on: August 13, 2013, 03:16:57 pm »
Is the sprite collection data object in a resources folder? It needs to be for it to work with Resources.Load

mosquito

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #2 on: August 13, 2013, 03:23:05 pm »
Yes it is.
When I run the game and select background from hierarchy window, inspector shows correct collection and sprite but the game has the old background...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #3 on: August 13, 2013, 03:24:25 pm »
Oh wait. I missed the obvious bit.
You should use SetSprite to switch sprite.
Try this background_sprite.SetSprite( newCollection, sprite_name );

mosquito

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #4 on: August 13, 2013, 03:34:59 pm »
Tnx for the help, but it's still the same...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #5 on: August 13, 2013, 03:39:59 pm »
Is it definitely loading the correct sprite collection? After its loaded, does it show you the correct collection in the inspector?

mosquito

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #6 on: August 13, 2013, 03:44:05 pm »
Yes, after it's loaded, the correct collection and sprite are in the inspector. That's why it's so confusing to me...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #7 on: August 13, 2013, 03:45:14 pm »
Ok. If you can, create a stripped down repro case and email to support at unikronsoftware.com, don't need the contents of your textures or anything so you can blank them out if you need to. This should work.

mosquito

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #8 on: August 13, 2013, 04:10:41 pm »
I found what was the problem. I probably didn't understood you right. When you said "Try this background_sprite.SetSprite( newCollection, sprite_name );", did you mean try only with this or after these two lines?

background_sprite.Collection   = Resources.Load(collection_name, typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
background_sprite.spriteId = background_sprite.Collection.GetSpriteIdByName(sprite_name);


Anyway, I used my old code and then wrote your line and this is wrong because of this part of code:

public void SetSprite(tk2dSpriteCollectionData newCollection, int newSpriteId) {
   bool switchedCollection = false;
   if (Collection != newCollection) {
      collection = newCollection;
      collectionInst = collection.inst;
      _spriteId = -1; // force an update, but only when the collection has changed
      switchedCollection = true;
   }
   
   spriteId = newSpriteId;
   
   if (switchedCollection) {
      UpdateMaterial();
   }
}

In my case UpdateMaterial() would never be called. Now I'm loading a sprite collection from resource and only calling SetSprite function without explicitly assigning new collection and spriteId. I don't know if this is wanted behavior, maybe a Collection set function should call UpdateMaterial...

Tnx for your help and speed... :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with changing a sprite at runtime
« Reply #9 on: August 13, 2013, 04:41:56 pm »
The .Collection itself shouldn't have a setter, but I can't remove it for backwards compatibility, yet.... It will be tidied up in a later release.

Glad you've got it sorted now :)