123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class BatchGenerateBoxColliders : Editor
- {
- [MenuItem("警속툭旒竟/눼쉔")]
- static void AddSingleBoxCollider()
- {
- for (int i = 0; i < Selection.gameObjects.Length; i++)
- {
- AddGameObjectCollider(Selection.gameObjects[i]);
- }
- }
- [MenuItem("警속툭旒竟/눼쉔掘齡盧땡툭旒竟")]
- static void AddUnMoveBoxCollider()
- {
- for (int i = 0; i < Selection.gameObjects.Length; i++)
- {
- AddGameObjectWithCollider(Selection.gameObjects[i]);
- }
- }
- /// <summary>
- /// 警속툭旒竟
- /// </summary>
- /// <param name="gameObject"></param>
- public static void AddGameObjectCollider(GameObject gameObject)
- {
- Vector3 pos = gameObject.transform.localPosition;
- Quaternion qt = gameObject.transform.localRotation;
- Vector3 ls = gameObject.transform.localScale;
- gameObject.transform.position = Vector3.zero;
- gameObject.transform.eulerAngles = Vector3.zero;
- gameObject.transform.localScale = Vector3.one;
- //삿혤膠竟돨離鬼관鍋분
- Bounds itemBound = GetLocalBounds(gameObject);
- gameObject.transform.localPosition = pos;
- gameObject.transform.localRotation = qt;
- gameObject.transform.localScale = ls;
- //parent = null;
- if (!gameObject.GetComponent<Collider>())
- gameObject.AddComponent<BoxCollider>();
- if (gameObject.GetComponent<BoxCollider>())
- {
- gameObject.GetComponent<BoxCollider>().size = itemBound.size;
- gameObject.GetComponent<BoxCollider>().center = itemBound.center;
- }
- }
- /// <summary>
- /// 警속툭旒竟(깻눼쉔劤膠竟)
- /// </summary>
- /// <param name="gameObject"></param>
- public static void AddGameObjectWithCollider(GameObject gameObject)
- {
- Vector3 pos = gameObject.transform.localPosition;
- Quaternion qt = gameObject.transform.localRotation;
- Vector3 ls = gameObject.transform.localScale;
- gameObject.transform.position = Vector3.zero;
- gameObject.transform.eulerAngles = Vector3.zero;
- gameObject.transform.localScale = Vector3.one;
- //삿혤膠竟돨離鬼관鍋분
- Bounds itemBound = GetLocalBounds(gameObject);
- gameObject.transform.localPosition = pos;
- gameObject.transform.localRotation = qt;
- gameObject.transform.localScale = ls;
- //눼쉔膠竟
- GameObject UnMoveCollider = new GameObject();
- UnMoveCollider.transform.SetParent(gameObject.transform);
- UnMoveCollider.transform.localPosition = Vector3.zero;
- UnMoveCollider.transform.localRotation = Quaternion.identity;
- UnMoveCollider.transform.localScale = Vector3.one;
- UnMoveCollider.name = gameObject.name + "_UnMoveCollider";
- UnMoveCollider.tag = "UnMoveArea";
- BoxCollider boxCollider = UnMoveCollider.AddComponent<BoxCollider>();
- boxCollider.size = itemBound.size;
- boxCollider.center = itemBound.center;
- GameObject UnMoveRoot = GameObject.Find("UnMoveRoot");
- if (UnMoveRoot == null)
- {
- UnMoveRoot = new GameObject("UnMoveRoot");
- UnMoveRoot.transform.position = Vector3.zero;
- UnMoveRoot.transform.rotation = Quaternion.identity;
- UnMoveRoot.transform.localScale = Vector3.one;
- }
- UnMoveCollider.transform.SetParent(UnMoveRoot.transform);
- }
- /// <summary>
- /// 삿돤뚤蹶돨離鬼관鍋분
- /// </summary>
- public static Bounds GetLocalBounds(GameObject target)
- {
- Renderer[] mfs = target.GetComponentsInChildren<Renderer>();
- Bounds bounds = new Bounds();
- if (mfs.Length != 0)
- {
- bounds = mfs[0].bounds;
- foreach (Renderer mf in mfs)
- {
- bounds.Encapsulate(mf.bounds);
- }
- }
- return bounds;
- }
- }
|