Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelSelector : MonoBehaviour
{
    public Button[] levelButtons;

    public ScrollRect scrollRect;
    public RectTransform contentPanel;

    public void SnapTo(Button mButton)
    {
        Canvas.ForceUpdateCanvases();


        contentPanel.anchoredPosition = new Vector2(contentPanel.anchoredPosition.x,
                ((Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)
                - (Vector2)scrollRect.transform.InverseTransformPoint(mButton.transform.position)).y);

    }

    void Start()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", 1);

        for (int i = 0; i < levelButtons.Length; i++)
        {
            if (i + 1 > levelReached)
                levelButtons[i].interactable = false;
        }

        if (levelReached <= 3)
        {
            return;
        }

        SnapTo(levelButtons[levelReached-3]);
    }
}