Unity 计算包围盒

包围盒

请添加图片描述

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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_26318597/article/details/127750925