Cammin / LDtkToUnity

Unity importer for the LDtk level editor
https://cammin.github.io/LDtkToUnity
MIT License
388 stars 39 forks source link

Help getting access to custom tile data #39

Closed ninjatronic closed 1 year ago

ninjatronic commented 2 years ago

Hi. Apologies for the noob question but I'm coming up against a brick wall here.

I have a tileset "Ground" which has custom data for each tile using the following schema...

{
  "movementSpeed": 0.8
}

I've imported the world into unity and put the level in the scene. In code I can get the tile under the player's current position...

public class PlayerController : MonoBehaviour
{

    Rigidbody2D body;
    Tilemap ground;
    private float horizontal, vertical;
    private readonly float runSpeed = 1.0f, moveLimiter = 0.7f;

    void Start()
    {
        body = GetComponent<Rigidbody2D>();
        ground = GameObject.Find("Ground").GetComponentInChildren<Tilemap>();
    }

    private void FixedUpdate()
    {
        Vector3Int position = Vector3Int.RoundToInt(body.position);
        TileBase tile = ground.GetTile(position);
    }
}

But from here I am drawing a blank on how to access the custom data associated with this tile. Any help is much appreciated. Thanks for the excellent project so far it has been very useful.

Cammin commented 2 years ago

Hi, there is indeed a way to get the custom data defined from the importer. I have plans to make this experience easier, but for now, you can access the tile data by deserializing the json data, then traversing through it until you can access the CustomData from the TilesetDefinition:
image

You can access and deserialize the data from this scriptableobject asset from the imported project with the function FromJson(): image

Let me know if this solution can work for you 🙂

ninjatronic commented 2 years ago

My LDTK project saves levels as separate files and as such I'm struggling to find a way to access this JSON object in code. I can see it in the assets in the project explorer but when I drop the LDTK project file into the scene in the hierarchy it does not show these children. Is it possible for you to show a simple code example as to how I would access this? Sorry for the noob question, I'm new to Unity and LDTK. Many thanks.

Cammin commented 2 years ago

This function in particular will be useful for your situation, where you can traverse the project json to access definitions and additionally levels and their used tilesets in that way.

And you can check here to understand how the LDtk postprocessing works.

Also feel free to join the Discord server where I can provide quicker answers. I didn't realize you had responded here recently after my previous post 😅

Cammin commented 2 years ago

Another significantly easier option is defining intgrid values to describe where your character should change it's speed. then you can make your character check for an IntGridTile existing at a position, and check if it matches a reference you are checking for. And voila :) Let me know if that can work out for you.

Cammin commented 1 year ago

Custom tile data is now available in the newest update! (4.0.0) You get a tileset tile from the tilemap, which will contain both the custom data and enum values. image