Unity3D:获取GameObject的size

1.Renderer:渲染尺寸,受缩放影响,受旋转影响。3D图像投影在2D平面上的尺寸

 

2.MeshFilter:原始尺寸,不受缩放影响

计算真实的size:需要乘以localScale

如:gameObject.GetComponent<MeshFilter>().mesh.bounds.size.x*gameObject.transform.localScale.x

 

3.Collider:碰撞体尺寸,受缩放影响,受旋转影响。3D图像投影在2D平面上的尺寸

 

    void onClick_scale_percent(){
//        stlObjects [0].clingToBottom ();

        GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
        Debug.LogError ("cube Renderer size:"+cube.GetComponent<Renderer>().bounds.size);//default:1,1,1
        Debug.LogError ("cube MeshFilter size:"+cube.GetComponent<MeshFilter>().mesh.bounds.size);
        Debug.LogError ("cube collider size:"+cube.GetComponent<Collider>().bounds.size);

        cube.transform.parent = go_printCuber.transform;
        cube.transform.localPosition = new Vector3 (0, 0, 0);
        cube.transform.localScale = new Vector3 (0.5f, 0.5f, 0.5f);

        cube.transform.Rotate (0, 45, 0);

        Debug.LogError ("-------------------");
        Debug.LogError ("cube Renderer size:"+cube.GetComponent<Renderer>().bounds.size);
        Debug.LogError ("cube MeshFilter size:"+cube.GetComponent<MeshFilter>().mesh.bounds.size);
        Debug.LogError ("cube collider size:"+cube.GetComponent<Collider>().bounds.size);

        Debug.LogError ("cube localScale:"+cube.transform.localScale);//default:1,1,1
    }

 

 


    

posted @ 2017-04-19 18:00  MakeBetter  阅读(7220)  评论(0编辑  收藏  举报