Need help with movement of UI elements position, in comparison to the canvas size.
I am making a game in unity 4.7.3, coding it in c#. This game is meant to be made for mobile, and for many different devices, meaning multiple screen sizes, which means multiple canvas sizes. I need UI elements to be moved at a certain percentage across the screen. For example, there is some text that needs to 60% of the way up the screen and 10% across from the left. Is there a way of doing this in script?
SO fat, my main issue is that I don't know how to move objects using the RectTransform, as using this.transform.position = doesn't work, as the world block sizes are different to the canvas sizes.
Is there a way of doing this, or should I just make 20 different versions, for 20 different screen sizes?
Answer by Mmmpies · May 31, 2016 at 02:12 PM
The easiest way to do this is simply move the anchors to the corner of the UI element you add like this...
On each screen it should scale to be proportionally the same for each. It's still difficult if you've got loads of differing screen shapes but it's the best way I've found so far.
Answer by yayhappyface1 · May 31, 2016 at 05:38 PM
For those that may find this in the future: Do what Mmmpies said, setting the anchor to the bottom left and then use this.
this.GetComponent().anchoredPosition = new Vector2(0.5f*Width, 0.5f*Height)
To find the Height and Width, do this: For Width, make sure the canvas scaler is set to Scale With Screen Size, and set the reference to 200 and something else, while also setting the Screen Match Mode to match by Width or height, and change the slider to full width. Set 200 to the width and then to find the length write this:
Length = (objectRectTransform.GetComponent().anchoredPosition.y/objectRectTransform.GetComponent().anchoredPosition.x) * 200f;