MB3_MeshBakerEditorInternal.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. //----------------------------------------------
  2. // MeshBaker
  3. // Copyright © 2011-2012 Ian Deane
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.IO;
  8. using System;
  9. using System.Collections.Specialized;
  10. using System.Collections.Generic;
  11. using DigitalOpus.MB.Core;
  12. using UnityEditor;
  13. namespace DigitalOpus.MB.MBEditor
  14. {
  15. public interface MB3_MeshBakerEditorWindowInterface
  16. {
  17. MonoBehaviour target
  18. {
  19. get;
  20. set;
  21. }
  22. }
  23. public class MB_EditorStyles
  24. {
  25. public GUIStyle multipleMaterialBackgroundStyle = new GUIStyle();
  26. public GUIStyle multipleMaterialBackgroundStyleDarker = new GUIStyle();
  27. public GUIStyle editorBoxBackgroundStyle = new GUIStyle();
  28. Texture2D multipleMaterialBackgroundColor;
  29. Texture2D multipleMaterialBackgroundColorDarker;
  30. Texture2D editorBoxBackgroundColor;
  31. public void Init()
  32. {
  33. bool isPro = EditorGUIUtility.isProSkin;
  34. Color backgroundColor = isPro
  35. ? new Color32(35, 35, 35, 255)
  36. : new Color32(174, 174, 174, 255);
  37. if (multipleMaterialBackgroundColor == null)
  38. {
  39. multipleMaterialBackgroundColor = MB3_MeshBakerEditorFunctions.MakeTex(8, 8, backgroundColor);
  40. }
  41. backgroundColor = isPro
  42. ? new Color32(50, 50, 50, 255)
  43. : new Color32(160, 160, 160, 255);
  44. if (multipleMaterialBackgroundColorDarker == null)
  45. {
  46. multipleMaterialBackgroundColorDarker = MB3_MeshBakerEditorFunctions.MakeTex(8, 8, backgroundColor);
  47. }
  48. backgroundColor = isPro
  49. ? new Color32(35, 35, 35, 255)
  50. : new Color32(174, 174, 174, 255);
  51. multipleMaterialBackgroundStyle.normal.background = multipleMaterialBackgroundColor;
  52. multipleMaterialBackgroundStyleDarker.normal.background = multipleMaterialBackgroundColorDarker;
  53. if (editorBoxBackgroundColor == null)
  54. {
  55. editorBoxBackgroundColor = MB3_MeshBakerEditorFunctions.MakeTex(8, 8, backgroundColor);
  56. }
  57. editorBoxBackgroundStyle.normal.background = editorBoxBackgroundColor;
  58. editorBoxBackgroundStyle.border = new RectOffset(0, 0, 0, 0);
  59. editorBoxBackgroundStyle.margin = new RectOffset(5, 5, 5, 5);
  60. editorBoxBackgroundStyle.padding = new RectOffset(10, 10, 10, 10);
  61. }
  62. public void DestroyTextures()
  63. {
  64. if (multipleMaterialBackgroundColor != null) GameObject.DestroyImmediate(multipleMaterialBackgroundColor);
  65. if (multipleMaterialBackgroundColorDarker != null) GameObject.DestroyImmediate(multipleMaterialBackgroundColorDarker);
  66. if (editorBoxBackgroundColor != null) GameObject.DestroyImmediate(editorBoxBackgroundColor);
  67. }
  68. }
  69. public class MB3_MeshBakerEditorInternal
  70. {
  71. //add option to exclude skinned mesh renderer and mesh renderer in filter
  72. //example scenes for multi material
  73. private static GUIContent
  74. gc_outputOptoinsGUIContent = new GUIContent("Output"),
  75. gc_logLevelContent = new GUIContent("Log Level"),
  76. gc_openToolsWindowLabelContent = new GUIContent("Open Tools For Adding Objects", "Use these tools to find out what can be combined, discover problems with meshes, and quickly add objects."),
  77. gc_objectsToCombineGUIContent = new GUIContent("Custom List Of Objects To Be Combined", "You can add objects here that were not on the list in the MB3_TextureBaker as long as they use a material that is in the Texture Bake Results"),
  78. gc_textureBakeResultsGUIContent = new GUIContent("Texture Bake Result", "When materials are combined a MB2_TextureBakeResult Asset is generated. Drag that Asset to this field to use the combined material."),
  79. gc_useTextureBakerObjsGUIContent = new GUIContent("Same As Texture Baker", "Build a combined mesh using using the same list of objects that generated the Combined Material"),
  80. gc_combinedMeshPrefabGUIContent = new GUIContent("Combined Mesh Prefab", "Create a new prefab asset an drag an empty game object to it. Drag the prefab asset to here."),
  81. gc_SortAlongAxis = new GUIContent("SortAlongAxis", "Transparent materials often require that triangles be rendered in a certain order. This will sort Game Objects along the specified axis. Triangles will be added to the combined mesh in this order."),
  82. gc_Settings = new GUIContent("Use Shared Settings", "Different bakers can share the same settings. If this field is None, then the settings below will be used. " +
  83. "Assign one of the following:\n" +
  84. " - Mesh Baker Settings project asset \n" +
  85. " - Mesh Baker Grouper scene instance \n");
  86. private SerializedObject meshBaker;
  87. private SerializedProperty logLevel, combiner, outputOptions, textureBakeResults, useObjsToMeshFromTexBaker, objsToMesh, mesh, sortOrderAxis;
  88. private SerializedProperty settingsHolder;
  89. private MB_MeshBakerSettingsEditor meshBakerSettingsThis;
  90. private MB_MeshBakerSettingsEditor meshBakerSettingsExternal;
  91. bool showInstructions = false;
  92. bool showContainsReport = true;
  93. MB_EditorStyles editorStyles = new MB_EditorStyles();
  94. Color buttonColor = new Color(.8f, .8f, 1f, 1f);
  95. void _init(SerializedObject mb)
  96. {
  97. this.meshBaker = mb;
  98. objsToMesh = meshBaker.FindProperty("objsToMesh");
  99. combiner = meshBaker.FindProperty("_meshCombiner");
  100. logLevel = combiner.FindPropertyRelative("_LOG_LEVEL");
  101. outputOptions = combiner.FindPropertyRelative("_outputOption");
  102. useObjsToMeshFromTexBaker = meshBaker.FindProperty("useObjsToMeshFromTexBaker");
  103. textureBakeResults = combiner.FindPropertyRelative("_textureBakeResults");
  104. mesh = combiner.FindPropertyRelative("_mesh");
  105. sortOrderAxis = meshBaker.FindProperty("sortAxis");
  106. settingsHolder = combiner.FindPropertyRelative("_settingsHolder");
  107. meshBakerSettingsThis = new MB_MeshBakerSettingsEditor();
  108. meshBakerSettingsThis.OnEnable(combiner, meshBaker);
  109. editorStyles.Init();
  110. }
  111. public void OnEnable(SerializedObject meshBaker)
  112. {
  113. _init(meshBaker);
  114. }
  115. public void OnDisable()
  116. {
  117. editorStyles.DestroyTextures();
  118. if (meshBakerSettingsThis != null) meshBakerSettingsThis.OnDisable();
  119. if (meshBakerSettingsExternal != null) meshBakerSettingsExternal.OnDisable();
  120. }
  121. public void OnInspectorGUI(SerializedObject meshBaker, MB3_MeshBakerCommon target, UnityEngine.Object[] targets, System.Type editorWindowType)
  122. {
  123. DrawGUI(meshBaker, target, targets, editorWindowType);
  124. }
  125. public void DrawGUI(SerializedObject meshBaker, MB3_MeshBakerCommon target, UnityEngine.Object[] targets, System.Type editorWindowType)
  126. {
  127. if (meshBaker == null)
  128. {
  129. return;
  130. }
  131. meshBaker.Update();
  132. showInstructions = EditorGUILayout.Foldout(showInstructions, "Instructions:");
  133. if (showInstructions)
  134. {
  135. EditorGUILayout.HelpBox("1. Bake combined material(s).\n\n" +
  136. "2. If necessary set the 'Texture Bake Results' field.\n\n" +
  137. "3. Add scene objects or prefabs to combine or check 'Same As Texture Baker'. For best results these should use the same shader as result material.\n\n" +
  138. "4. Select options and 'Bake'.\n\n" +
  139. "6. Look at warnings/errors in console. Decide if action needs to be taken.\n\n" +
  140. "7. (optional) Disable renderers in source objects.", UnityEditor.MessageType.None);
  141. EditorGUILayout.Separator();
  142. }
  143. MB3_MeshBakerCommon momm = (MB3_MeshBakerCommon)target;
  144. EditorGUILayout.PropertyField(logLevel, gc_logLevelContent);
  145. EditorGUILayout.PropertyField(textureBakeResults, gc_textureBakeResultsGUIContent);
  146. bool doingTextureArray = false;
  147. if (textureBakeResults.objectReferenceValue != null)
  148. {
  149. doingTextureArray = ((MB2_TextureBakeResults)textureBakeResults.objectReferenceValue).resultType == MB2_TextureBakeResults.ResultType.textureArray;
  150. showContainsReport = EditorGUILayout.Foldout(showContainsReport, "Shaders & Materials Contained");
  151. if (showContainsReport)
  152. {
  153. EditorGUILayout.HelpBox(((MB2_TextureBakeResults)textureBakeResults.objectReferenceValue).GetDescription(), MessageType.Info);
  154. }
  155. }
  156. EditorGUILayout.BeginVertical(editorStyles.editorBoxBackgroundStyle);
  157. EditorGUILayout.LabelField("Objects To Be Combined", EditorStyles.boldLabel);
  158. if (momm.GetTextureBaker() != null)
  159. {
  160. EditorGUILayout.PropertyField(useObjsToMeshFromTexBaker, gc_useTextureBakerObjsGUIContent);
  161. }
  162. else
  163. {
  164. useObjsToMeshFromTexBaker.boolValue = false;
  165. momm.useObjsToMeshFromTexBaker = false;
  166. GUI.enabled = false;
  167. EditorGUILayout.PropertyField(useObjsToMeshFromTexBaker, gc_useTextureBakerObjsGUIContent);
  168. GUI.enabled = true;
  169. }
  170. if (!momm.useObjsToMeshFromTexBaker)
  171. {
  172. if (GUILayout.Button(gc_openToolsWindowLabelContent))
  173. {
  174. MB3_MeshBakerEditorWindowInterface mmWin = (MB3_MeshBakerEditorWindowInterface)EditorWindow.GetWindow(editorWindowType);
  175. mmWin.target = (MB3_MeshBakerRoot)target;
  176. }
  177. object[] objs = MB3_EditorMethods.DropZone("Drag & Drop Renderers Or Parents Here To Add Objects To Be Combined", 300, 50);
  178. MB3_EditorMethods.AddDroppedObjects(objs, momm);
  179. EditorGUILayout.PropertyField(objsToMesh, gc_objectsToCombineGUIContent, true);
  180. EditorGUILayout.Separator();
  181. EditorGUILayout.BeginHorizontal();
  182. if (GUILayout.Button("Select Objects In Scene"))
  183. {
  184. List<MB3_MeshBakerCommon> selectedBakers = _getBakersFromTargets(targets);
  185. List<GameObject> obsToCombine = new List<GameObject>();
  186. foreach(MB3_MeshBakerCommon baker in selectedBakers) obsToCombine.AddRange(baker.GetObjectsToCombine());
  187. Selection.objects = obsToCombine.ToArray();
  188. if (momm.GetObjectsToCombine().Count > 0)
  189. {
  190. SceneView.lastActiveSceneView.pivot = momm.GetObjectsToCombine()[0].transform.position;
  191. }
  192. }
  193. if (GUILayout.Button(gc_SortAlongAxis))
  194. {
  195. MB3_MeshBakerRoot.ZSortObjects sorter = new MB3_MeshBakerRoot.ZSortObjects();
  196. sorter.sortAxis = sortOrderAxis.vector3Value;
  197. sorter.SortByDistanceAlongAxis(momm.GetObjectsToCombine());
  198. }
  199. EditorGUILayout.PropertyField(sortOrderAxis, GUIContent.none);
  200. EditorGUILayout.EndHorizontal();
  201. }
  202. else
  203. {
  204. GUI.enabled = false;
  205. EditorGUILayout.PropertyField(objsToMesh, gc_objectsToCombineGUIContent, true);
  206. GUI.enabled = true;
  207. }
  208. EditorGUILayout.EndVertical();
  209. EditorGUILayout.Space();
  210. EditorGUILayout.LabelField("Output", EditorStyles.boldLabel);
  211. EditorGUILayout.PropertyField(outputOptions, gc_outputOptoinsGUIContent);
  212. if (momm.meshCombiner.outputOption == MB2_OutputOptions.bakeIntoSceneObject)
  213. {
  214. //todo switch to renderer
  215. momm.meshCombiner.resultSceneObject = (GameObject)EditorGUILayout.ObjectField("Combined Mesh Object", momm.meshCombiner.resultSceneObject, typeof(GameObject), true);
  216. if (momm is MB3_MeshBaker)
  217. {
  218. string l = "Mesh";
  219. Mesh m = (Mesh)mesh.objectReferenceValue;
  220. if (m != null)
  221. {
  222. l += " (" + m.GetInstanceID() + ")";
  223. }
  224. Mesh nm = (Mesh)EditorGUILayout.ObjectField(new GUIContent(l), m, typeof(Mesh), true);
  225. if (nm != m)
  226. {
  227. Undo.RecordObject(momm, "Assign Mesh");
  228. ((MB3_MeshCombinerSingle)momm.meshCombiner).SetMesh(nm);
  229. mesh.objectReferenceValue = nm;
  230. }
  231. }
  232. }
  233. else if (momm.meshCombiner.outputOption == MB2_OutputOptions.bakeIntoPrefab)
  234. {
  235. if (momm.meshCombiner.settings.renderType == MB_RenderType.skinnedMeshRenderer)
  236. {
  237. EditorGUILayout.HelpBox("The workflow for baking Skinned Meshes into prefabs has changed as of version 29.1. " +
  238. "It is no longer necessary to manually copy bones to the target prefab after baking. This should happen automatically.", MessageType.Info);
  239. }
  240. momm.resultPrefab = (GameObject)EditorGUILayout.ObjectField(gc_combinedMeshPrefabGUIContent, momm.resultPrefab, typeof(GameObject), true);
  241. if (momm.resultPrefab != null)
  242. {
  243. string assetPath = AssetDatabase.GetAssetPath(momm.resultPrefab);
  244. if (assetPath == null || assetPath.Length == 0)
  245. {
  246. Debug.LogError("The " + gc_combinedMeshPrefabGUIContent.text + " must be a prefab asset, not a scene GameObject");
  247. momm.resultPrefab = null;
  248. } else
  249. {
  250. MB_PrefabType pt = MBVersionEditor.GetPrefabType(momm.resultPrefab);
  251. if (pt != MB_PrefabType.prefabAsset)
  252. {
  253. Debug.LogError("The " + gc_combinedMeshPrefabGUIContent.text + " must be a prefab asset, the prefab type was '" + pt + "'");
  254. momm.resultPrefab = null;
  255. }
  256. }
  257. }
  258. if (momm is MB3_MeshBaker)
  259. {
  260. string l = "Mesh";
  261. Mesh m = (Mesh)mesh.objectReferenceValue;
  262. if (m != null)
  263. {
  264. l += " (" + m.GetInstanceID() + ")";
  265. }
  266. Mesh nm = (Mesh)EditorGUILayout.ObjectField(new GUIContent(l), m, typeof(Mesh), true);
  267. if (nm != m)
  268. {
  269. Undo.RecordObject(momm, "Assign Mesh");
  270. ((MB3_MeshCombinerSingle)momm.meshCombiner).SetMesh(nm);
  271. mesh.objectReferenceValue = nm;
  272. }
  273. }
  274. }
  275. else if (momm.meshCombiner.outputOption == MB2_OutputOptions.bakeMeshAssetsInPlace)
  276. {
  277. EditorGUILayout.HelpBox("NEW! Try the BatchPrefabBaker component. It makes preparing a batch of prefabs for static/ dynamic batching much easier.", MessageType.Info);
  278. if (GUILayout.Button("Choose Folder For Bake In Place Meshes"))
  279. {
  280. string newFolder = EditorUtility.SaveFolderPanel("Folder For Bake In Place Meshes", Application.dataPath, "");
  281. if (!newFolder.Contains(Application.dataPath)) Debug.LogWarning("The chosen folder must be in your assets folder.");
  282. string folder = "Assets" + newFolder.Replace(Application.dataPath, "");
  283. List<MB3_MeshBakerCommon> selectedBakers = _getBakersFromTargets(targets);
  284. Undo.RecordObjects(targets, "Undo Set Folder");
  285. foreach (MB3_MeshBakerCommon baker in selectedBakers)
  286. {
  287. baker.bakeAssetsInPlaceFolderPath = folder;
  288. EditorUtility.SetDirty(baker);
  289. }
  290. }
  291. EditorGUILayout.LabelField("Folder For Meshes: " + momm.bakeAssetsInPlaceFolderPath);
  292. }
  293. if (momm is MB3_MultiMeshBaker)
  294. {
  295. MB3_MultiMeshCombiner mmc = (MB3_MultiMeshCombiner)momm.meshCombiner;
  296. mmc.maxVertsInMesh = EditorGUILayout.IntField("Max Verts In Mesh", mmc.maxVertsInMesh);
  297. }
  298. //-----------------------------------
  299. EditorGUILayout.Space();
  300. EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
  301. bool settingsEnabled = true;
  302. //------------- Mesh Baker Settings is a bit tricky because it is an interface.
  303. EditorGUILayout.Space();
  304. UnityEngine.Object obj = settingsHolder.objectReferenceValue;
  305. // Don't use a PropertyField because we may not be able to use the assigned object. It may not implement requried interface.
  306. obj = EditorGUILayout.ObjectField(gc_Settings, obj, typeof(UnityEngine.Object), true);
  307. if (obj == null)
  308. {
  309. settingsEnabled = true;
  310. settingsHolder.objectReferenceValue = null;
  311. if (meshBakerSettingsExternal != null)
  312. {
  313. meshBakerSettingsExternal.OnDisable();
  314. meshBakerSettingsExternal = null;
  315. }
  316. }
  317. else if (obj is GameObject)
  318. {
  319. // Check to see if there is a component on this game object that implements MB_IMeshBakerSettingsHolder
  320. MB_IMeshBakerSettingsHolder itf = (MB_IMeshBakerSettingsHolder)((GameObject)obj).GetComponent(typeof(MB_IMeshBakerSettingsHolder));
  321. if (itf != null)
  322. {
  323. settingsEnabled = false;
  324. Component settingsHolderComponent = (Component)itf;
  325. if (settingsHolder.objectReferenceValue != settingsHolderComponent)
  326. {
  327. settingsHolder.objectReferenceValue = settingsHolderComponent;
  328. meshBakerSettingsExternal = new MB_MeshBakerSettingsEditor();
  329. UnityEngine.Object targetObj;
  330. string propertyName;
  331. itf.GetMeshBakerSettingsAsSerializedProperty(out propertyName, out targetObj);
  332. SerializedProperty meshBakerSettings = new SerializedObject(targetObj).FindProperty(propertyName);
  333. meshBakerSettingsExternal.OnEnable(meshBakerSettings);
  334. }
  335. }
  336. else
  337. {
  338. settingsEnabled = true;
  339. settingsHolder = null;
  340. if (meshBakerSettingsExternal != null)
  341. {
  342. meshBakerSettingsExternal.OnDisable();
  343. meshBakerSettingsExternal = null;
  344. }
  345. }
  346. }
  347. else if (obj is MB_IMeshBakerSettingsHolder)
  348. {
  349. settingsEnabled = false;
  350. if (settingsHolder.objectReferenceValue != obj)
  351. {
  352. settingsHolder.objectReferenceValue = obj;
  353. meshBakerSettingsExternal = new MB_MeshBakerSettingsEditor();
  354. UnityEngine.Object targetObj;
  355. string propertyName;
  356. ((MB_IMeshBakerSettingsHolder)obj).GetMeshBakerSettingsAsSerializedProperty(out propertyName, out targetObj);
  357. SerializedProperty meshBakerSettings = new SerializedObject(targetObj).FindProperty(propertyName);
  358. meshBakerSettingsExternal.OnEnable(meshBakerSettings);
  359. }
  360. }
  361. else
  362. {
  363. Debug.LogError("Object was not a Mesh Baker Settings object.");
  364. }
  365. EditorGUILayout.Space();
  366. if (settingsHolder.objectReferenceValue == null)
  367. {
  368. // Use the meshCombiner settings
  369. meshBakerSettingsThis.DrawGUI(momm.meshCombiner, settingsEnabled, doingTextureArray);
  370. }
  371. else
  372. {
  373. if (meshBakerSettingsExternal == null)
  374. {
  375. meshBakerSettingsExternal = new MB_MeshBakerSettingsEditor();
  376. UnityEngine.Object targetObj;
  377. string propertyName;
  378. ((MB_IMeshBakerSettingsHolder)obj).GetMeshBakerSettingsAsSerializedProperty(out propertyName, out targetObj);
  379. SerializedProperty meshBakerSettings = new SerializedObject(targetObj).FindProperty(propertyName);
  380. meshBakerSettingsExternal.OnEnable(meshBakerSettings);
  381. }
  382. meshBakerSettingsExternal.DrawGUI(((MB_IMeshBakerSettingsHolder)settingsHolder.objectReferenceValue).GetMeshBakerSettings(), settingsEnabled, doingTextureArray);
  383. }
  384. Color oldColor = GUI.backgroundColor;
  385. GUI.backgroundColor = buttonColor;
  386. if (GUILayout.Button("Bake"))
  387. {
  388. List<MB3_MeshBakerCommon> selectedBakers = _getBakersFromTargets(targets);
  389. if (selectedBakers.Count > 1) Debug.Log("About to bake " + selectedBakers.Count);
  390. foreach(MB3_MeshBakerCommon baker in selectedBakers)
  391. {
  392. // Why are we caching and recreating the SerializedObject? Because "bakeIntoPrefab" corrupts the serialized object
  393. // and the meshBaker SerializedObject throws an NRE the next time it gets used.
  394. MB3_MeshBakerCommon mbr = (MB3_MeshBakerCommon) meshBaker.targetObject;
  395. bake(baker);
  396. meshBaker = new SerializedObject(mbr);
  397. }
  398. }
  399. GUI.backgroundColor = oldColor;
  400. string enableRenderersLabel;
  401. bool disableRendererInSource = false;
  402. if (momm.GetObjectsToCombine().Count > 0)
  403. {
  404. Renderer r = MB_Utility.GetRenderer(momm.GetObjectsToCombine()[0]);
  405. if (r != null && r.enabled) disableRendererInSource = true;
  406. }
  407. if (disableRendererInSource)
  408. {
  409. enableRenderersLabel = "Disable Renderers On Source Objects";
  410. }
  411. else
  412. {
  413. enableRenderersLabel = "Enable Renderers On Source Objects";
  414. }
  415. if (GUILayout.Button(enableRenderersLabel))
  416. {
  417. List<MB3_MeshBakerCommon> selectedBakers = _getBakersFromTargets(targets);
  418. foreach (MB3_MeshBakerCommon baker in selectedBakers)
  419. {
  420. baker.EnableDisableSourceObjectRenderers(!disableRendererInSource);
  421. }
  422. }
  423. meshBaker.ApplyModifiedProperties();
  424. meshBaker.SetIsDifferentCacheDirty();
  425. }
  426. public static void updateProgressBar(string msg, float progress)
  427. {
  428. EditorUtility.DisplayProgressBar("Combining Meshes", msg, progress);
  429. }
  430. public static bool bake(MB3_MeshBakerCommon mom)
  431. {
  432. SerializedObject so = null;
  433. return bake(mom, ref so);
  434. }
  435. private List<MB3_MeshBakerCommon> _getBakersFromTargets(UnityEngine.Object[] targs)
  436. {
  437. List<MB3_MeshBakerCommon> outList = new List<MB3_MeshBakerCommon>(targs.Length);
  438. for (int i = 0; i < targs.Length; i++)
  439. {
  440. outList.Add((MB3_MeshBakerCommon) targs[i]);
  441. }
  442. return outList;
  443. }
  444. /// <summary>
  445. /// Bakes a combined mesh.
  446. /// </summary>
  447. /// <param name="mom"></param>
  448. /// <param name="so">This is needed to work around a Unity bug where UnpackPrefabInstance corrupts
  449. /// a SerializedObject. Only needed for bake into prefab.</param>
  450. public static bool bake(MB3_MeshBakerCommon mom, ref SerializedObject so)
  451. {
  452. bool createdDummyTextureBakeResults = false;
  453. bool success = false;
  454. try
  455. {
  456. if (mom.meshCombiner.outputOption == MB2_OutputOptions.bakeIntoSceneObject ||
  457. mom.meshCombiner.outputOption == MB2_OutputOptions.bakeIntoPrefab)
  458. {
  459. success = MB3_MeshBakerEditorFunctions.BakeIntoCombined(mom, out createdDummyTextureBakeResults, ref so);
  460. }
  461. else
  462. {
  463. //bake meshes in place
  464. if (mom is MB3_MeshBaker)
  465. {
  466. if (MB3_MeshCombiner.EVAL_VERSION)
  467. {
  468. Debug.LogError("Bake Meshes In Place is disabled in the evaluation version.");
  469. }
  470. else
  471. {
  472. MB2_ValidationLevel vl = Application.isPlaying ? MB2_ValidationLevel.quick : MB2_ValidationLevel.robust;
  473. if (!MB3_MeshBakerRoot.DoCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly, new MB3_EditorMethods(), vl)) return false;
  474. List<GameObject> objsToMesh = mom.GetObjectsToCombine();
  475. success = MB3_BakeInPlace.BakeMeshesInPlace((MB3_MeshCombinerSingle)((MB3_MeshBaker)mom).meshCombiner, objsToMesh, mom.bakeAssetsInPlaceFolderPath, mom.clearBuffersAfterBake, updateProgressBar);
  476. }
  477. }
  478. else
  479. {
  480. Debug.LogError("Multi-mesh Baker components cannot be used for Bake In Place. Use an ordinary Mesh Baker object instead.");
  481. }
  482. }
  483. mom.meshCombiner.CheckIntegrity();
  484. }
  485. catch (Exception e)
  486. {
  487. Debug.LogError(e);
  488. }
  489. finally
  490. {
  491. if (createdDummyTextureBakeResults && mom.textureBakeResults != null)
  492. {
  493. MB_Utility.Destroy(mom.textureBakeResults);
  494. mom.textureBakeResults = null;
  495. }
  496. EditorUtility.ClearProgressBar();
  497. }
  498. return success;
  499. }
  500. }
  501. }