- Home /
The question is answered, right answer was accepted
How to draw a rectangle using scripts
So basicly, i want to draw a rectangle with lines. I have 2 sets of objects being spawned, and i want a line around them, but much rather a big rectangle around the area they can spawn in.
Right now i have this:
void DrawLine(Vector3 start, Vector3 end, float duration = 0.2f)
{
GameObject myLine = new GameObject();
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.SetWidth(0.1f, 0.1f);
lr.SetPosition(0, start);
lr.SetPosition(1, end);
}
Which works ok, but for 1 rectangle you already need 4 lines of code, and you cant edit it that easliy, so my question is is there any way to do this but then in a rectangle?
Answer by Zodiarc · Oct 07, 2016 at 11:48 AM
Well it worked, i only don'r really get what it does as in, the mesh is a cube, but it sill spawns a triangle :S, Trying to figure it out, but if you could explain it would be awesome :3
(So mostly, how would you for example spawn 2 on different locations, since when you spawn them in you don't give a location.
How do you change the mesh, size etc?
:S
Basically the vertices array defines the point cloud of which the mesh consists, the uv array the uv coordiantes (but I don't know how it works for more complicated meshes than a square, need to figure it out) and tris defines the indexes which build the trianglular faces of the mesh. Every set of 3 integers in that array defines a triangle.
float scale = 1.0f
Vector3[] vertices = new Vector3[]
{
new Vector3(-scale / 2, 0, scale / 2),
new Vector3(scale / 2, 0, scale / 2),
new Vector3(-scale / 2, 0, -scale / 2),
new Vector3(scale / 2, 0, -scale / 2)
};
Vector2[] uv = new Vector2[]
{
new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0),new Vector2(0, 0)
};
int[] tris = new int[]
{
2,1,0,1,3
};
Debug.Log(this.tile$$anonymous$$esh);
this.tile$$anonymous$$esh.vertices = vertices;
this.tile$$anonymous$$esh.uv = uv;
this.tile$$anonymous$$esh.triangles = tris;
this.GetComponent<$$anonymous$$eshFilter>().mesh = this.tile$$anonymous$$esh;
this.GetComponent<$$anonymous$$eshRenderer>().material = this.material;
Should build a square. By changing the scale variable you can change the size. Also you need to take care of the normals. If some part isn't displayed just change the integers in the tris array around. There's also another way to recalculate normals but I don't know how it works.
Throws out the errors: Assets/LineController.cs(31,42): error CS1526: A new expression requires () or [] after type
And
Assets/LineController.cs(31,52): error CS1525: Unexpected symbol )', expecting
,', or `}'
Basicly the Vector3 part, its in C# (Previous part worked tough)
That makes 3 triangles and it's upside down (Sorry, never worked with tris before, also googling it XD)
Weird. It should create 2. Can you post a screenshot?
The upside down problem can be solved by inverting the tris array to
0,1,3,0,1,2
If this doesn't work in a few hours I'll be able to look it up because I have this up and running in my project.
Forgot I have it on my gitlab:
public class Adaptive$$anonymous$$eshTile : AbstractTileController
{
public $$anonymous$$aterial material;
private $$anonymous$$esh tile$$anonymous$$esh;
// Use this for initialization
public void Start()
{
this.tile$$anonymous$$esh = new $$anonymous$$esh();
Vector3[] vertices = new Vector3[]
{
new Vector3(-scale / 2, 0, -scale / 2),
new Vector3(scale / 2, 0, - scale / 2),
new Vector3( -scale / 2, 0, scale / 2),
new Vector3( scale / 2, 0, scale / 2)
};
Vector2[] uv = new Vector2[]
{
new Vector2(0 ,0), new Vector2(0, 1), new Vector2(1, 0), new Vector2(1, 1)
};
int[] tris = new int[]
{
2,1,0,1,2,3
};
this.tile$$anonymous$$esh.vertices = vertices;
this.tile$$anonymous$$esh.uv = uv;
this.tile$$anonymous$$esh.triangles = tris;
this.GetComponent<$$anonymous$$eshFilter>().mesh = this.tile$$anonymous$$esh;
this.GetComponent<$$anonymous$$eshRenderer>().material = this.material;
}
}
Solved it by turning the GameObject 180 degrees, not perfect but ah well. Thanks!