2D Toolkit Forum
2D Toolkit => Support => Topic started by: roid100 on February 26, 2013, 03:13:09 am
-
Hello,
I am almost the beginner.
I searched and use this way to change the sprite image.
tk2dSprite img = GetComponent<tk2dSprite>();
img.collection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SwitchCollectionAndSprite(img.collection, img.GetSpriteIdByName("test"));
When I tested it,
the names of collection and sprite in the Inspector were changed.
But not changed in the game view ( also scene view ).
What is the problem? Please help me.
Many Thanks.
-
Get 1.91, the API is really much nicer to use in there.
tk2dSprite img = GetComponent<tk2dSprite>();
tk2dSpriteCollectionData spriteCollection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SetSprite ( spriteCollection, "test" );
-
I've just updated it. Thanks a lot!
-
tk2dSprite img = GetComponent<tk2dSprite>();
tk2dSpriteCollectionData spriteCollection = (tk2dSpriteCollectionData) Resources.Load ("World1", typeof(tk2dSpriteCollectionData));
img.SetSprite ( spriteCollection, "test" );
Just to confirm, in this example the "World1" name referes to a sprite collection? Does this code works to change the sprite collection of a sprite at runtime?
-
"World1" is the name of the sprite collection data object. Yup, you can use this to change collections at runtime.
-
Hello unikron, another problem arises when I tried to change the sprite: the collider doesn't change to the new one.
Could you please help me with that ?
Thanks very much !
-
The collider won't change if it is a mesh collider. This is because mesh colliders are really expensive to change at runtime. If you know what sprites you'll be changing to, it might be better to create disabled copies of them, and enable when you need to.
Also don't forget, if you want to move a collider you should always have a kinematic rigid body.