Save last position of scrollrect or rect transform

1k views Asked by At

I have Level scrollrect but i like when i go to select a level i like to show me always last level(if i finish level 13 unlock me level 14 and when a go to menu scene to show me level 14 in center after this if i finish level 14 when i go again in menu scene to show me level 15 in center) is posible to make this and to save last position of last level. When i open menu Scene it show me from botton see - Picture 1 but i like when i open menu scene to open from this position see - Picture 2.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


public class LevelData
{
    public LevelData(string levelName)
    {
        string data = PlayerPrefs.GetString(levelName);
        if (data == "")
            return;

        string[] allData = data.Split('&');
        BestTime = float.Parse(allData[0]);
        SilverTime = float.Parse(allData[1]);
        GoldTime = float.Parse(allData[2]);
    }

    public float BestTime { set; get; }
    public float GoldTime { set; get; }
    public float SilverTime { set; get; }
    public float BronzeTime { set; get; }
}

public class Menu : MonoBehaviour
{
    private static Menu instance;
    public static Menu Instance { get { return instance; } }
    private int idLevel;
    public Text timeText;
    public Text txtselectLevel;
    public string[] Level;
    public ScrollRect scrol;

    public GameObject PlayPanel;
    public GameObject star1;
    public GameObject star2;
    public GameObject star3;

    void Start()
    {
        instance = this;
        PlayPanel.SetActive(false);
        GameManager.Instance.Load();
        idLevel = 0;
        txtselectLevel.text = Level[idLevel];
        PlayerPrefs.GetString(idLevel.ToString());

        star1.SetActive(false);
        star2.SetActive(false);
        star3.SetActive(false);

    }

    public void Scenes()
    {
        SceneManager.LoadScene("L" + idLevel.ToString());
    }

    public void SelectLevel(int i)
    {
        idLevel = i;

        PlayerPrefs.SetInt("idLevel", idLevel);
        txtselectLevel.text = Level[idLevel];

        LevelData level = new LevelData(i.ToString());

        string minutes = ((int)level.BestTime / 60).ToString("00");
        string seconds = (level.BestTime % 60).ToString("00.00");

        timeText.text = (level.BestTime != 0.0f) 
            ? "Best Time: " + minutes + ":" + seconds 
            : "";

        if (level.BestTime < level.GoldTime)
        {
            star1.SetActive(true);
            star2.SetActive(true);
            star3.SetActive(true);
        }
        else if (level.BestTime < level.SilverTime)
        {
            star1.SetActive(true);
            star2.SetActive(true);
            star3.SetActive(false);
        }
        else if(level.BestTime < level.BronzeTime)
        {
            star1.SetActive(true);
            star2.SetActive(false);
            star3.SetActive(false);
        }
        else
        {
            star1.SetActive(false);
            star2.SetActive(false);
            star3.SetActive(false);
        }

        PlayPanel.SetActive(true);
    }

    public void ExitPanel()
    {
        PlayPanel.SetActive(false);
    }
}

0

There are 0 answers