- Home /
Answer by Tomer-Barkan · Apr 28, 2015 at 04:53 AM
Of course it's possible.
There may be several ways to do it. If your game is a 2D game, you can either have one sprite for the entire grid, or several small sprites, one for each square, depending on whether or not your grid is constant in size.
If it's a 3D game, you can create a mesh shaped like the grid, and have a tiled texture on it. Again, this is if the grid is fixed side, otherwise you can create many small meshes. You can use sprites in a 3D game too.
Last option that comes to mind, and the preferred one IMO, is using Unity's new Canvas component, in World Space render mode. This allows to create something like a billboard inside the game world, which could work great fro your grid. Inside the canvas you can have a tiled image, and also a mask to prevent the corners from rendering. Otherwise you could use a "Grid Layout Group" and arrange single tile images inside, where the corners will have their "Image" component disabled, while the others will be enabled.
Or you could have a single mesh, scale it, and change the number of times the material repeats the texture http://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.SetTextureScale.htmlyou can create many small meshes
Although from the picture it seems OP may want a grid that isn't square... so your way is probably better
The canvas idea is brilliant! It works perfectly for my game since it brings the grid down to 1 draw call and is easily customizable.
I have tried to make a grid of squares 300 x 300 (big map in a strategy game). I have tested that 3 ways. #1 World canvas with grid layout and many small panels as children - not possible, out of limit 65k elements. #2 $$anonymous$$any small sprites - possible but needs lots of cpu and memory. #3 One big quad with tiled texture - low memory and cpu using.
Answer by BroVodo · Apr 28, 2015 at 05:42 AM
using UnityEngine;
//using System.Collections;
public class Grid : MonoBehaviour
{
[ SerializeField ] private Transform _transform;
[ SerializeField ] private Material _material;
[ SerializeField ] private Vector2 _gridSize;
[ SerializeField ] private int _rows;
[ SerializeField ] private int _columns;
void Start()
{
UpdateGrid();
}
public void UpdateGrid()
{
_transform.localScale = new Vector3( _gridSize.x, _gridSize.y, 1.0f );
_material.SetTextureScale( "_MainTex", new Vector2( _columns, _rows ) );
}
}
Thanks for this answer. I've decided that I'm going to use a canvas for this task - since I can turn on and off grid tiles at my leisure - but this seems like another great solution that might help others (and it also might be faster).
Your answer
Follow this Question
Related Questions
Best solution for Grid-based building system? 0 Answers
Keyword 'void' cannot be used in this context 1 Answer
Fog and Blur not affecting terrain and some trees! 1 Answer
Compact Wordle Graphics in Unity3D 0 Answers
Is it possible to set smoothness and metallic of a mesh per-vertex instead of the whole material? 0 Answers