Unity 计算包围盒

  • Post author:
  • Post category:其他


包围盒

请添加图片描述

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using ZYF;

public class BoundsGet : MonoBehaviour
{
    private void OnDrawGizmos()
    {
        var bounds = GetMaxBounds(gameObject);
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(center: bounds.center,size:bounds.size);
    }
    private Bounds GetMaxBounds(GameObject g)
    {
        Renderer[] rs = g.GetComponentsInChildren<Renderer>();
        if (rs.Length > 0)
        {
            Bounds b = rs[0].bounds;
            for (int i = 1; i < rs.Length; i++)
            {
                b.Encapsulate(rs[i].bounds);
            }
            return b;
        }
        else
        {
            return new Bounds();
        }
    }
}



版权声明:本文为qq_26318597原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。