Hello,
-
I have a texture with multiple 64 x 64 tiles.
-
I have a few quads in the scene, and I would like the quads to use some of the tiles from the texture.
-
I would like to drag and drop a script on the quad and edit the public variables so I can pick the right files.
x_offset = 0
y_offset = 0
tile_size = 64
tile_sheet = 384
the material is using a 384x384 texture
this is my code but it doesn’t seem to be working. I cant figure out whats wrong.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileChooser : MonoBehaviour {
public int tile_x;
public int tile_y;
public int tile_size;
public int sheet_size;
private Renderer rend;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.material.mainTextureScale = (new Vector2(tile_size/sheet_size,tile_size/sheet_size));
rend.material.SetTextureOffset("_MainTex", new Vector2(tile_x,tile_y));
}
// Update is called once per frame
void Update () {
}
}
Hello, I have finally figured out how to set a Quads Texture to a tile from a tileset.
I’ve noticed there are 92 followers so I decided to update the answer.
-
create a material that has the tileset.
-
add a script to your project, use the code I have provded.
-
drag and drop the script on to the quad in your scene.
-
edit the public variables.
- sheet_size, is the size of your tile sheet eg: 256x256
- tile_size is the size of your tiles, eg 32x32, 64x64, 16x16, ect.
- tile_x is which tile you want, 0,1,2,3,4, starting from left
- tile_y is the tile you want STARTING FROM THE BOTTOM OF THE SHEET, eg 0,1,2,3,4
- it may help to change the IMPORT SETTINGS on your TEXTURE. set “WRAP MODE” to CLAMP.
-
enjoy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileChooser : MonoBehaviour {
public float tile_x;
public float tile_y;
public float tile_size;
public float sheet_size;
private Renderer rend;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.material.mainTextureScale = (new Vector2(tile_size/sheet_size,tile_size/sheet_size));
rend.material.SetTextureOffset("_MainTex", new Vector2(tile_x*(tile_size/sheet_size),tile_y*(tile_size/sheet_size)));
}
// Update is called once per frame
void Update () {
}
}