Unity, creating mesh with scripts

Source: Internet
Author: User

Create an empty Gameobject, add mesh filter and mesh renderer two component, and add a script createmeshscript:

Using Unityengine;
Using System.Collections;
[Executeineditmode]
public class Createmeshscript:monobehaviour {

void Awake () {

Gameobject.getcomponent<meshfilter> (). Mesh = Createmesh (n);
}

Mesh Createmesh (float width, float height)
{
Mesh m = new mesh ();
M.name = "Scriptedmesh";
Note:unity is left-hand system
M.vertices = new vector3[] {
New Vector3 (-width,-height, 0),
New Vector3 (-width, height, 0),
New Vector3 (width, height, 0),
New Vector3 (width,-height, 0)
};
M.UV = new vector2[] {
New Vector2 (0, 0),
New Vector2 (0, 1),
New Vector2 (1, 1),
New Vector2 (1, 0)
};
M.triangles = new int[] {0, 1, 2, 0, 2, 3};
M.recalculatenormals ();
M.recalculatebounds ();
return m;
}
}

You can now see that the mesh Filter->mesh in inspector is displayed as Scriptedmesh.

The mesh created at this time has no material, is displayed in purple, a new material is dragged onto the mesh renderer->materials->element 0 to complete the mesh creation.

Because [Executeineditmode] is added to the script, you can see the newly created mesh without running directly in the scene window.

It is also important to note that:

1,unity is the coordinate system is the left-hand line, if you do not pay attention to this point of the vertex order (normal) is reversed, may be drawn from the back of the object can not be seen.

2,mesh.vertices to use local coordinates.

Unity, creating mesh with scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.