How do I place a Tile from a Tile Palette by script?

I’m doing a TileVania style game. I’m making the traps system and I need to place some Tiles by script as part of my traps systems. I didn’t found nothing in Docs.

i have this problem too, i want to create a palette with a range sprites, so have you find the right answer?

You can also use a “brush” to do it, that way if you have custom behavior for the brush, all rules are respected.

Example:

#if UNITY_EDITOR
using Tiles;
using UnityEngine;
using UnityEditor.Tilemaps;
using UnityEngine.Tilemaps;

/* The purpose of this script is to determine if it is possible to paint onto a tilemap
* during runtime. (it is!)
*
*/

namespace Test
{
    public class TestPaint : MonoBehaviour
    {
        public TileBase tileAsset;
        public Tilemap tilemap;

        public Grid grid;
        // Use this for initialization

        void Start () {
            AdvancedTileBrush brushInstance = ScriptableObject.CreateInstance<AdvancedTileBrush>();

            Vector4 v = new Vector4(1f, 0, 0, 0);
            Vector4 v2 = new Vector4(0, 1f, 0, 0);
            Vector4 v3 = new Vector4(0, 0, 1f, 0);
            Vector4 v4 = new Vector4(0, 0, 0, 1f);

            brushInstance.cells[0] = new GridBrush.BrushCell{tile = tileAsset, color = Color.white, matrix = new Matrix4x4(v,v2,v3,v4)};
            for (int i = 0; i < 10; i++)
            {
                brushInstance.Paint(grid, tilemap.gameObject ,new Vector3Int(i,i,0));
            }
        }
    }
}

My advice for adding tiles by script during runtime:
Create a new struct that contains a public Tile (tile) and a public String (name). Make that struct and its field serializables.

Create a new C# script deriving from ScriptableObject, with a [CreateAssetMenu] thing (unity tutorial about scriptableObjects include the way to do so), that contains a List, make it serializable.
Right clic somewhere in your asset folder and create a new "Whatever the name you gave to your scriptableObject in the createassetmenu thing in your script’.
Add the tiles you might want to use into the list of that newly created sriptableObject by first adding new items to the list in the inspector, dragging and dropping those tile assets in the corresponding place, and giving those a name.
Add a reference to that scriptableObject in the script that needs to add those tiles during runtime.
I believe you might want to save your project everytime you modify one of those scriptableObjects (not entirely sure, might need confirmation).

You should now be able to use that list, and search for tiles using their names or their order number in the list, to call SetTile(Vector2int position, Tilebase tile) on your tilemap. It should be quicker and easier than searching for the tileAsset directly in your asset folder by code to then set those during runtime, and would prevent you from having to add those tiles manually to every prefab that will require those. It would also fit well in a static class or singleton meant to hold a reference to that kind of asset.

With a bit more editor scripting, you could make the scriptableObject to automatically take and sort all the tileassets in a certain folder, name them automatically, and other things that might help a lot when you need to reproduce that kind of steps often (I guess it’s possible to grab all the tiles included in a tilepalette).

1 Like

Hey, if you are still using this forum please answer me. I am trying to do that thing, but I don’t see whats wrong. Can you check my code?

using UnityEngine;
using UnityEngine.Tilemaps;

public class ingameBuild : MonoBehaviour
{
    public Tile highlightTile;
    public Tilemap highlightMap;
    [SerializeField]
  

    // do late so that the player has a chance to move in update if necessary
    private void Update()
    {
        // get current grid location
        Vector3Int currentCell = highlightMap.WorldToCell(transform.position);    
        // if the position has changed
        if(Input.GetMouseButtonDown(0))
            highlightMap.SetTile(currentCell, highlightTile);
        }
    }

You need to use TileBase:

public TileBase highlightTile;

How to access a reference for the Tile object? I can’t figure out how to set the reference inside unity like I do with the tilemaps, since the maps are objects in the scene and the palette tiles are not.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class GridManager : MonoBehaviour
{
    // If you have more layers to handle then do a List<Tilemap>
    public Tilemap tilemap;

    // If you have a lot of tiles, think about some list, dictionary or structure
    public TileBase tileLand;
    public TileBase tileWater;

    // Sample terrain to be generated
    List<List<int>> gameWorld = new List<List<int>>
    {
        new List<int> { 0, 0, 0, 0, 0},
        new List<int> { 0, 1, 1, 1, 0},
        new List<int> { 0, 1, 1, 1, 0},
        new List<int> { 0, 1, 1, 1, 0},
        new List<int> { 0, 0, 0, 0, 0},
    };

    void Start()
    {
        for(int x = 0; x < gameWorld.Count; x++)
        {
            for(int y = 0; y < gameWorld[x].Count; y++)
            {
                tilemap.SetTile(new Vector3Int(x, y, 0), (gameWorld[x][y] == 0 ? tileWater : tileLand));
            }
        }
    }
}