For example, i want those anchors point at those blue dots, if i uses the button in inspector, anchor will be placed according to the parent, but i want it place according to the current object, i noticed i spend 90% of my time adjusting those anchor, and sometimes I zoom in 1000x and still fail to adjust them perfectly. Are there any easy way to do that ?
I did that too, look at those texts and input fields, they are perfectly aligned, same size and perfectly anchored cause i spend like 20-30 mins calculating values, its almost impossible to make them perfectly the same size, and perfectly align, and perfectly anchored without doing that.
It usually takes me only seconds. Typically the anchor positions I want are either 0, 0.5, or 1 in each axis. What are you doing that requires such tricky calculations?
I want all UI element aligned, and same size(same type), gap between each element same too, and the size scales with screen size.
These are the values of my current textboxes
height of each textbox is 0.18, gap between them is 0.03, these are just textboxes, there are other things like buttons, labels etc, when there are too little space or too much space left, i need to recalculate again since those values are from 0 to 1 only.
There are several components that can do all that for you, content size filter, aspect ratio fitter, horizontal layout group, vertical layout group, and grid layout group. The right combination of these and properly configured, should allow your layout to stay intact even when resizing the screen.
Ah — you’re trying to do everything with the anchors, and not taking advantage of the other properties. And this is also why none of the anchor presets work for you (they don’t cover every situation, but they do cover most).
If this is because you really want the whole UI to scale uniformly with the screen size, then you should configure your canvas to do the scaling. Then you just define your UI in terms of “pixels” (really arbitrary screen units), and use the standard anchor modes.
Or, you just use containers to your advantage. Say I need a column of text boxes, each with a label to the left, and I want the text boxes to take up most of the width of their container. Well, an easy way to do this is to make a container (an empty GameObject) for the text boxes, which vertically extends the full height of its container, and horizontally, maybe we set Min X to 0.3 and Max X to 1 or whatever. Now I just toss in my text boxes, and set them to scale with the full width of their container (one of the standard anchor modes). And I also toss in labels, bound to the left side of their container, with Pivot X=1 and position X = -10 (or whatever gap I want). Note that visually they’re not in their container, but they are still positioned relative to it.
Nothing here has any funky anchors except the text box container. And that I can drag around or change how its size is computed, and all the text boxes and labels automatically update — the decision of how to size all those boxes is in one place, instead of being scattered around all the controls.
This, combined with the layout components @Bill_Martini mentioned (which I rarely need but I hear they’re cool), should let you arrange your UI without having to work so hard mucking about with all those anchors.
I tried to use Layouts, but failed, the main reason is i want the UI to scales according to screen size(will test in 800x600, and 2048x1536), and in layout element, things are measure in pixels, not in percent.
Or any of you able to provide me a sample project with a Canvas that has many UI elements(properly placed), it should be able to scale according to screen size. I want to know if im using the correct way.
Screen size is one thing, but aspect ratio changes are another.
If you just care about screen size, your best friend is the Canvas Scaler component that I believe is now added to new canvases by default. What I typically do is change the Reference Resolution to 1920x1080, ensure Screen Match Mode is set to Match Width or Height, and set Match to 1 (Height only).
Doing this, as long as the aspect ratio is the same (so if it’s a true 1920x1080 or some kind of crazy Retina display at that same ratio), my elements will all look the same in relation to each other, including their positioning. They’ll just scale to fit.
If you want your UI elements to also scale with aspect ratio, you’ve got to be a lot more careful. And your UI needs to be flexible, meaning you should use the Anchor Presets that have blue arrows, indicating they scale and stretch. If your Canvas is scaling UI reference points based on Height (which you should be, in my opinion), you’ll want to either always use horizontal stretching or be positive an explicitly defined width on a UI element will never overlap on your smallest supported resolution.
So if, for example, you want a button 50px (remember, that’s based on your reference resolution, not necessarily the number of pixels on the device) from the left edge of the screen, and never go past the halfway point, and my lowest supported resolution is 800x600, that button would be 350px wide. Keep in mind, that’ll make it 350px on all screen sizes, which is probably going to look bad in most of those. And that’s why I just aim at 16:9 ratios all the time.