AddBoxCollider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class BatchGenerateBoxColliders : Editor
  6. {
  7. [MenuItem("警속툭旒竟/눼쉔")]
  8. static void AddSingleBoxCollider()
  9. {
  10. for (int i = 0; i < Selection.gameObjects.Length; i++)
  11. {
  12. AddGameObjectCollider(Selection.gameObjects[i]);
  13. }
  14. }
  15. [MenuItem("警속툭旒竟/눼쉔掘齡盧땡툭旒竟")]
  16. static void AddUnMoveBoxCollider()
  17. {
  18. for (int i = 0; i < Selection.gameObjects.Length; i++)
  19. {
  20. AddGameObjectWithCollider(Selection.gameObjects[i]);
  21. }
  22. }
  23. /// <summary>
  24. /// 警속툭旒竟
  25. /// </summary>
  26. /// <param name="gameObject"></param>
  27. public static void AddGameObjectCollider(GameObject gameObject)
  28. {
  29. Vector3 pos = gameObject.transform.localPosition;
  30. Quaternion qt = gameObject.transform.localRotation;
  31. Vector3 ls = gameObject.transform.localScale;
  32. gameObject.transform.position = Vector3.zero;
  33. gameObject.transform.eulerAngles = Vector3.zero;
  34. gameObject.transform.localScale = Vector3.one;
  35. //삿혤膠竟돨離鬼관鍋분
  36. Bounds itemBound = GetLocalBounds(gameObject);
  37. gameObject.transform.localPosition = pos;
  38. gameObject.transform.localRotation = qt;
  39. gameObject.transform.localScale = ls;
  40. //parent = null;
  41. if (!gameObject.GetComponent<Collider>())
  42. gameObject.AddComponent<BoxCollider>();
  43. if (gameObject.GetComponent<BoxCollider>())
  44. {
  45. gameObject.GetComponent<BoxCollider>().size = itemBound.size;
  46. gameObject.GetComponent<BoxCollider>().center = itemBound.center;
  47. }
  48. }
  49. /// <summary>
  50. /// 警속툭旒竟(깻눼쉔劤膠竟)
  51. /// </summary>
  52. /// <param name="gameObject"></param>
  53. public static void AddGameObjectWithCollider(GameObject gameObject)
  54. {
  55. Vector3 pos = gameObject.transform.localPosition;
  56. Quaternion qt = gameObject.transform.localRotation;
  57. Vector3 ls = gameObject.transform.localScale;
  58. gameObject.transform.position = Vector3.zero;
  59. gameObject.transform.eulerAngles = Vector3.zero;
  60. gameObject.transform.localScale = Vector3.one;
  61. //삿혤膠竟돨離鬼관鍋분
  62. Bounds itemBound = GetLocalBounds(gameObject);
  63. gameObject.transform.localPosition = pos;
  64. gameObject.transform.localRotation = qt;
  65. gameObject.transform.localScale = ls;
  66. //눼쉔膠竟
  67. GameObject UnMoveCollider = new GameObject();
  68. UnMoveCollider.transform.SetParent(gameObject.transform);
  69. UnMoveCollider.transform.localPosition = Vector3.zero;
  70. UnMoveCollider.transform.localRotation = Quaternion.identity;
  71. UnMoveCollider.transform.localScale = Vector3.one;
  72. UnMoveCollider.name = gameObject.name + "_UnMoveCollider";
  73. UnMoveCollider.tag = "UnMoveArea";
  74. BoxCollider boxCollider = UnMoveCollider.AddComponent<BoxCollider>();
  75. boxCollider.size = itemBound.size;
  76. boxCollider.center = itemBound.center;
  77. GameObject UnMoveRoot = GameObject.Find("UnMoveRoot");
  78. if (UnMoveRoot == null)
  79. {
  80. UnMoveRoot = new GameObject("UnMoveRoot");
  81. UnMoveRoot.transform.position = Vector3.zero;
  82. UnMoveRoot.transform.rotation = Quaternion.identity;
  83. UnMoveRoot.transform.localScale = Vector3.one;
  84. }
  85. UnMoveCollider.transform.SetParent(UnMoveRoot.transform);
  86. }
  87. /// <summary>
  88. /// 삿돤뚤蹶돨離鬼관鍋분
  89. /// </summary>
  90. public static Bounds GetLocalBounds(GameObject target)
  91. {
  92. Renderer[] mfs = target.GetComponentsInChildren<Renderer>();
  93. Bounds bounds = new Bounds();
  94. if (mfs.Length != 0)
  95. {
  96. bounds = mfs[0].bounds;
  97. foreach (Renderer mf in mfs)
  98. {
  99. bounds.Encapsulate(mf.bounds);
  100. }
  101. }
  102. return bounds;
  103. }
  104. }