Unity3D:将所有游戏对象转换为相同大小,而不考虑其大小

问题描述

假设我有一个比例为(1,1,1)的参考GameObject Cube。还有另一个缩放比例为(1,1)的GameObject,但其大小是Cube的3倍。如何动态更改游戏对象的比例以适合多维数据集的大小?

解决方法

在每个滤镜上获取网格滤镜,并使用Mesh.bounds.size来获取其尺寸。为了使第二个对象适合第一个对象,您可以根据网格大小的不同来更改第二个对象的比例。

// If you want it to be 100% accurate,you should multiply RefSize here 
// by RefMeshFilter.transform.lossyScale so that the other object's 
// size also accounts for the reference object's scale.
var RefSize = RefMeshFilter.bounds.size;
var MySize = MyMeshFilter.bounds.size;  
var NewScale = new Vector3(RefSize.x / MySize.x,RefSize.y / MySize.y,RefSize.z / MySize.z);

// I'm un-parenting and re-parenting here as a quick way of setting global/lossy scale
var Parent = MyMeshFilter.transform.parent;
MyMeshFilter.transform.parent = null;
MyMeshFilter.transform.localScale = NewScale;
MyMeshFilter.transform.parent = Parent;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...