Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TileData question

Discussion in '2D' started by JupiterOP, Oct 10, 2019.

  1. JupiterOP

    JupiterOP

    Joined:
    Mar 5, 2019
    Posts:
    6
    I am creating a custom tile that implements TileBase but I have an enum we'll call:
    enum Type { center, right, left, isolated }
    I want each tile to store which type it is because I want to write logic in my custom tile that dictates the sprite based on that type.

    I know under the intended circumstances tile logic uses neighboring tiles to then decide which sprite to use, but in this case I want to place tiles through a script that defines the type, then I want the tile class to define in GetTileData which sprite to use based on its assigned type.

    Now I know I could use separate assets for each type but they are all thematically the same and I would like to avoid having multiple asset files for 1 tile set.

    Is it possible to store info in each tile and not have it affect all tiles? Can I modify TileData or make my own and use it somehow?
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well, I would imagine you have your tiles tagged so you could gather them into an array on Start() by using FindObjectsWithTag()

    Code (CSharp):
    1. private GameObject[] centerTileArray;
    2. private GameObject[] rightTileArray;
    3. private GameObject[] leftTileArray;
    4. private GameObject isoTileArray;
    5.  
    6. private void Start()
    7. {
    8. centerTileArray = GameObject.FindObjectsWithTag("center");
    9. // do this for each array
    10.  
    11. foreach(GameObject tile in centerTileArray)
    12. {
    13. // Here you can set information into each tile
    14. // Best results would be to create a method to determine
    15. // coordinates of tile so you can set specific-individual
    16. // logic
    17. }
    18. }
    Now if you want, you can [SerializeField] on your arrays and should be able to insert your tiles into it manually (maybe... never tried it). Then since you know exactly which element the tile is inside the array you can easily access it through code.

    I realize you're trying to use enum to locate your tiles and insert parameters. I'm offering a different approach to that.
     
  3. JupiterOP

    JupiterOP

    Joined:
    Mar 5, 2019
    Posts:
    6
    I like your idea but in this case I'm not using GameObject tiles that I can add tags to I'm using unity's Tilemap system and the TileBase class we are supposed to inherit is not unique in each tile on the tilemap, only the TileData is unique on each tile. Meaning TileBase derivatives can only store data that affects every tile of it's type. TileData in this case is not something I can edit or overload (I don't think), and is a file that is automatically used by Unity's tilemap system and holds the individual and differing data of each tile. If I could make unity use my own form of TileData, that is more what I am asking if I can do. Sorry if I make little sense its hard to explain unless you've tried to create custom unity tiles.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You should absolutely be able to create tile palettes and give them each a tag. You'd only need to paint them on for your specific needs. However, if this doesn't work for you, you can try this, scriptable tiles.

    https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles-Example.html