Unity3D:无论其规模如何,都将所有游戏对象转换为相同大小
Unity3D: Convert all game objects to same size irrespective of it's scale
假设我有一个参考游戏对象,立方体,比例为 (1,1,1)。还有另一个 GameObject 的比例为 (1,1,1),但大小是 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,1,1)。还有另一个 GameObject 的比例为 (1,1,1),但大小是 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;