2D Toolkit Forum

2D Toolkit => Support => Topic started by: saygon on February 18, 2013, 11:33:20 pm

Title: Sprite.ForceBuild() errors perfomance.
Post by: saygon on February 18, 2013, 11:33:20 pm
Hi there.
I use serilazation from xml and want to create sprites in runtime. My strategy is follow:

1. Instantiate sprite from sprite prefab.
2. Select proper spriteid and collection (call SwitchCollectionAndSprite).
3. After this sprite images are changed, but collider are not created properly - collider is still from old sprite.
4. Call ForceBuild(). After this new collider will be created.

But step 4 occurs to "Invalid mesh collider on sprite, please remove and try again." error.
In this post: http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1131.msg5381.html#msg5381 (http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1131.msg5381.html#msg5381)  I read that it's error in unity and it's not a fatal and it's resolved in short time.

But it's in editor mode - I create sprite in runtime, so perfomace is not clear for me - I have 90 errors in editor console for one level load (1 error for one sprite creation).
Can this lead to application crash? Should I wrap this in try-catch (but in this case I will have >90 call of catch block and it's very perfomance expensive).
Maybe exists some workaround for this stuff (Create sprites in runtime with different collider types)?
I see that  EditMode__CreateCollider() did not lead to exceptions or errors, but it annotated as call only in editor. Can I use it (with modification of course)?

Thanks in advance.

Update:
ForceBuild works in Editor but does not work on release build (win standalone) - collider from old sprite is still exist.
I modified EditMode__CreateCollider() method (remove #if Editor directives) and call it from script instead ForceBuild() - seems like issue fixed.
I will try this workaround on Android and let you know about results.
Title: Re: Sprite.ForceBuild() errors perfomance.
Post by: unikronsoftware on February 19, 2013, 05:48:06 pm
If you're Instantiating for the sole purpose of switching to the correct sprite, why not just use
GameObject go = new GameObject();
tk2dSprite.AddComponent (go , collection, spriteId );

That should create the sprite for you, and not go through that bit of code at all. All of that is so you can't switch from a box -> mesh collider.