MakeQuad

From Unify Community Wiki
Jump to: navigation, search

Contents

Description

This is a simple function that creates a quad mesh for you (with two triangles), and sets up the vertices, UVs and triangles appropriately. It returns a Mesh object.

Usage

Mesh myQuad = MeshUtils.MakeQuad( 0, 0, 10, 10 );

Code

Please feel free to do what you like with this code. The only thing I ask is that you let me know if you use it for anything interesting :)


CSharp - MakeQuad.cs

// I'd love to hear from you if you do anything cool with this or have any suggestions :)
// www.tenebrous.co.uk
 
using UnityEngine;
using System.Collections;
 
public class MeshUtils
{
    public static Mesh MakeQuad( float originX, float originZ, float sizeX, float sizeZ )
    {
        Mesh newMesh = new Mesh();
 
        Vector3[] vertices = new Vector3[]
        {
            new Vector3( originX,         0, originZ ),
            new Vector3( originX + sizeX, 0, originZ ),
            new Vector3( originX + sizeX, 0, originZ + sizeZ ),
            new Vector3( originX,         0, originZ + sizeZ )
        };
 
        int[] triangles = new int[]
        {
            2, 1, 0,
            0, 3, 2
        };
 
        Vector2[] uvs = new Vector2[]
        {
            new Vector2( 0, 0 ),
            new Vector2( 1, 0 ),
            new Vector2( 1, 1 ),
            new Vector2( 0, 1 )
        };
 
        newMesh.vertices = vertices;
        newMesh.triangles = triangles;
        newMesh.uv = uvs;
 
        return ( newMesh );
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox