Unity3d: Changing the mesh of Gameobject

Source: Internet
Author: User

1. Only regenerate new mesh, re-assign value

2. The generated mesh must be in accordance with official documents and specify: The index array of vertices and triangles, otherwise not.

A) Assign vertices
b) Assign triangles.

3. Example: Use code to generate a mesh for a specified size cube:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 GameObject createCube (Vector3 meshBoundSize){    GameObject go = GameObject.CreatePrimitive (PrimitiveType.Cube);    Mesh mesh = newMesh ();    floatx = meshBoundSize.x / 2;    floaty = meshBoundSize.y / 2;    floatz = meshBoundSize.z / 2;    mesh.vertices = newVector3[] {          newVector3 (-x, -y, -z),         newVector3 (x, -y, -z),         newVector3 (x, y, -z),         newVector3 (-x, y, -z),         newVector3 (-x, y, z),         newVector3 (x, y, z),         newVector3 (x, -y, z),         newVector3 (-x, -y, z),    };     //anticlockwise    mesh.triangles = newint[] {         3, 1, 0,         3, 2, 1,         2, 3, 4,         2, 4, 5,         7, 5, 4,         7, 6, 5,         0, 6, 7,         0, 1, 6,         3, 7, 4,         3, 0, 7,         1, 5, 6,         1, 2, 5     };     mesh.RecalculateNormals ();    mesh.RecalculateBounds ();    go.GetComponent<MeshFilter> ().mesh = mesh;    Material material = newMaterial (Shader.Find ("Transparent/Diffuse"));    Color color = Color.red;    color.a = 0.2f;    material.SetColor ("_Color", color);    MeshRenderer renderer = go.GetComponent<MeshRenderer> ();    renderer.sharedMaterial = material;    returngo;}

Unity3d: Changing the mesh of Gameobject

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.