近期热门
粉丝64
关注 0
获赞 3
Unity3d自动计算所有包围盒的中心点设置

[U3D] Unity3d自动计算所有包围盒的中心点设置

[复制链接]
1960 2 0 0 8年前 举报
QQ截图20151125140112.jpg
在unity3d开发中,相信程序有时候拿到的模型 transform中心点远离模型十万八千里,美术在做场景的时候可能会出现这个,与其相信美术或者策划我觉得程序要更相信自己!下面我们来看下在Unity 3d自动计算所有包围盒的中心点来使模型transform中心点居中!贴上代码先:
[MenuItem ("MyMenu/Do Test")]
static void Test ()
{
Transform parent = Selection.activeGameObject.transform;
Vector3 postion = parent.position;
Quaternion rotation = parent.rotation;
Vector3 scale = parent.localScale;
parent.position = Vector3.zero;
parent.rotation = Quaternion.Euler(Vector3.zero);
parent.localScale = Vector3.one;
Vector3 center = Vector3.zero;
Renderer[] renders = parent.GetComponentsInChildren();
foreach (Renderer child in renders){
center += child.bounds.center;
}
center /= parent.GetComponentsInChildren().Length;
Bounds bounds = new Bounds(center,Vector3.zero);
foreach (Renderer child in renders){
bounds.Encapsulate(child.bounds);
}
parent.position = postion;
parent.rotation = rotation;
parent.localScale = scale;
foreach(Transform t in parent){
t.position = t.position - bounds.center;
}
parent.transform.position = bounds.center + parent.position;
}
以上就是关于Unity3d自动计算所有包围盒的中心点 教程,本篇unity3d教程到此结束。
资源地址: http://cg.silucg.com/dongman/unity3d/7959.html


0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论2
您需要登录后才可以回帖 登录 | 立即注册

好好好好好好好好好
8年前
回复

使用道具 举报

感谢分享
8年前
回复

使用道具 举报