Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to get the collider type from a TileBase? [SOLVED]

Discussion in 'Scripting' started by Epic-Username, Aug 12, 2020.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Hi Everyone,

    I'm wanting to get the Collider Type (None, Sprite, Grid) for a specific TileBase which i got via a tilemap
    Although looking at the docs for TileBase there doesn't appear to be any variable which contain info on the collider type the only thing that sounds like it would be useful is .GetTileData but looking at the docs it appears that this doesn't contain any info on the collider either, I've tried researching for the solution but have found nothing in regards to this.

    It would be greatly appreciated if anyone could let me know how i can get the collider type of a specific TileBase tile as this is a problem i have been stuck with for a while now.

    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    I think it's in a component on the GameObject, I believe this one:

    https://docs.unity3d.com/Manual/class-TilemapCollider2D.html

    Caveat: I've not used that, and a TilemapCollider2D is a Component (i.e., can only live on a GameObject) whereas a TileBase is actually a ScriptableObject. Maybe the TileBase gives the shape info to the collider? Now I'm just guessing because I don't have a handy setup to mess with it just now.
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    So basically i'm wanting to check the collider type of an individual tile to check if its solid or not (None or Sprite/Grid) i just checked TilemapCollider2D and i don't think it has a way to check the type of individual tiles on the tilemap correct me if i'm wrong.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    I'm sorry, this has gone beyond my knowledge of tilemaps... I know what they are (been using them since 1983 on the Vic20) but I have not used the Unity implementation of them.

    I do know that normally when you check "type of collider," it's either the C# type, or else you use a PhysicMaterial to change and detect the qualities.

    If you want to embed more info in a traditional Unity collider you make a script and co-attach it and put whatever you want in that. I do that to make a floor deadly for instance.
     
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Ok so just to elaborate on what im wanting to achieve:
    I want to get a boolean array of all tiles on the tilemap for if they're solid or not None being False and Grid/Sprite being True.
    What i have currently tried is to get the tilemap and loop through all tiles:
    Code (CSharp):
    1.  for (int x = 0; x < tilemapBounds.size.x; x++)
    2.         {
    3.             for (int y = 0; y < tilemapBounds.size.y; y++)
    4.             {
    5.                 TileBase tile = allTiles[x + y * tilemapBounds.size.x];
    6.  
    7.                 if (tile != null)
    8.                 {
    9.                     //get tile collider type
    10.                 }
    11.             }
    12.         }
    Although i have found no solution for getting the collider type from an individual tile. Hopefully this should explain exactly what i'm trying to accomplish.

    Thanks.
     
  6. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    sorry to spam my own thread but i have found a struct which contains what i'm looking for which might be helpful for anyone wanting to help find the solution which is TileData:
    Code (CSharp):
    1. TileData td;
    2. UnityEngine.Tilemaps.Tile.ColliderType ct = td.colliderType;
    The problem is though i have no idea how to get this TileData struct, at first glance it looks as if GetTileData would accomplish this but further looking into it GetTileData appears to be a override method you use for implementing custom tiles and not used to get the TileData by looking at the docs.
    So i guess my question changes to: how can i get TileData from a TileBase?
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Good news, i think i found the solution:
    Code (CSharp):
    1. BoundsInt tilemapBounds = tilemap.cellBounds;
    2. for (int x = 0; x < tilemapBounds.size.x; x++)
    3.         {
    4.             for (int y = 0; y < tilemapBounds.size.y; y++)
    5.             {
    6.                 UnityEngine.Tilemaps.Tile t = tilemap.GetTile<UnityEngine.Tilemaps.Tile>(new Vector3Int(x, y, 0));
    7.                 if (t != null)
    8.                 {
    9.                     print(t.name + ", " + t.colliderType);
    10.                 }
    11.             }
    12.         }
    its not TileBase which contains the ColliderType but instead Tile i simply had to get that instead and just got the colliderType variable out of that.
    Thanks for those who responded and helped out.
     
    soriyamasoutarou likes this.