- Home /
Question by
SkandYxyz · Jan 29, 2016 at 05:04 PM ·
meshresourcesquadprimitives
Load default quad mesh from resources
Hi!
Is it possible to load the primitive quad mesh from resources? Something like this:
// Load default quad mesh
Mesh _quadMesh = Resources.Load("Quad",typeof(Mesh)) as Mesh;
Thank you in advance!
best, Andre
Comment
Best Answer
Answer by SkandYxyz · Jan 29, 2016 at 05:41 PM
It seems that the shortest way to do this is to write a script that generates a quad from scratch. Here is a script that I found:
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;
}
}