Positioning UI elements with Anchor Presets via code

瘦欲@ 提交于 2020-08-24 07:07:10

问题


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.

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?


回答1:


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.

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);
}



回答2:


You can use these to control anchor presets:

http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html

look at this for more details: http://answers.unity3d.com/questions/1007886/how-to-set-the-new-unity-ui-rect-transform-anchor.html



来源:https://stackoverflow.com/questions/46756823/positioning-ui-elements-with-anchor-presets-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!