How to store tile data for placing tiles

I’m making a game similar to the game Terraria and ran into an issue on how to store the data of tile types. Each tile has an integer called tileType and this dictates all the other attributes of the tile like hardness, texture, etc. The tile itself is not a gameobject but instead a class in an array called Tiles located in each chunk (the chunks are the gameobjects). My issue is storing all the data of the tile types and have resorted to doing a switch statement for every possible tile type but this feels extremely slow because I plan of making tiles destructible. Is there any way to store the tile type data in a more efficient way?

Often times when you want to model storing data in an object-oriented program, a good path to take is making a class! I’d recommend making a TileType class that has all the data about a tile type. You can reference this type from your tile object to populate various properties. If you structure things this way, you can also load all your tile types from a file. Here’s a really simple example, where the data would come from a json file. For more on how to read JSON, see this article:

Code Files

[Serializable]
public class TileType
{
    public int id;
    public string name;
    public double hardness;

    public static TileType CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<TileType>(jsonString);
    }
}

public class TileTypeLoader
{
    public IEnumerable<TileType> LoadTileTypes()
    {
        var tileTypes = new List<TileType>();
        var tileTypeStrings = File.ReadAllLines("C:/path/to/tile/type/file.json");
        foreach(var tileTypeString in tileTypeStrings)
        {
            var tileType = TileType.CreateFromJSON(tileTypeString);
            tileTypes.Add(tileType);
        }

        return tileTypes;
    }
}

A JSON File

{"id": 1, "name": "Dirt", "hardness": 1}
{"id": 2, "name": "Stone", "hardness": 2.5}
{"id": 3, "name": "Grass", "hardness": 1.5}

I created a tutorial on medium, which basically is close the answer posted here.