How do I transform/resize a polygon from within a C# Script Component?
Hi Everyone!
I'm currently trying to set up a script that transforms/resizes a polygon drawn the GUI Canvas from within a C# Script within the same object. The Polygon itself is drawn using CiaccoDavide's [Unity-UI-Polygon Extension][1], which appears to house a regular Rect Transform Component.
Basically, what I'm trying to do is made a script that grows/shrinks a polygon depending on a relationship between two variables. The 'base' size of the Polygon is 248x248, and so the (general) formula I'm working with is:
width = (breathCount / breathCountMax)*248;
height = (breathCount /breathCountMax)*248;
I've been hunting around various tutorials and queries about Transform, RectTransform, and all kinds of things, but without much (any) luck at all. In the interests of full disclosure, this is my first attempt at a Unity Project (coming from GameMaker), and so I'm probably a little over my head for now.
SO far my attempt has looked like this (along with some subtle deviations, none of which worked):
public class breathTransform : MonoBehaviour
{
// Start is called before the first frame update
GameObject obj;
public RectTransform rT;
void Start()
{
obj = GameObject.Find("breath_UI_cur");
rT = obj.GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
rT.sizeDelta = new Vector2((rT.sizeDelta.x * ((breathing.breathCount / breathing.breathCountMax) * 248)), (rT.sizeDelta.y * ((breathing.breathCount / breathing.breathCountMax) * 248)));
}
Any help is greatly appreciated, as this query is boggling my mind! It's something that's relatively simple in GameMaker, but I can't wrap my head around it yet in Unity! [1]: https://github.com/CiaccoDavide/Unity-UI-Polygon
Answer by streeetwalker · Apr 05, 2020 at 07:18 AM
@CitronMW, I believe it will be easier setting the local scale.
If the base size is 248. then the scale you need in any dimension will be desired size/248. So set those for x and y, for z you can use 1 because UI elements don't have a z size, per se.
for example,
float scaleX = desiredXSize/248;
float scaleY = desiredYSize/248;
rT.localScale = new Vector3( scaleX, scaleY, 1 );
Hi! Thanks for the insight, that makes a lot of sense!
At the moment, I'm getting the below error on runtime:
UnassignedReferenceException: The variable rT of breathTransform has not been assigned. You probably need to assign the rT variable of the breathTransform script in the inspector.
Which I don't quite understand. From what I can see, rT is defined in the constructor, and assigned in the Start() function. Where am I going wrong with my variables?
Thanks so much for your help! Unity has always been super daunting to me, but I'm finally starting to make a little headway, so I really appreciate the help! :)
Edit: Scratch that, it was a capitalisation error in my code, which I have now fixed and everything works perfectly! Thanks for your help @streeetwalker, your solution is much cleaner than what I had, too! :)