在Unity开发中,相信程序有时候拿到的模型 transform中心点远离模型十万八千里,美术在做场景的时候可能会出现这个,与其相信美术或者策划,我觉得程序要更相信自己
下面我们来看下在Unity自动计算所有包围盒的中心点来使模型transform中心点居中
[MenuItem ("Tools/SetModelCenter")]
public static void SetModelCenter()
{
Transform trans = Selection.activeGameObject.transform;
Vector3 postion = trans.position;
Quaternion rotation = trans.rotation;
Vector3 scale = trans.localScale;
trans.position = Vector3.zero;
trans.rotation = Quaternion.Euler(Vector3.zero);
trans.localScale = Vector3.one;
Vector3 center = Vector3.zero;
Renderer[] renders = trans.GetComponentsInChildren<Renderer>();
foreach (Renderer child in renders)
{
center += child.bounds.center;
}
center /= trans.GetComponentsInChildren().Length;
Bounds bounds = new Bounds(center, Vector3.zero);
foreach (Renderer child in renders)
{
bounds.Encapsulate(child.bounds);
}
trans.position = postion;
trans.rotation = rotation;
trans.localScale = scale;
foreach(Transform t in trans)
{
t.position = t.position - bounds.center;
}
trans.transform.position = bounds.center + trans.position;
}
原文地址:https://linxinfa.blog.csdn.net
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。