Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning UI elements with Anchor Presets via code

When we position UI elements in Unity, we fix the position from Anchor Presets, so that it's position is placed correctly on the canvas.

enter image description here

We select top, middle, bottom, stretch and the blue color dot.

How can I do the same thing if I create UI element in the code in C#?

I make

Texture2D textureWhite = new Texture2D(1, 1);
textureWhite.SetPixel(0, 0, Color.white);
textureWhite.Apply(); 

How can I fix as top left corner together with blue color dot?

like image 232
batuman Avatar asked Oct 15 '17 15:10

batuman


People also ask

How do you anchor UI elements in unity?

Select the Anchor Presets button in the Inspector, to open up the Anchor Presets options. Select the button corresponding to the Anchor Preset for top-center. Now when you resize the Game window, the UI Text element will stay in proportion to the top center of the canvas screen.

What are used to control the position of UI element within the layout?

android:layout_gravity : This attribute is used with a UI element to control where the element is arranged within its parent.

How do I set an anchor preset in unity?

Anchor presetsIn the Inspector, the Anchor Preset button can be found in the upper left corner of the Rect Transform component. Clicking the button brings up the Anchor Presets dropdown. From here you can quickly select from some of the most common anchoring options.

What is anchoring in UI?

Summary: Anchoring is a psychological principle which can impact how people perceive value and make decisions — in real life and on an interface.


1 Answers

The anchors(min,max) values and the pivot point value of the UI Object's RectTransform are what determines where the UI blue color dot is located.

If you hold the shift key and click on each preset, you will the circled properties in the image below change. You can then copy each variable and put them into code.

enter image description here

These properties are controlled from script via RectTransform.anchorMin, RectTransform.anchorMax and RectTransform.pivot.

Not exactly sure what the Texture2D code in your question has do with this but below are functions to set each preset:

//------------Top-------------------
void topLeft(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0, 1);
    uitransform.anchorMax = new Vector2(0, 1);
    uitransform.pivot = new Vector2(0, 1);
}

void topMiddle(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0.5f, 1);
    uitransform.anchorMax = new Vector2(0.5f, 1);
    uitransform.pivot = new Vector2(0.5f, 1);
}


void topRight(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(1, 1);
    uitransform.anchorMax = new Vector2(1, 1);
    uitransform.pivot = new Vector2(1, 1);
}

//------------Middle-------------------
void middleLeft(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0, 0.5f);
    uitransform.anchorMax = new Vector2(0, 0.5f);
    uitransform.pivot = new Vector2(0, 0.5f);
}

void middle(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0.5f, 0.5f);
    uitransform.anchorMax = new Vector2(0.5f, 0.5f);
    uitransform.pivot = new Vector2(0.5f, 0.5f);
}

void middleRight(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(1, 0.5f);
    uitransform.anchorMax = new Vector2(1, 0.5f);
    uitransform.pivot = new Vector2(1, 0.5f);
}

//------------Bottom-------------------
void bottomLeft(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0, 0);
    uitransform.anchorMax = new Vector2(0, 0);
    uitransform.pivot = new Vector2(0, 0);
}

void bottomMiddle(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(0.5f, 0);
    uitransform.anchorMax = new Vector2(0.5f, 0);
    uitransform.pivot = new Vector2(0.5f, 0);
}

void bottomRight(GameObject uiObject)
{
    RectTransform uitransform = uiObject.GetComponent<RectTransform>();

    uitransform.anchorMin = new Vector2(1, 0);
    uitransform.anchorMax = new Vector2(1, 0);
    uitransform.pivot = new Vector2(1, 0);
}
like image 69
Programmer Avatar answered Sep 21 '22 05:09

Programmer