- Home /
Question by
ythl · Mar 01, 2018 at 06:17 PM ·
uibugcanvasbug report
World Space Canvas Bug?
Man, Unity's "World Space" Canvas seems super buggy.
It doesn't matter whether or not an Event camera is present, buttons work either way
However, if you modify any RectTransform properties in a script, all UI events stop working (including buttons, etc.)
I attach the following script to a world space canvas:
public float orthographicCameraWidth(Camera cam) {
return 2f * cam.orthographicSize * Camera.main.aspect;
}
public float orthographicCameraHeight(Camera cam) {
return 2f * cam.orthographicSize;
}
void Start () {
float width = orthographicCameraWidth(Camera.main);
float height = orthographicCameraHeight(Camera.main);
RectTransform rt = transform.GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(Screen.width, Screen.height);
rt.localScale = new Vector3(width/Screen.width, height/Screen.height, 0);
}
To auto-resize/scale it to my current resolution, and it works... except all events are broken, so none of the buttons work.
Yet if I manually set the width/height and x/y scale in the Editor so that it looks good in the Camera, everything magically works. What the heck is going on? Please tell me I'm missing something stupid.
Here's a gif of what I'm talking about:
I'm using 2017.3
Comment