Search Unity

Unity UI About renderer.bounds scaling when parented under a Canvas

Discussion in 'UGUI & TextMesh Pro' started by Xarbrough, Nov 16, 2020.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    In my setup I have a 3d object (in this case only a scaled cube) in a UI hierarchy:

    upload_2020-11-16_22-25-40.png

    I'm now trying to scale the cube transform so that it fits certain criteria. For this to work correctly I need to know either of two things:

    1) The size of the cube's bounding box in canvas space (180, 120, 30).
    2) The size of the cube's bounding box in the cube's local space but only affected by rotation.

    I'm trying to set the localScale property of the cube, so I need to find these values dynamically.

    I tried to simply use renderer.bounds, but this shows some numbers around 5, since it gives me the world space size. This is also dependent on the canvas plane distance from the canvas, etc. If I could convert from this world space value to a canvas value (the larger number 180) which is scaled by the camera distance and the canvas scaler component, this would be enough to make it work for me.

    I could also make it work if I knew the size of the cube's bounding box but without the scaling applied to it via the canvas system. So e.g. if the mesh.bounds were (2,1,1), I could use this value, but I would also need to factor in the rotation to make the box larger if it were rotated 45 degrees. So, basically, transform mesh.bounds to cube transform space but without applying the scale.

    Thanks for any ideas!