Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created December 27, 2012 00:36
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mstevenson/4384385 to your computer and use it in GitHub Desktop.
Save mstevenson/4384385 to your computer and use it in GitHub Desktop.
Create a quad mesh asset in Unity. Place this script in the Editor folder.
using System;
using UnityEngine;
using UnityEditor;
public class CreateQuadMesh : Editor {
[MenuItem("Assets/Create/Quad Mesh", false, 10000)]
public static void Create ()
{
Mesh mesh = BuildQuad (1, 1);
string name = "Quad Mesh";
mesh.name = name;
AssetDatabase.CreateAsset (mesh, String.Format ("Assets/{0}.asset", name));
EditorUtility.FocusProjectWindow ();
Selection.activeObject = mesh;
}
private static Mesh BuildQuad (float width, float height)
{
Mesh mesh = new Mesh ();
// Setup vertices
Vector3[] newVertices = new Vector3[4];
float halfHeight = height * 0.5f;
float halfWidth = width * 0.5f;
newVertices [0] = new Vector3 (-halfWidth, -halfHeight, 0);
newVertices [1] = new Vector3 (-halfWidth, halfHeight, 0);
newVertices [2] = new Vector3 (halfWidth, -halfHeight, 0);
newVertices [3] = new Vector3 (halfWidth, halfHeight, 0);
// Setup UVs
Vector2[] newUVs = new Vector2[newVertices.Length];
newUVs [0] = new Vector2 (0, 0);
newUVs [1] = new Vector2 (0, 1);
newUVs [2] = new Vector2 (1, 0);
newUVs [3] = new Vector2 (1, 1);
// Setup triangles
int[] newTriangles = new int[] { 0, 1, 2, 3, 2, 1 };
// Setup normals
Vector3[] newNormals = new Vector3[newVertices.Length];
for (int i = 0; i < newNormals.Length; i++) {
newNormals [i] = Vector3.forward;
}
// Create quad
mesh.vertices = newVertices;
mesh.uv = newUVs;
mesh.triangles = newTriangles;
mesh.normals = newNormals;
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment