MB3_MeshBakerEditor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 System.Text.RegularExpressions;
  12. using DigitalOpus.MB.Core;
  13. using UnityEditor;
  14. namespace DigitalOpus.MB.MBEditor
  15. {
  16. [CustomEditor(typeof(MB3_MeshBaker))]
  17. [CanEditMultipleObjects]
  18. public class MB3_MeshBakerEditor : Editor
  19. {
  20. MB3_MeshBakerEditorInternal mbe = new MB3_MeshBakerEditorInternal();
  21. [MenuItem("GameObject/Create Other/Mesh Baker/TextureBaker and MeshBaker", false, 100)]
  22. public static GameObject CreateNewMeshBaker()
  23. {
  24. MB3_TextureBaker[] mbs = (MB3_TextureBaker[])GameObject.FindObjectsOfType(typeof(MB3_TextureBaker));
  25. Regex regex = new Regex(@"\((\d+)\)$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
  26. int largest = 0;
  27. try
  28. {
  29. for (int i = 0; i < mbs.Length; i++)
  30. {
  31. Match match = regex.Match(mbs[i].name);
  32. if (match.Success)
  33. {
  34. int val = Convert.ToInt32(match.Groups[1].Value);
  35. if (val >= largest)
  36. largest = val + 1;
  37. }
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. if (e == null) e = null; //Do nothing supress compiler warning
  43. }
  44. GameObject nmb = new GameObject("TextureBaker (" + largest + ")");
  45. nmb.transform.position = Vector3.zero;
  46. MB3_TextureBaker tb = nmb.AddComponent<MB3_TextureBaker>();
  47. tb.packingAlgorithm = MB2_PackingAlgorithmEnum.MeshBakerTexturePacker;
  48. MB3_MeshBakerGrouper mbg = nmb.AddComponent<MB3_MeshBakerGrouper>();
  49. GameObject meshBaker = new GameObject("MeshBaker");
  50. MB3_MeshBaker mb = meshBaker.AddComponent<MB3_MeshBaker>();
  51. meshBaker.transform.parent = nmb.transform;
  52. mb.meshCombiner.settingsHolder = mbg;
  53. return nmb.gameObject;
  54. }
  55. [MenuItem("GameObject/Create Other/Mesh Baker/MeshBaker", false, 100)]
  56. public static GameObject CreateNewMeshBakerOnly()
  57. {
  58. MB3_MeshBaker[] mbs = (MB3_MeshBaker[])GameObject.FindObjectsOfType(typeof(MB3_MeshBaker));
  59. Regex regex = new Regex(@"\((\d+)\)$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
  60. int largest = 0;
  61. try
  62. {
  63. for (int i = 0; i < mbs.Length; i++)
  64. {
  65. Match match = regex.Match(mbs[i].name);
  66. if (match.Success)
  67. {
  68. int val = Convert.ToInt32(match.Groups[1].Value);
  69. if (val >= largest)
  70. largest = val + 1;
  71. }
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. if (e == null) e = null; //Do nothing supress compiler warning
  77. }
  78. GameObject meshBaker = new GameObject("MeshBaker (" + largest + ")");
  79. meshBaker.AddComponent<MB3_MeshBaker>();
  80. return meshBaker.gameObject;
  81. }
  82. void OnEnable()
  83. {
  84. mbe.OnEnable(serializedObject);
  85. }
  86. void OnDisable()
  87. {
  88. mbe.OnDisable();
  89. }
  90. public override void OnInspectorGUI()
  91. {
  92. mbe.OnInspectorGUI(serializedObject, (MB3_MeshBakerCommon)target, targets, typeof(MB3_MeshBakerEditorWindow));
  93. }
  94. }
  95. }