- Home /
how can I create a quad from script?
How can I create a quad from script with a specific texture attached on it as it will mainly used for rendering 2D stuffs with orthographic camera instead of using GUITexture which is really expensive if there are loads of GUITexture objects on iPhone..
Answer by Dreamora · Oct 07, 2010 at 02:37 AM
You would do so through the Mesh class
yes.. it can be done through mesh class although it's quite complex lolz.. It's better if we can use the other 3d $$anonymous$$odelling software to draw quads and using texture atlas...
It's not complex. But, creating a simple 'quad' model in a modeling program and just using that would be a viable option as well.
Answer by luci1B · Jun 03, 2014 at 08:23 AM
You can do it that way :
var go = GameObject.CreatePrimitive(PrimitiveType.Quad);
go.renderer.material.mainTexture = texture;
Answer by Tokars · May 15, 2020 at 08:51 AM
Today I asked the same question. Here how create a custom quad:
public class CustomQuad : MonoBehaviour
{
private Mesh _mesh;
private float width = 2f;
private float height = 2f;
void Start()
{
var mf = GetComponent<MeshFilter>();
_mesh = new Mesh();
mf.mesh = _mesh;
var vertices = new Vector3[4];
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(width, 0, 0);
vertices[2] = new Vector3(0, height, 0);
vertices[3] = new Vector3(width, height, 0);
_mesh.vertices = vertices;
var tri = new int[6];
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
_mesh.triangles = tri;
var normals = new Vector3[4];
normals[0] = -Vector3.forward;
normals[1] = -Vector3.forward;
normals[2] = -Vector3.forward;
normals[3] = -Vector3.forward;
_mesh.normals = normals;
var uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
_mesh.uv = uv;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireMesh(_mesh, Vector3.zero);
}
}