找回密码
 立即注册
查看: 10388|回复: 85

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

[复制链接]
发表于 2015-11-25 14:26 | 显示全部楼层 |阅读模式

在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

发表于 2017-4-5 12:06 | 显示全部楼层
很不错
发表于 2017-4-5 12:29 | 显示全部楼层
顶顶多好
发表于 2017-4-5 12:24 | 显示全部楼层
难得一见的好帖
发表于 2017-4-5 12:23 | 显示全部楼层
说的非常好
发表于 2017-4-5 12:43 | 显示全部楼层
LZ真是人才
发表于 2017-4-10 07:57 | 显示全部楼层
楼主是超人
发表于 2017-4-10 08:13 | 显示全部楼层
真心顶
发表于 2017-4-10 07:54 | 显示全部楼层
说的非常好
发表于 2017-4-10 07:52 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 04:56 , Processed in 0.157896 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表