MakeQuad

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Code)
(Code)
Line 9: Line 9:
  
 
== Code ==
 
== Code ==
 
=== CSharp - MakeQuad.cs ===
 
  
 
{{:User:Tenebrous}}
 
{{:User:Tenebrous}}
 +
 +
=== CSharp - MakeQuad.cs ===
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">

Revision as of 21:37, 10 February 2012

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

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