How can I get a UI Panel's size in relation to the screen size?

I’m working on draggable ui panels, which is working fine. The problem is when I try to clamp the panel inside the screen so that it can’t be dragged off. I tried using the panels rect transform and screen.width and screen.height, but the latter two change with the screen size while the panel size stays the same.

You can add a canvas scaler component to your ui object and then set the ui scale mode to “scale with screen size”. You can find it by searching canvas scaler inside AddComponet menu in the inspector.