BakeTexturesAtRuntime.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using UnityEngine;
  2. using System.Collections;
  3. using DigitalOpus.MB.Core;
  4. /*
  5. * For building atlases at runtime it is very important that:
  6. * - textures be in trucolor/RBGA32 format
  7. * - textures have read flag set
  8. *
  9. *
  10. * It is also Highly recommended to avoid resizing
  11. * - build padding into textures in editor
  12. * - don't use padding when creating atlases
  13. * - don't use tiled materials
  14. *
  15. * If you are having problems look at the Debug Log on the device
  16. */
  17. public class BakeTexturesAtRuntime : MonoBehaviour {
  18. public GameObject target;
  19. float elapsedTime = 0;
  20. MB3_TextureBaker.CreateAtlasesCoroutineResult result = new MB3_TextureBaker.CreateAtlasesCoroutineResult();
  21. void OnGUI(){
  22. GUILayout.Label("Time to bake textures: " + elapsedTime);
  23. if (GUILayout.Button("Combine textures & build combined mesh all at once")){
  24. MB3_MeshBaker meshbaker = target.GetComponentInChildren<MB3_MeshBaker>();
  25. MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();
  26. //These can be assets configured at runtime or you can create them
  27. // on the fly like this
  28. textureBaker.textureBakeResults = ScriptableObject.CreateInstance<MB2_TextureBakeResults>();
  29. textureBaker.resultMaterial = new Material( Shader.Find("Diffuse") );
  30. float t1 = Time.realtimeSinceStartup;
  31. textureBaker.CreateAtlases();
  32. elapsedTime = Time.realtimeSinceStartup - t1;
  33. meshbaker.ClearMesh(); //only necessary if your not sure whats in the combined mesh
  34. meshbaker.textureBakeResults = textureBaker.textureBakeResults;
  35. //Add the objects to the combined mesh
  36. meshbaker.AddDeleteGameObjects(textureBaker.GetObjectsToCombine().ToArray(), null, true);
  37. meshbaker.Apply();
  38. }
  39. if (GUILayout.Button("Combine textures & build combined mesh using coroutine"))
  40. {
  41. Debug.Log("Starting to bake textures on frame " + Time.frameCount);
  42. MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();
  43. //These can be assets configured at runtime or you can create them
  44. // on the fly like this
  45. textureBaker.textureBakeResults = ScriptableObject.CreateInstance<MB2_TextureBakeResults>();
  46. textureBaker.resultMaterial = new Material(Shader.Find("Diffuse"));
  47. //register an OnSuccess function to be called when texture baking is complete
  48. textureBaker.onBuiltAtlasesSuccess = new MB3_TextureBaker.OnCombinedTexturesCoroutineSuccess(OnBuiltAtlasesSuccess);
  49. StartCoroutine(textureBaker.CreateAtlasesCoroutine(null,result,false,null,.01f));
  50. }
  51. }
  52. void OnBuiltAtlasesSuccess()
  53. {
  54. Debug.Log("Calling success callback. baking meshes");
  55. MB3_MeshBaker meshbaker = target.GetComponentInChildren<MB3_MeshBaker>();
  56. MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();
  57. //elapsedTime = Time.realtimeSinceStartup - t1;
  58. if (result.isFinished &&
  59. result.success)
  60. {
  61. meshbaker.ClearMesh(); //only necessary if your not sure whats in the combined mesh
  62. meshbaker.textureBakeResults = textureBaker.textureBakeResults;
  63. //Add the objects to the combined mesh
  64. meshbaker.AddDeleteGameObjects(textureBaker.GetObjectsToCombine().ToArray(), null, true);
  65. meshbaker.Apply();
  66. }
  67. Debug.Log("Completed baking textures on frame " + Time.frameCount);
  68. }
  69. }