Hello Guest

Author Topic: Issue with SetSprite  (Read 4217 times)

Ezro

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Issue with SetSprite
« on: January 13, 2015, 06:51:16 pm »
Hi everyone,

I'm having a bit of difficulty using SetSprite.

Here is my current code:
Code: [Select]
this.GetComponent<tk2dSprite>().SetSprite(spriteManager.GetCharacterModels(), modelID);
My game successfully runs that command, and I'm able to see the sprite loaded in the Unity inspector, but it doesn't actually appear in my game.

In order to get around this, I've been switching sprites to refresh it:
Code: [Select]
this.GetComponent<tk2dSprite>().SetSprite(spriteManager.GetCharacterModels(), modelID+1);
this.GetComponent<tk2dSprite>().SetSprite(spriteManager.GetCharacterModels(), modelID);


Does anyone know how I can resolve this cleanly?

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issue with SetSprite
« Reply #1 on: January 14, 2015, 10:52:15 am »
Hmmm... that shouldn't break. What version of tk2d are you using? Can you send me a repro case of this breaking?

Ezro

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Issue with SetSprite
« Reply #2 on: January 20, 2015, 05:53:44 pm »
Sorry for the delay.

I'm using 2.4.0 currently.

Here's my code:

ClientController
Code: [Select]
void BuildCharacterGO()
    {
        string serializedCharacter = "";
        foreach(string segment in serializedCharacterParts)
        {
            serializedCharacter += segment;
        }
        byteArr = System.Convert.FromBase64String(serializedCharacter);
        byteArr.LoadObjectTree();
       
        selectedCharacter = GameObject.FindGameObjectWithTag("Player");
        selectedCharacter.GetComponent<MovementController>().Initialize();
        selectedCharacter.GetComponent<EquipmentController>().Initialize();
        selectedCharacter.GetComponent<ModelController>().Initialize();       
    }

ModelController
Code: [Select]
public void Initialize()
    {
        spriteManager = GameObject.FindGameObjectWithTag("Sprite Manager").GetComponent<SpriteManager>();
        LoadModel();
    }

public void LoadModel()
    {
        //HACK - Fix this
        //For some reason, the SetSprite won't properly display unless it's set to a different sprite, then set back
        this.GetComponent<tk2dSprite>().SetSprite(spriteManager.GetCharacterModels(), modelID+1);
        this.GetComponent<tk2dSprite>().SetSprite(spriteManager.GetCharacterModels(), modelID);
        this.GetComponent<tk2dSprite>().scale = new Vector3(1, 1, 1);
        Debug.Log("Sprite scale set");
    }

SpriteManager()
Code: [Select]
public tk2dSpriteCollectionData characterModels;

    public tk2dSpriteCollectionData GetCharacterModels()
    {
        return characterModels;
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issue with SetSprite
« Reply #3 on: January 20, 2015, 09:46:19 pm »
That doesn't make sense, it should work fine if the collection != spriteCollection or spriteID != newSpriteID. Please post a repro in the private support forum, I can't guess what is wrong from your code.

Ezro

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Issue with SetSprite
« Reply #4 on: February 05, 2015, 02:20:04 am »
Hey everyone,

Figure I'd update this, in case anyone comes across this issue as well.

In my code, I was rebuilding the player gameobject, as sent from my server. The rebuilt GO had a tk2dSprite component on it already, with a valid texture.

Instead of using the code to switch to modelID+1 then back to modelID, I'm now adding a new tk2dSprite component to it.
(As such, I've removed the tk2dSprite component from the server template, and am also adding the component on the server)