Hello Guest

Author Topic: Creating tiles/sprites in script  (Read 10694 times)

Flink

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 49
    • View Profile
    • In The Pavilion
Creating tiles/sprites in script
« on: June 17, 2012, 06:44:35 am »
Hi,

I'm new to working with both Unity & 2DToolkit, but I've done the basics of 2dtoolkit and so far so good, it's really smooth and easy to work with.

So here for my question, I'm gonna do a isometric tiled game which will load the levels from another data file so I want to create the actually tiles of the level through the script which loads the level. So I've found a couple of post addressing this technique but I'm not sure I totally understand them.

So could someone give me some brief advice on how to do this?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating tiles/sprites in script
« Reply #1 on: June 17, 2012, 11:37:54 am »
If all you need is to directly create these sprites, simply keep a reference to the sprite collection data object (not the sprite collection object, as that will include the source data), and you should be able to create sprites in the scene.

You can do this by storing a prefab and creating it, or simply adding a sprite component to a gameobject like so -
var boundsData = spriteCollectionData.FirstValidDefinition.untrimmedBoundsData;
Bounds bounds = new Bounds(boundsData[0], boundsData[1]);

GameObject go = new GameObject();
go.transform.position = new Vector3(bounds.extents.x * 2 * positionX, bounds.extents.y * 2 * positionY, 0);
tk2dSprite.AddComponent(go, spriteCollectionData, spriteId);
where spriteId is the id of sprite you wish to create based on your external data.

Hope that helps. Feel free to ask if you have any further questions.

Flink

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 49
    • View Profile
    • In The Pavilion
Re: Creating tiles/sprites in script
« Reply #2 on: June 17, 2012, 12:43:34 pm »
Thanks, some quick questions thou...

How do I get hold of the "spriteCollectionData"? I'm not sure exactly how it works in Unity getting hold of stuff, or is it depending on which object the script is added onto? Now it's added to the camera since it should run in the beginning of the scene.

Then I can't find the "AddComponent" function, and I have 1.7 + patch 2, is it in the new beta one?
And when I use AddComponent should I use it as I have in the code below?

Code: [Select]
using UnityEngine;
using System.Collections;

public class StartScript : MonoBehaviour {

tk2dSprite sprite;

// Use this for initialization
void Start () {

var spriteCollectionData = GetComponent<tk2dSpriteCollectionData>();
var boundsData = spriteCollectionData.FirstValidDefinition.untrimmedBoundsData;
Bounds bounds = new Bounds(boundsData[0], boundsData[1]);

GameObject go = new GameObject();
//go.transform.position = new Vector3(bounds.extents.x*2*
sprite.AddComponent(go, spriteCollectionData, "lamp");
}

// Update is called once per frame
void Update () {

}
}

Then I have one un-related question. I use a tk2dCamera but I can't really find where to set the resolution so the game preview window shows in the correct resolution, thou it looks correct in the scene window, the camera bounds is exactly covering the whole background I have. Even if I added a "2048x1536" option in the build setting and using "Standalone(2048x1536)" in the game preview window I only get a really close up view of my scene when I should be able to see the whole background which have the size 2048x1536.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating tiles/sprites in script
« Reply #4 on: June 17, 2012, 02:14:59 pm »
To grab a hold of SpriteCollectionData, make a
public tk2dSpriteCollectionData data; and in the inspector drag a link to the object in here. That is probably the easiest and tidiest way to do it in Unity.

AddComponent is in the beta, sorry should've mentioned.

Unfortunately there is a bug in the scene view preview window and bounding box in Unity, where when you use a custom matrix like the tk2dCamera does, it displays incorrectly. I have reported this but not heard back from the Unity guys. For now, the only way to see the correct output is to look in the game window.

Say you'd like to set the resolution to 1024x768, set it in both the build settings AND teh resolution override checkbox in the tk2dCamera. That will make the output display what will be seen in a 1024x768 display, regardless of the resolution.

Flink

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 49
    • View Profile
    • In The Pavilion
Re: Creating tiles/sprites in script
« Reply #5 on: June 17, 2012, 02:56:54 pm »
Thanks, the camera works now, too bad the unity guys haven't answered you.

So now I think I have everything done correct, thou I can't see any sprite on the screen. The script should run if I put it on the camera right?

This is how the script looks now:

Code: [Select]
using UnityEngine;
using System.Collections;

public class StartScript : MonoBehaviour {

public tk2dSpriteCollectionData data;

// Use this for initialization
void Start () {

var boundsData = data.FirstValidDefinition.untrimmedBoundsData;
Bounds bounds = new Bounds(boundsData[0], boundsData[1]);

GameObject go = new GameObject();
go.transform.position = new Vector3(bounds.extents.x*2*1024,bounds.extents.y*2*768, -1);
tk2dSprite.AddComponent(go, data, data.GetSpriteIdByName("lamp"));
}

// Update is called once per frame
void Update () {

}
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating tiles/sprites in script
« Reply #6 on: June 17, 2012, 04:19:29 pm »
This is likely a bug which has been fixed for the next release. Can you try patching this in to see if it fixes it for you?

http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,369.msg1601.html#msg1601

Anyway, this will be fixed for the next beta - I hope to release that very soon now (and the final shortly after).

Flink

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 49
    • View Profile
    • In The Pavilion
Re: Creating tiles/sprites in script
« Reply #7 on: June 17, 2012, 11:16:05 pm »
Thanks, works great!

I have one other unrelated question for you. I was thinking that every level will be it's own scene, so is it proper to have a SpriteCollection for every scene/level? Specially since we will have allot of unique graphics for every level I think the SpriteCollection would get pretty crowded. Or maybe it doesn't matter?

Also can one add collision boxes and callbacks(?) for colliding with them through the script?

Thanks again.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating tiles/sprites in script
« Reply #8 on: June 17, 2012, 11:34:34 pm »
That is a good idea if each level will have unique graphics. Obviously the usual/logical optimizations apply, like sharing sprite collection for sprites shared between levels if there are enough of them.

Yes, you can add colliders around sprites, and then use OnCollisionEnter together with triggers to detect collisions.

Flink

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 49
    • View Profile
    • In The Pavilion
Re: Creating tiles/sprites in script
« Reply #9 on: June 26, 2012, 09:04:02 am »
Hi again,

So I was also wondering if I can create a tk2dAnimatedSprite from the script? I have a tk2dSpriteAnimation set up with the correct animation. Thou I couldn't find any similar function to tk2dSprites AddComponent function in the tk2dAnimatedSprite class, is there any way to do this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating tiles/sprites in script
« Reply #10 on: June 26, 2012, 07:33:01 pm »