MB2_TestUpdate.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityEngine;
  2. using System.Collections;
  3. public class MB2_TestUpdate : MonoBehaviour {
  4. public MB3_MeshBaker meshbaker;
  5. public MB3_MultiMeshBaker multiMeshBaker;
  6. public GameObject[] objsToMove;
  7. public GameObject objWithChangingUVs;
  8. Vector2[] uvs;
  9. Mesh m;
  10. void Start(){
  11. //Add the objects to the combined mesh
  12. //Must have previously baked textures for these in the editor
  13. meshbaker.AddDeleteGameObjects(objsToMove, null, true);
  14. meshbaker.AddDeleteGameObjects(new GameObject[]{objWithChangingUVs}, null, true);
  15. MeshFilter mf = objWithChangingUVs.GetComponent<MeshFilter>();
  16. m = mf.sharedMesh;
  17. uvs = m.uv;
  18. //apply the changes we made this can be slow. See documentation
  19. meshbaker.Apply();
  20. //same with multi mesh baker
  21. multiMeshBaker.AddDeleteGameObjects(objsToMove, null, true);
  22. multiMeshBaker.AddDeleteGameObjects(new GameObject[]{objWithChangingUVs}, null, true);
  23. mf = objWithChangingUVs.GetComponent<MeshFilter>();
  24. m = mf.sharedMesh;
  25. uvs = m.uv;
  26. multiMeshBaker.Apply();
  27. }
  28. void LateUpdate(){
  29. //Apply changes after this and other scripts have made changes
  30. //Only to vertecies, tangents and normals
  31. //Only want to call this once per frame since it is slow
  32. meshbaker.UpdateGameObjects(objsToMove,false);
  33. Vector2[] uvs2 = m.uv;
  34. for (int i = 0; i < uvs2.Length; i++){
  35. uvs2[i] = Mathf.Sin(Time.time) * uvs[i];
  36. }
  37. m.uv = uvs2;
  38. meshbaker.UpdateGameObjects(new GameObject[]{objWithChangingUVs},true,true,true,true,true,false,false,false,false);
  39. meshbaker.Apply(false,true,true,true,true,false,false,false,false,false);
  40. // now multi mesh baker
  41. multiMeshBaker.UpdateGameObjects(objsToMove,false);
  42. uvs2 = m.uv;
  43. for (int i = 0; i < uvs2.Length; i++){
  44. uvs2[i] = Mathf.Sin(Time.time) * uvs[i];
  45. }
  46. m.uv = uvs2;
  47. multiMeshBaker.UpdateGameObjects(new GameObject[]{objWithChangingUVs},true,true,true,true,true,false,false,false,false);
  48. multiMeshBaker.Apply(false,true,true,true,true,false,false,false,false,false);
  49. }
  50. }