- Home /
Paint Blocks on a 3D Grid
Hi, I am fairly new to the whole Unity scene, so I have a question about what I am trying to do. Currently, I am trying to make an editor where you can select blocks from a menu on the side of the screen and place/paint them in a 3D grid. I already have the textures for my blocks made, and I'm just not really sure where to go from here. I have done some research already and none of what I found is exactly what I am looking for. If anyone can offer help that would be much appreciated. Thanks!
A very broad question. It would be better to split it into smaller parts for each issue involved.
First of all, you need some kind of class that stores the data of each position in the 3d grid. Let's call this class "Cell".
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Cell {
public int type = 0;
}
In this cast, it only had one variable which stores the type as an integer ID. For example, empty would be 0, dirt would be 1, and so on.
Then you need to a way to store and access each cell data. I find it easiest to do it via a Dictionary that takes three integers which are the coordinates of the cell as the key and it returns the cell data as the value. So, since a normal Dictionary doesn't take more than one key, we need a class that holds these three xyz values, like a Vector3 but with simple integers.
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public struct Coordinate3 : IEquatable<Coordinate3> {
public int x;
public int y;
public int z;
public Coordinate3() {
x = 0;
y = 0;
z = 0;
}
public Coordinate3(int a, int b, int c) {
x = a;
y = b;
z = c;
}
public Coordinate3(Vector3 v3) {
x = $$anonymous$$athf.RoundToInt(v3.x);
y = $$anonymous$$athf.RoundToInt(v3.y);
z = $$anonymous$$athf.RoundToInt(v3.z);
}
public bool Equals(Coordinate3 other) {
if (this.x == other.x && this.y == other.y && this.z == other.z) {
return true;
}
else {
return false;
}
}
public override int GetHashCode() {
return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
}
}
Now the dictionary itself:
Dictionary<Coordinate3,Cell> cells = new Dictionary<Coordinate3,Cell>();
Coordinate3 coords = new Coordinate3(3,1,7);
Null Reference Error-proof example of accessing a stored cell:
Cell cell = null;
if(cells.TryGetValue(coords,out cell) == true) {
cell.type = 1;
}
Now comes the final part: actually getting a coordinate in the grid. I usually have a large invisible plane with a collider at the current editing level which encompasses the whole grid area. I cast a ray from the current cursor position forward from the camera and the hit.point (if any), converted into a Coordinate3, is the coordinate the cursor is pointing at. The conversion math depends on how large each grid space is.
This would be the "behind the scenes" stuff. To actually see any changes in the game world, you need to build mesh chunks based on the cell data in the grid. there are dozens of examples for $$anonymous$$inecraft-like games made in Unity on the web, including the massive After playing $$anonymous$$ecraft... thread on the Uniy forums.