Search Unity

how do I add scripts to tiles?

Discussion in '2D' started by sirjacobgoodrich, Jul 27, 2019.

  1. sirjacobgoodrich

    sirjacobgoodrich

    Joined:
    Jul 27, 2019
    Posts:
    10
    I'm starting a new project, and I have run into an issue, this is my 1st time with tiles, and I need to give tiles different properties. For instance I want to give some tiles a resource value, for instance a "rock tile" would have ore as a resource. I can only add scripts to the tile map, which would give all the tiles the same properties.
     
  2. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    You can create tiles classes deriving from the tilebase class, starting like this :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5.  
    6. [CreateAssetMenu]
    7. public class BlockTile : TileBase
    8. {
    You'll have to override some of the functions in there.
    Putting in there a reference to an enum containing the names of each resource value this type of tile holds, or a flag system if a tile can contain multiple resources at once, or anything you like actually.

    How do you "mine" a resource that is in a tile ? If you click on it or something, then you actually know how to get a particular tile in your tilemap, you could create a "map" class holding all the data that you can't acheive to store directly on tile in it, referenced with x and y coordinates, and call both the script on your tilemap that destroys the tile, and the script on the class you created to add some more logic.

    I've just started a tilebased game and am not sure yet what are the best practices when it comes to do so though
     
  3. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    154
    You should watch this talk "Creating your first Scriptable Brush and Scriptable Tile" by Jay Santos at Unite Seoul 2018