2D Toolkit Forum

2D Toolkit => Support => Topic started by: cloudcamaleoniv on May 24, 2013, 10:19:58 pm

Title: SetSprite, polygon colliders not updating
Post by: cloudcamaleoniv on May 24, 2013, 10:19:58 pm
First, sorry for my "questions spam". I've got A LOT of questions cos I'm newbie with unity and 2dtoolkit. Most of them I find easily in the forums, but some of them don't.

Well, ok. I got a Collection with 2 sprites in it. It's two 'pointy rocks'. The first one is bigger and the second is smaller. Since they're so pointy, I can't use box colliders. Then I used Polygon colliders.

I've created a tk2dSprite and selected the newly created Collection and selected one of the sprites.

Now, to Instantiate it randomly, I've created a Prefab with this Sprite, and linked the Prefab in a script.

This script is now responsible for instantiating the Prefab in the scene. So, I use the following code:

(I created more variables to make the code easy to follow)


GameObject go = (GameObject)Instantiate(rocksPrefab);

tk2dSprite sprite = go.GetComponent<tk2dSprite>();
int numSprites = sprite.Collection.spriteDefinitions.Length;

sprite.SetSprite(sprite.Collection.spriteDefinitions[Random.Range(0, numSprites)].name);


This is enough to instantiate the tk2dSprite with the RocksCollection in it. But when I do the SetSprite part, it changes randomly to one of the sprites nicely, BUT do not UPDATE the collider. I keep getting the collider which the prefab is set. Why is that? And how can I fix this?

Thanks again.
Title: Re: SetSprite, polygon colliders not updating
Post by: unikronsoftware on May 25, 2013, 11:31:34 pm
Hi,

The reason the collider isn't updated, is that performance updating mesh colliders in Unity is really really bad, bad to the extent that it isn't practical to update them. It isn't much more expensive to destroy and re-add the sprite.

Thing is, there is often a better way to do it - if this is for a background, create a static sprite batcher with a group of them. Updating mesh colliders is slow, might as well do it on a group of objects instead of one.

Animating them is a big no-no for the same reason, but 2D Toolkit 2.x will let you do this using cached sprites.
Title: Re: SetSprite, polygon colliders not updating
Post by: cloudcamaleoniv on May 26, 2013, 10:10:09 pm
Thanks, but the obstacles are totally random. Like.... uhn.... like Jetpack Joyride. So, creating static sprite batchers are not an option. (at least not by what I understood)

You've said that updating mesh colliders is slow. But even so, how can I do this with the sprites? I reached out an array of Vector3 objects within each Sprite, describing each of its vertices position, but I couldn't pass this information to the mesh.

Anyway, I'll explain my issue again. I have a tk2dSprite prefab. This tk2dSprite is linked with a SpriteCollection that have 5 sprites, each one with a differente "polygon collider". I want to instantiate this tk2dSprite prefab using a random sprite of the collection. What is the best way to achieve this? Keeping the polygon colliders intact?

Thanks.
Title: Re: SetSprite, polygon colliders not updating
Post by: unikronsoftware on May 26, 2013, 11:09:52 pm
Easy (and pretty efficient) solution.

1. Create 1 sprite of each type in the scene, keep a reference to each of these. Use any means for this - eg. tk2dSprite.AddComponent is an easy way to do this, or even do it by hand.

2. "Instantiate" using these sprites as the source objects. The instantiated sprites will instantiate the source mesh collider, and this will be much more efficient than creating it each time.

Title: Re: SetSprite, polygon colliders not updating
Post by: cloudcamaleoniv on May 27, 2013, 12:27:07 am
Oh hell, that was great! I used my SpriteCollectionData as a reference to the script and iterate throught its spriteDefinition array on the Start() method to create the references. Now everything is running smoothly.

Thanks man!