Basically what i am trying to do is write a script that can resize Ui element that are child of the canvas dynamically so the user can have control over the size and placement of them. Placement and moving them was easy, resizing them based on input how ever is giving me fits lol. I clearly am either not understanding how anchors work or i am just not taking the proper approach.
I’ve read the doc’s and tried a bunch of different tutorials and asset store scripts and i still couldn’t find
a “simple” working solution. Tyvm in advance for your time i do greatly appreciate it and if i could have slogged it out on my own i would have.
using UnityEngine;
using System.Collections;
public class ResizeElements : MonoBehaviour {
public RectTransform elementSize; //Assigning this to my canvas child panel i want to resize from the editor.
public float sizingFactor = 0.02f;
private float startSizeX;
private float startSizeY;
private float startX;
private float startY;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
/*Here it is resizing my panel too 0 or 1 based off of my input not working the way i intended at all
but i cant quit figure out how to make it do what i want it to do.
*/
Vector2 position = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
startX = position.x;
startY = position.y;
Debug.Log("Mouse position X = " + startX);
Debug.Log("Mouse position Y = " + startY);
}
if (Input.GetMouseButton(0))
{
Vector2 size = new Vector2(elementSize.sizeDelta.x, elementSize.sizeDelta.y);
size.x = startSizeX + (Input.mousePosition.x - startX) * sizingFactor;
size.y = startSizeY + (Input.mousePosition.y - startY) * sizingFactor;
elementSize.sizeDelta = size;
}
}
}