Hello Guest

Author Topic: set sprite  (Read 8382 times)

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
set sprite
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: set sprite
« Reply #1 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

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: set sprite
« Reply #2 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: set sprite
« Reply #3 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.

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: set sprite
« Reply #4 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

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: set sprite
« Reply #5 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?

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: set sprite
« Reply #6 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?
« Last Edit: September 12, 2013, 10:33:59 am by gary-unikronsoftware »

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: set sprite
« Reply #7 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: set sprite
« Reply #8 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.

K0mix

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: set sprite
« Reply #9 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: set sprite
« Reply #10 on: September 12, 2013, 11:39:42 pm »
Glad you've figured it out.