How do I set a Canvas to a specific size in world units in unity?

Viewed 8481

Assuming that I have a UI Canvas which is set to 'World Space' and positioned absolutely in the world above my cube.

My cube is resized at runtime.

Without scaling the canvas, how do I set the size of the canvas to match the known world unit width of my cube?

I know that you can set the canvas in 'canvas units' using:

var rect = canvas.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(200, 200);

However, 200x200 are canvas units, not world units; my cube is 4 units wide; what should the sizeDelta be set to?

I presume this has something to do with the CanvasScale component, but I can't figure it out; referenceUnitsPerPixel is 100, and that's completely wrong as a scale factor, and dynamicPixelsPerUnit is like 1, which is wrong too.

2 Answers
Related