How should I store and load structures for 2d tile based game?

by FICHEKK   Last Updated August 10, 2018 09:13 AM

I am working on structure generation for my 2d tile based game. Structures can be anything from houses, castles, dungeons, mazes etc.

Structures are pre-defined, meaning I defined the width/height of the structure and wrote every single tile that should be placed for each coordinate.

Structure "template":

public class Structure 
{
    private int width;
    private int height;

    private int[,] element;

    public Structure(int _w, int _h, int[,] _e)
    {
        width = _w;
        height = _h;
        element = _e;
    }
}

Creating a new structure:

// 0 = air

// 1 = wall

// 2 = door

Structure house = new Structure(4, 4, 
{

{1, 1, 1, 1},

{1, 0, 0, 1},

{1, 0, 0, 1},

{1, 2, 1, 1}

});

This works fine for small amounts of structures. However, having tens/hundreds of structures in code is not only mess, but taking memory unnecessarily since structure generation gets called rarily. So how should I store that data?

1) Should I make a file for each structure and load from it?

2) Should I dump all structures in 1 file?

3) What file format should I use for this?

4) Does Unity provide some data containers that can be easily accessed and read from?

5) How is this usually done?

Tags : unity c#


Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM