通过代码使用锚预设定位 UI 个元素
Positioning UI elements with Anchor Presets via code
当我们在 Unity 中定位 UI 个元素时,我们从锚预设中固定位置,以便它的位置正确放置在 canvas.
我们select顶部、中间、底部、拉伸和蓝色圆点。
如果我在 C# 代码中创建 UI 元素,我该如何做同样的事情?
我做
Texture2D textureWhite = new Texture2D(1, 1);
textureWhite.SetPixel(0, 0, Color.white);
textureWhite.Apply();
如何固定为左上角和蓝色圆点?
您可以使用这些来控制锚预设:
http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html
看这个了解更多详情:http://answers.unity3d.com/questions/1007886/how-to-set-the-new-unity-ui-rect-transform-anchor.html
UI 对象的 RectTransform
的锚点(最小值,最大值)值和枢轴点值决定了 UI 蓝色点的位置。
如果你按住shift键并点击每个预设,你将在下图中圈出的属性发生变化。然后您可以复制每个变量并将它们放入代码中。
这些属性由脚本通过 RectTransform.anchorMin
、RectTransform.anchorMax
和 RectTransform.pivot
控制。
不确定您问题中的 Texture2D
代码与此有何关系,但以下是设置每个预设的函数:
//------------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);
}
当我们在 Unity 中定位 UI 个元素时,我们从锚预设中固定位置,以便它的位置正确放置在 canvas.
我们select顶部、中间、底部、拉伸和蓝色圆点。
如果我在 C# 代码中创建 UI 元素,我该如何做同样的事情?
我做
Texture2D textureWhite = new Texture2D(1, 1);
textureWhite.SetPixel(0, 0, Color.white);
textureWhite.Apply();
如何固定为左上角和蓝色圆点?
您可以使用这些来控制锚预设:
http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html
看这个了解更多详情:http://answers.unity3d.com/questions/1007886/how-to-set-the-new-unity-ui-rect-transform-anchor.html
UI 对象的 RectTransform
的锚点(最小值,最大值)值和枢轴点值决定了 UI 蓝色点的位置。
如果你按住shift键并点击每个预设,你将在下图中圈出的属性发生变化。然后您可以复制每个变量并将它们放入代码中。
这些属性由脚本通过 RectTransform.anchorMin
、RectTransform.anchorMax
和 RectTransform.pivot
控制。
不确定您问题中的 Texture2D
代码与此有何关系,但以下是设置每个预设的函数:
//------------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);
}