2D Toolkit Forum
2D Toolkit => Support => Topic started by: K0mix on September 08, 2013, 03:22:55 pm
-
I just started Using Unity and toolkit 2d.
I ve figured out some basics about how to work with sprites, but I havent figured out how to attach them newly to the scene with code.
the code should look somethign like this
void create(){
var target = new GameObject("sprite1");
var sprite = trg.AddComponent<tk2dSprite>();
sprite.setSprite(mysprites,"ball");
}
now the problem with this is I don't know how to properly access the sprite collection (mysprites) I have set up.
how would I refer to the sprite collection properly?
-
The same way you'd refer to anything in Unity - the easiest way is to create a public tk2dSpriteCollectionData mysprites in your class, and dragging in a reference to the sprite collection data object. Alternatively you can use resources folders and Resources.Load
-
ok very good now I have another problem however: sprite not found in collection: ball
there is definitely the sprite ball in the collection so I must ve set something up wrong.
do I need to set any of the parameters for this purpose?
-
you should probably check that the name is exactly the same (check for empty spaces, invisible characters or something like that). You can also print out all the sprites in the collection - mysprites.spriteDefinitions contains the list of them, and make sure the names match.
-
no the spelling seems fine. and mysprites.spriteDefinitions.Length returns 0
the sprite collection has 2 sprites in it. what can i change so this will work? it must be one of the options of the sprite collection
-
I can't figure it out, no matter how I set up this collection I drag it into the variable, and call for mysprites "ball" and it just doesn't exist in the collection. it always returns an empty but existing sprite collection. what could I be doing wrong?
-
Hi,
I wrote this little test script (although it's in c#), which works on one of my sprite collections.
using UnityEngine;
using System.Collections;
public class SpriteLoad : MonoBehaviour {
public tk2dSpriteCollectionData mySpriteData;
void Start ()
{
int spriteID = mySpriteData.GetSpriteIdByName("eye");
GameObject go = new GameObject();
tk2dSprite.AddComponent(go, mySpriteData, spriteID);
}
}
So if you drag your sprite collection data object to mySpriteData and change the GetSpriteIdByName to "ball", does that work for you?
Also, are you using platform collections?
-
I used your code as it is and i get this error.
IndexOutOfRangeException: Array index is out of range.
the problem is not with the script but with setting up the collection so it can be accessed by the script.
no I am not using platform sprites, what does it mean?
-
Can you create a sample UNity project with this and then email it to support at unikronsoftware.com please. This should definitely work, so theres something we're clearly missing here.
-
oh I figured it out. I needed to drag the object inside the sprite collection folder on the script! what I did was add a new component spritecollectiondata to the collection and then drag the collection onto the script and that was wrong.
oh well thanks for the help.
-
Glad you've figured it out.