• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Starleg2 · May 22, 2018 at 12:46 AM · 2d gametilemaptiletilesterrain generation

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alanisaac · May 22, 2018 at 01:29 AM

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}


Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Starleg2 · May 22, 2018 at 02:27 AM 0
Share

Thanks for the support, I do use C# so if you have a C# example that would be nice but I understand the concept. Thanks again for the answer that fits perfectly

avatar image Captain_Pineapple Starleg2 · May 22, 2018 at 07:29 AM 0
Share

The given code is in C#. JSON is only the name of this specific way to format data but has nothing to do with the programming language.

avatar image Starleg2 Captain_Pineapple · May 22, 2018 at 03:21 PM 0
Share

Oops, my mistake. Thanks again

Show more comments
avatar image Starleg2 · May 22, 2018 at 06:23 PM 0
Share

I have worked with the script and have found it very useful but I still don't understand how to get the TileType List out of the IEnumerable. On line 26 where does it return the list to? @Alanisaac

avatar image Alanisaac Starleg2 · May 22, 2018 at 07:12 PM 0
Share

It depends on how your code is structured. But let's say you have some sort of Tile$$anonymous$$anager class that controls the creation of tiles. In that class, you could load the tile types in the Awake call like so:

 public class Tile$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
     private List<TileType> tileTypes;
 
     void Awake() 
     {
         var tileLoader = new TileLoader();
         var tiles = tileLoader.LoadTiles();
         tileTypes = tiles.ToList();
     }
 }
avatar image Starleg2 Alanisaac · May 23, 2018 at 01:43 PM 0
Share

Thanks you, that fixed it

avatar image
0

Answer by allencoded · Jun 03, 2018 at 06:01 PM

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

https://medium.com/@allencoded/unity-tilemaps-and-storing-individual-tile-data-8b95d87e9f32

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Starleg2 · Jun 04, 2018 at 02:15 AM 0
Share

Thanks for the feedback, this helps as well!

avatar image varikor · Mar 31, 2019 at 07:20 PM 0
Share

Thanks so much! I am just wondering how to access the bool IsExplored in your script?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2D TileMap Rule Tile Carpet 1 Answer

TileMap won't show up in 2d object in heirarchy. 0 Answers

How do I read from TilePalette? 0 Answers

Falling Tiles 1 Answer

Streaming in 2D tiled maps for seamless world, does this conflict with Unity's editor? Do I need to use an external editor? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges