12345678910111213141516171819202122232425262728293031 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class JISuanBox
- {
- public static void setBox(GameObject gameObject)
- {
- Transform[] children = gameObject.GetComponentsInChildren<Transform>();
- Bounds totalBounds = new Bounds();
- totalBounds.center = gameObject.transform.position;
- foreach (Transform child in children)
- {
- MeshRenderer meshFilter = child.GetComponent<MeshRenderer>();
- if (meshFilter != null)
- {
- totalBounds.Encapsulate(meshFilter.bounds);
- }
- }
- Vector3 totalSize = totalBounds.size;
- BoxCollider box = gameObject.GetComponent<BoxCollider>();
- if (box == null)
- {
- box = gameObject.AddComponent<BoxCollider>();
- }
- box.size = totalSize / gameObject.transform.localScale.x;
- box.center = gameObject.transform.InverseTransformPoint(totalBounds.center);
- }
- }
|