Zmiana pozycji tekstu RectTransform w Unity za pomocą slidera

Przykład przedstawia sytuację kiedy za pomocą slidera zmienia się pozycję obiektu UI. Funkcja SetSlider() jest wywoływana na sliderze przy każdorazowej zmianie wartości.
W komponencie RectTransform należy dostosować Anchors.

using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class NapisComponent : MonoBehaviour
{

    [SerializeField] Slider slid;
    [SerializeField] TextMeshProUGUI tekst1;

    public void SetSliders()
    {
        tekst1.GetComponent<RectTransform>().anchoredPosition = new Vector2(slid.value, tekst1.GetComponent<RectTransform>().anchoredPosition.y);
    }
}

Zmiana rozmiaru RectTransform danego obiektu na y=70, zachowując dotychczasową szerokość

this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(GetComponent<RectTransform>().sizeDelta.x, 70);

Zmiana skali tekstu czy innego elementu UI

this.gameObject.GetComponent<RectTransform>().localScale = new Vector2(slid, slid);
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Scroll to Top