Hi,
My problem boils down to me wanting convert from screen space to canvas space when my canvas is set to scale with screen size. I could change the canvas to not be scaled but other parts of this project require it. Any thoughts on what I’m doing wrong?
Some more detail: I want to make UI elements that the user can resize by dragging their borders, like a standard program window. So far I’m just trying to make a box whose right hand edge you can drag to resize. My set up looks like this.
I have a canvas whose UI scale mode is set to scale with screen size. In that I have a panel called Window anchored to the top left of the canvas, at a x position of 0. Inside that I have a second panel called Right Border along the right hand side of Window, anchored to it’s right hand corners.
I’ve set up a script on Right Border which should let the user drag it to resize Window horizontally. This works fine if my app window is 800 pixels wide (800 pixels is my canvas’ reference width). However if its larger the panel grows faster than the mouse moves and over takes it. I’ve attempted to take account of this but so far had no joy. My script’s OnDrag function looks like this;
public void OnDrag()
{
float delta = Input.mousePosition.x - m_mainPanelRect.anchoredPosition.x;
if (m_parentCanvasScaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize)
{
//delta = (delta / (float)Screen.width) * m_parentCanvasScaler.referenceResolution.x;
}
float newx = Mathf.Max(delta, 1.0f);
m_mainPanelRect.sizeDelta = new Vector2(newx, m_mainPanelRect.rect.height);
}
m_mainPanelRect is the rect of the Window panel and m_parentCanvasScaler is the scaler from the canvas. That commented out line is my attempt at fixing it but if I enable it the panel doesn’t grow nearly quickly enough and gets left behind the mouse.
The problem boils down to me wanting to set the size of a UI element on a scaled canvas, when I only know what size I want it to occupy on screen. I could change the canvas to not be scaled but other parts of this project require it. Any thoughts on what I’m doing wrong?