VVK93
1
Hello everyone.
I am new Unity developer and i want to create a simple 2D game. Unfortunately, i have a one small issue
I want to create a grid which consists of NxM cells. Each Cell has a 4 walls (right, left, down, up) as a boolean variable, and then i will call draw method which should draw only walls with true value. Which class i should use for this task and which methods or properties? GameObject or Sprite? i have no idea…
I know that it is a very simple question for experienced developers, but i can’t find a good solution and i really need help.
Here is my incomplete code.
public class CellSc : MonoBehaviour {
public bool UpWall;
public bool DownWall;
public bool LeftWall;
public bool RightWall;
public Vector2 Position;
public Texture2D texture;
private void Draw()
{
//....
}
}
Unity is not meant to “draw” stuff like in; for example, Actionscript.
In 2D you can have different versions of a texture with the walls drawn on it and show whatever texture is right based on the booleans status ?
Alternatively you can have 4 children in your sprite game object that would be the walls and toggle them on the screen based on the booleans.
I’ve been using Unity primarily for a 2D game since before the 2D sprite graphics were officially introduced and have been drawing everything through the OnGUI function using GUI.Box or GUI.DrawTexture.
First I would make floats for the cell size and wall thickness:
float cellSize = 100;
float wallThickness = 10;
Then create a Rect for the cell itself:
Rect cellRect = new Rect (position.X, position.Y, cellSize, cellSize);
Then make 4 narrow Rects for each of the walls whose dimensions are dependent on the cell Rect’s:
Rect leftRect = new Rect (cellRect.x, cellRect.y, wallThickness, cellSize);
Rect rightRect = new Rect (cellRect.x+cellSize-wallThickness, cellRect.y, wallThickness, cellSize);
Rect topRect = new Rect (cellRect.x, cellRect.y, cellSize, wallThicknes);
Rect bottomRect = new Rect (cellRect.x, cellRect.y+cellSize-wallThickness, wallThickness);
Now that you have all those Rects ready, you can use the bools to decide whether or not to draw some texture in them.
if (upWall) {GUI.DrawTexture(topRect, wallTexture);}
.
.
.