MB3_BatchPrefabBaker.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DigitalOpus.MB.Core;
  5. public class MB3_BatchPrefabBaker : MonoBehaviour {
  6. public MB2_LogLevel LOG_LEVEL = MB2_LogLevel.info;
  7. [System.Serializable]
  8. public class MB3_PrefabBakerRow{
  9. public GameObject sourcePrefab;
  10. public GameObject resultPrefab;
  11. }
  12. public MB3_PrefabBakerRow[] prefabRows;
  13. public string outputPrefabFolder;
  14. [ContextMenu("Create Instances For Prefab Rows")]
  15. public void CreateSourceAndResultPrefabInstances()
  16. {
  17. #if UNITY_EDITOR
  18. // instantiate the prefabs
  19. List<GameObject> srcPrefabs = new List<GameObject>();
  20. List<GameObject> resultPrefabs = new List<GameObject>();
  21. for (int i = 0; i < prefabRows.Length; i++)
  22. {
  23. if (prefabRows[i].sourcePrefab != null && prefabRows[i].resultPrefab != null)
  24. {
  25. GameObject src = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefabRows[i].sourcePrefab);
  26. GameObject result = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefabRows[i].resultPrefab);
  27. srcPrefabs.Add(src);
  28. resultPrefabs.Add(result);
  29. }
  30. }
  31. Vector3 offsetX = new Vector3(2, 0, 0);
  32. // layout the prefabs
  33. GameObject srcRoot = new GameObject("SourcePrefabInstances");
  34. GameObject resultRoot = new GameObject("ResultPrefabInstance");
  35. Vector3 srcPos = Vector3.zero - offsetX;
  36. Vector3 resultPos = Vector3.zero + offsetX;
  37. for (int i = 0; i < srcPrefabs.Count; i++)
  38. {
  39. Renderer[] rs = srcPrefabs[i].GetComponentsInChildren<Renderer>(true);
  40. Bounds b = new Bounds(Vector3.zero, Vector3.one);
  41. if (rs.Length > 0)
  42. {
  43. b = rs[0].bounds;
  44. for (int bndsIdx = 1; bndsIdx < rs.Length; bndsIdx++)
  45. {
  46. b.Encapsulate(rs[bndsIdx].bounds);
  47. }
  48. }
  49. srcPrefabs[i].transform.parent = srcRoot.transform;
  50. resultPrefabs[i].transform.parent = resultRoot.transform;
  51. srcPrefabs[i].transform.localPosition = srcPos + new Vector3(-b.extents.x, 0, b.extents.z + b.extents.z * .3f);
  52. resultPrefabs[i].transform.localPosition = resultPos + new Vector3(b.extents.x, 0, b.extents.z + b.extents.z * .3f);
  53. srcPos += new Vector3(0,0,b.size.z + 1f);
  54. resultPos += new Vector3(0, 0, b.size.z + 1f);
  55. }
  56. #else
  57. Debug.LogError("Cannot be used outside the editor");
  58. #endif
  59. }
  60. }