美文网首页Unity编辑器开发分享unity修炼之路Unity基础入门分享
Unity3D研究院之自动计算所有包围盒的中心点(七)

Unity3D研究院之自动计算所有包围盒的中心点(七)

作者: 霸俊流年 | 来源:发表于2017-09-25 21:16 被阅读8次

    转自:http://www.xuanyusong.com/archives/3643
    美术在做场景的时候可能会出现这个情况? 如下图所示,这个地块的中心点远离模型十万八千里?与其相信美术或者策划我觉得程序要更相信自己!!


    C#

    [MenuItem("MyMenu/Do Test")]
    staticvoidTest()
    {
    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<Renderer>();
    foreach(Renderer child inrenders){
    center+=child.bounds.center;
    }
    center/=parent.GetComponentsInChildren<Transform>().Length;
    Bounds bounds=newBounds(center,Vector3.zero);
    foreach(Renderer child inrenders){
    bounds.Encapsulate(child.bounds);
    }
    parent.position=postion;
    parent.rotation=rotation;
    parent.localScale=scale;
    foreach(Transformtinparent){
    t.position=t.position-bounds.center;
    }
    parent.transform.position=bounds.center+parent.position;
    }
    

    OK 大功告成。 中心点居中了。。

    本文固定链接: http://www.xuanyusong.com/archives/3643
    转载请注明: 雨松MOMO2015年09月09日 于 雨松MOMO程序研究院 发表

    相关文章

      网友评论

        本文标题:Unity3D研究院之自动计算所有包围盒的中心点(七)

        本文链接:https://www.haomeiwen.com/subject/wftjextx.html