Version: 2021.1

RectTransform.anchoredPosition

切换到手册
public Vector2 anchoredPosition ;

描述

此 RectTransform 的轴心相对于锚点参考点的位置。

锚定位置是考虑锚点参考点的 RectTransform 轴心位置。 锚点参考点是锚点的位置。如果锚点不在一起,则 Unity 会使用轴心位置作为参考来估算四个锚点位置。

注意:Inspector 基于在使用哪个锚点预设对公开哪些属性进行更改。有关更多信息,请参阅矩形变换基本布局

using UnityEngine;

public class Example : MonoBehaviour { RectTransform m_RectTransform; float m_XAxis, m_YAxis;

void Start() { //Fetch the RectTransform from the GameObject m_RectTransform = GetComponent<RectTransform>(); //Initiate the x and y positions m_XAxis = 0.5f; m_YAxis = 0.5f; }

void OnGUI() { //The Labels show what the Sliders represent GUI.Label(new Rect(0, 20, 150, 80), "Anchor Position X : "); GUI.Label(new Rect(300, 20, 150, 80), "Anchor Position Y : ");

//Create a horizontal Slider that controls the x and y Positions of the anchors m_XAxis = GUI.HorizontalSlider(new Rect(150, 20, 100, 80), m_XAxis, -50.0f, 50.0f); m_YAxis = GUI.HorizontalSlider(new Rect(450, 20, 100, 80), m_YAxis, -50.0f, 50.0f);

//Detect a change in the GUI Slider if (GUI.changed) { //Change the RectTransform's anchored positions depending on the Slider values m_RectTransform.anchoredPosition = new Vector2(m_XAxis, m_YAxis); } } }