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

查看:1354 |回复:49 | 2015-11-25 14:08:20

您需要 登录 才可以下载或查看,没有账号?注册

x
QQ截图20151125140112.jpg
Unity3d自动计算所有包围盒的中心点设置
在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教程到此结束。


2015-11-25 14:08:20  
 赞 赞 1

使用道具 登录

49个回答,把该问题分享到群,邀请大神一起回答。
3#
无私帖不要钱,谢谢啦哈!
回复 收起回复
2015-11-26 17:47:50   回复
 赞 赞 1

使用道具 登录

4#
这个经常会用到吧
回复 收起回复
2016-10-31 15:13:59   回复
 赞 赞 1

使用道具 登录

5#
感谢楼主无私分享~~
回复 收起回复
2016-11-6 12:16:27   回复
 赞 赞 1

使用道具 登录

6#
,多谢楼主分享
回复 收起回复
2018-10-15 10:47:26   回复
 赞 赞 1

使用道具 登录

8#
为了元素币,拼了!
回复 收起回复
2018-11-10 15:17:17   回复
 赞 赞 1

使用道具 登录

9#

有时候这个要用到  不错
回复 收起回复
2018-11-10 15:35:20   回复
 赞 赞 1

使用道具 登录

10#
超级棒棒棒
回复 收起回复
2018-11-11 12:53:24   回复
 赞 赞 1

使用道具 登录

11#
回复 收起回复
2018-11-22 09:05:57   回复
 赞 赞 1

使用道具 登录

12#
想要成大触,天天上元素!
回复 收起回复
2018-11-22 09:08:48   回复
 赞 赞 1

使用道具 登录

13#
不错的教程,谢谢分享
回复 收起回复
2018-11-22 09:36:31   回复
 赞 赞 1

使用道具 登录

14#
赞一个~666
回复 收起回复
2018-11-22 10:25:25   回复
 赞 赞 1

使用道具 登录

15#
资源哪里好,肯定元素找!
回复 收起回复
2018-11-22 10:41:34   回复
 赞 赞 1

使用道具 登录

16#
666666666
回复 收起回复
2018-11-22 10:48:38   回复
 赞 赞 1

使用道具 登录

17#
多谢楼主分享
回复 收起回复
2018-11-22 11:02:22   回复
 赞 赞 1

使用道具 登录

18#
6666666666666
回复 收起回复
2018-11-23 08:37:13   回复
 赞 赞 1

使用道具 登录

19#
感谢分享
回复 收起回复
2018-11-23 09:05:54   回复
 赞 赞 1

使用道具 登录

20#
元素大法好
回复 收起回复
2018-12-4 19:40:05   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
123下一页
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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