有时候会遇到相机观察某一物体,这个物体的尺寸是不确定的,可以是1米*1米*1米,也可能是1000m*1000m*1000m的物体。这样在动态加载物体时需要对物体进行位置上的调整保证物体全部显示在相机内:
先找到观察的物体
GameObject root = null;
然后对物体的子物体mesh进行合并:
if(root != null)
{
root.transform.localScale = Vector3.one;
if (root.transform.childCount > 0)
{
MeshFilter[] meshFilters = root.transform.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combineInstances = new CombineInstance[meshFilters.Length];
for (int k = 0; k < meshFilters.Length; k++)
{
combineInstances[k].mesh = meshFilters[k].sharedMesh;
combineInstances[k].transform = meshF