2D Toolkit Forum

2D Toolkit => Support => Topic started by: K0mix on September 08, 2013, 03:22:55 pm

Title: set sprite
Post 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
Code: [Select]
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?
Title: Re: set sprite
Post by: unikronsoftware on September 08, 2013, 03:33:36 pm
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
Title: Re: set sprite
Post by: K0mix on September 08, 2013, 05:40:24 pm
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?
Title: Re: set sprite
Post by: unikronsoftware on September 08, 2013, 06:08:13 pm
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.
Title: Re: set sprite
Post by: K0mix on September 08, 2013, 06:49:42 pm
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
Title: Re: set sprite
Post by: K0mix on September 12, 2013, 08:31:08 am
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?
Title: Re: set sprite
Post by: gary-unikronsoftware on September 12, 2013, 10:17:06 am
Hi,

I wrote this little test script (although it's in c#), which works on one of my sprite collections. 

Code: [Select]
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?
Title: Re: set sprite
Post by: K0mix on September 12, 2013, 04:35:53 pm
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?
Title: Re: set sprite
Post by: unikronsoftware on September 12, 2013, 06:14:32 pm
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.
Title: Re: set sprite
Post by: K0mix on September 12, 2013, 08:16:37 pm
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.
Title: Re: set sprite
Post by: unikronsoftware on September 12, 2013, 11:39:42 pm
Glad you've figured it out.