RectTransform.anchoredPosition

Unity · Версия 2020.3 LTS · Scripting · RectTransform

RectTransform.anchoredPosition

public Vector2 anchoredPosition;

Описание

Положение центра этого RectTransform относительно контрольной точки привязки.

Якорная позиция — это позиция опорной точки RectTransform с учетом опорной точки привязки. Контрольной точкой привязки является положение привязок. Если точки привязки не совпадают, Unity оценивает положение четырех точек привязки, используя в качестве эталона положение оси.

Примечание. Инспектор меняет отображаемые свойства в зависимости от используемой предустановки привязки. Дополнительную информацию см. в разделах Прямое преобразование и Базовый макет.

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 GUISlider
        if (GUI.changed)
        {
            //Change the RectTransform's anchored positions depending on the Slider values
            m_RectTransform.anchoredPosition = new Vector2(m_XAxis, m_YAxis);
        }
    }
}

Обновлено 17 ноября 2026 г.