I have an UI image that I made expand to around 20% of the screensize, and now im trying to set it’s location to the bottom right with a small gap (1% of the screensize)
I’ve manage to set the scale but not the location. I’m not sure what im doing wrong but I even tried debugging why it doesn’t set it but even that gave me contradictions.
The positioning of UI elements is very complex.
When I have to do it, I normally do it via anchors. so I would set the Anchor Min and Anchor Max to the same value (right botom would be { 1, 0 }) and the local position to zero. You can adjust the distance to the anchors with the pivot.
BTW, scaling within UI is often not a good idea. for a simple image it is okay. but as soon as you want to do more layouting, scale will screw up your whole setup. The RectTransform uses sizeDelta for the size which takes layout settings into account.
EDIT: Why do you set the scale every frame? You don’t change it…
What is the script for actually? Do you just want to be reloution independent? Then click on Better UI in my signature and watch the third video. You don’t have to buy Better UI, but the video also describes how you can achive it with unity itself.
The reason I tried what I did is because for some reason, anchoring seems to not work with different aspect ratios.
Look at my native screen display
vs a typical 3:2 ratios, they don’t seem to respect distance.
Why is that?
Answer to your edit: I was only testing it so that it updates every time I change resolutions, which for simplicity’s sake I simply putted it in update. The script itself is mainly for 2 things:
1: It was for anchoring the image properly to the bottom right
2: scaling it up to the right screen size.
Using anchors I can only seem to be able to do one of the 2, not both at the same time.
the problem is that your anchor is on one certain position and your graphic has a pixel distance between that position and the pivot point. when you change your resolution the pixel distance stays the same.
so you need relative distances.
there are two ways to achieve that: