selzero

Manipulate Mesh 1

Sep 22nd, 2012
2,447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ChangeMesh : MonoBehaviour {
  4.     //The source object that we will be copying
  5.     //vertex data from
  6.     public GameObject goSource;
  7.     void Start() {
  8.         //Get the mesh of the source object
  9.         Mesh meshObjectsMesh = (Mesh)(goSource.GetComponent().mesh);
  10.         //Create a new mesh that we will assign to this object
  11.         Mesh mesh = new Mesh();
  12.         //Get the vertices from the source object and assign
  13.         //them to the new mesh.
  14.         mesh.vertices = meshObjectsMesh.vertices;
  15.         //Get the uv map from the source object and assign
  16.         //it to the new mesh.
  17.         mesh.uv = meshObjectsMesh.uv;
  18.         //Get the triangles from the source object and assign
  19.         //them to the new mesh.
  20.         mesh.triangles = meshObjectsMesh.triangles;
  21.         //We can ask the engine to recalculate the normals  
  22.         //of the mesh, the normals are the surfaces that are rendered.
  23.         mesh.RecalculateNormals();
  24.         //We have to call RecalculateBounds to make sure
  25.         //that the triangles from the vertex relationships
  26.         //are recalculated correctly
  27.         mesh.RecalculateBounds();
  28.         //set the new mesh as the mesh for this object
  29.         GetComponent().mesh = mesh;
  30.     }
  31. }
RAW Paste Data