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

1 Answer

1

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;
	}
	
	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[4];
		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[4];
		newNormals[0] = Vector3.forward;
		newNormals[1] = Vector3.forward;
		newNormals[2] = Vector3.forward;
		newNormals[3] = Vector3.forward;

		// Create quad
		mesh.vertices = newVertices;
		mesh.uv = newUVs;
		mesh.triangles = newTriangles;
		mesh.normals = newNormals;

		return mesh;
	}
}

Yes, in most cases this probably makes more sense. It also allows you to modify it the way you need it. Though if you really want to get the hold of one of the built-in meshes, it would be possible to use Gameobject.CreatePrimitive. You get an actual gameobject in the scene and you could either use that object directly, or if you really just need the Mesh, grab the mesh from the MeshFilter and destroy the GameObject again.

–

It's even simpler than that. You can just use Resources.GetBuiltinResource<Mesh>("Quad.fbx").

–