Hello Guest

Author Topic: Issues Setting tk2dSpriteCollection At Runtime  (Read 4661 times)

dereklgg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Issues Setting tk2dSpriteCollection At Runtime
« on: September 02, 2014, 04:11:12 pm »
Hi,

I'm having an issue changing the tk2dSpriteCollection of a tk2dSprite at runtime.

The tk2dSpriteCollectionData object is in the Resources folder and loaded at runtime using Resources.Load (I have also tried dragging them into public variables in the inspector, but had the same results).

When I call the SetSprite function and pass the tk2dSpriteCollectionData object to it, the sprite correctly changes to the new collection. When this is done, however, other sprites in the scene (that are using completely different sprite collections) lose their material references. The material on these sprites changes to "Default-Diffuse (Instance)" instead of the "tk2dInternal$.Material_..." material. This only happens to some sprites; others in the scene that reference the same collection are fine. The affected sprites are not random, but they appear to have no connection. In addition, I receive numerous "Shader wants normals, but the mesh  doesn't have them" errors in the log.

If I call ForceBuild on all sprites in the scene after changing the sprite collection the sprites seem to fix themselves, but I still get the "Shader wants normals" errors. This issue only occurs when changing the sprite collection of sprites at runtime, otherwise the scene works properly.

Not sure if this is relevant, but I am changing multiple sprite collections in the same frame.

Any assistance would be greatly appreciated.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issues Setting tk2dSpriteCollection At Runtime
« Reply #1 on: September 03, 2014, 12:28:32 pm »
Can you please create a minimal repro case for this. It should be fine to change sprite collections at runtime, as many as you like, but I can't think of any reasons why it would break in the way you're describing.

The "Shader wants normals, but the mesh  doesn't have them" error is to be expected if the material on the sprites has changed to Default-Diffuse.

dereklgg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Issues Setting tk2dSpriteCollection At Runtime
« Reply #2 on: September 03, 2014, 03:50:55 pm »
I've been attempting to reproduce this all morning on a fresh project but haven't had any luck.

In my current project, however, I have narrowed it down to one line. If I comment out Line 85 in tk2dEditorSpriteDataUnloader.cs...

Code: [Select]
DestroyInternalObjects( allObjects.ToArray() );
...then the issue goes away. This leads me to believe that the issue only happens when running in the editor. Does this help at all?

EDIT: Just verified that it is only happening when running in the editor.
« Last Edit: September 03, 2014, 03:59:09 pm by dereklgg »

dereklgg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Issues Setting tk2dSpriteCollection At Runtime
« Reply #3 on: September 03, 2014, 03:51:42 pm »
I'll also add (not sure how relevant this may be) that the Sprite Collections are being changed in a Start() method.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issues Setting tk2dSpriteCollection At Runtime
« Reply #4 on: September 04, 2014, 09:49:23 am »
Hmmm I will need to investigate this properly. For now - I recommend exiting out of DestroyDisconnectedResources() when Application.isPlaying is true. Disabling it entirely will mean your sprites will never get unloaded, which is really bad, but stopping it while its playing should give you a happy medium.

dereklgg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Issues Setting tk2dSpriteCollection At Runtime
« Reply #5 on: September 04, 2014, 01:55:59 pm »
Great, I will do just that. Thanks!