If I want to recreate what I have made in Unity Editor using tilemap, my current guess is :
Scan through the tilemap and store tile information.
Recreate the tilemap on runtime by using the stored tile information.
All seems good and well, but my actual question is what would be the best method of actually storing the reference to the tile used?
Easiest method is to just use name for the tile asset, but surely there must be better way? Are there any kinds of GUID for tile asset that we can use to store the reference? InstanceID is not good… how about localID? would that work as well? Any experience, or idea on how to store reference to the assets such as tile?
The asset name stable; you control any breaking changes and could provide a conversion step in your deserialization process if you rename the asset. To my knowledge, Unity does not expose a GUID or unique reload-stable int. If you wanted such an int, you could add it as a field. Perhaps it could just hash its name when first-created with a hashing method you own (because GetHashCode in an implementation detail and can’t be relied upon for stability). But the drawback to that is its kind of unnecessary given that the string is right there. An additional point in favor of just using the string is it is readable, so debugging your serialized output may be easier.
What would make the asset name not work is if you create multiple instances of the same-named asset. If that’s the case, then you need some alternative. You could do what formal serializers (JSON.NET) do. They (iirc: rough approximation) add all references they encounter to a dictionary<object, int> where the int is a unique ID. Whatever is thrown in, the first time it is added it receives an this ID which is stable for save/load but will be assigned afresh every serialization. Then it’s serialized in place.So if the same tile is referenced in two places, the first place defines the ID it’ll use later in the document and is where other instance fields are serialized and the second place will simply be the $ref = integer. This means that the deserialization process must proceed front-to-back so that the reverse dictionary<int, object> can be constructed to deserialize the $ref = integer after the object has been recreated.
make an array of Tile[ ], insert all the tiles you have in that array.
when serializing use tilemap.gettile and loop through your array until you have a match, if you count how many loops it took to reach it you will have the index of the array it is on which you can serialize as an int.
so you would serialize 2 things: vector3Int + int
vector3int - position of the tile
int - index of the tile on your tilearray
when deserializing you would get the reference to the tile by going to your tilearray[index]
Interesting, but could you explain a bit about how your serialization method is based on? The git site doesn’t really have a lot of explanation / docs about it.