Generating Tiles at runtime

I cant seem to find this information anywhere, is it possible to create tiles from sprites at runtime?

I was looking for something like Tile.Create(Sprite)

I cant use the editor to create the tiles because Im creating the sprites themselves at runtime aswell with texture2d and sprite.create

Hi @brigas

If you are using Unity Tilemap system, look into Tilemap.SetTile:

It will allow you to change/add tiles runtime.

There are several examples floating around if you just google.

From my reading of the object hierarchy, a Tile inherits from TileBase, and TileBase inherits from ScriptableObject.

Therefore you should be able to use ScriptableObject.CreateInstance<>() to make what you want.

In fact, I just verified this works: I created a new Tile and stuck it in my map. And then if you actually want to write it to disk (at editor time obviously), there’s tutorials for how to create the asset, write it, etc.

And here’s my codelet and scene and stuff doing it, attached.

5138036–508487–TilemapStuff.unitypackage (36 KB)

Hello, thank you for the reply, I am already using set.tile, however set tile is _ set.tile(vector3,tile), what I need is the second parameter. I want to create the tile used for the second parameter of set tile

1 Like

Thanks! I will try this, I dont need to save the asset, I just need to create a tile and keep the reference so that can be used by set.tile (using a sprite).

Do you think that I could have for example a dummy tile in assets and then instantiate it to clone it? And then set only the new sprite?

Oh absolutely. That is a time-honored mechanism with procedural generation: use the editor for all the hard-to-set-up stuff, then inject just what changes.

Go go go!!! :slight_smile:

Works perfectly with ScriptableObject.instantiate thanks a lot! =D

1 Like

Hi I am trying the same thing, I also want to create a tile that can be used as the second argument to the setTile function at runtime. The answer with the Scriptable Objects sounds cool but I am unsure wether this can only work at editor time or if it is possible to even work in a deployed build?
Thanks in advance!

That’s an excellent question but we still prefer that you do not necro-post to old 2019 threads and instead start your own thread.

The answer is that ScriptableObjects are an actual asset that CAN be included in all target builds. It follows the same rules as every other asset and must be referenced either in an included scene or in some other thing within an included scene.

See the docs for more.