简体   繁体   中英

Unity: Change Slider Value save after scene change

I created a settings page for my app where you can toggle a Slider On or Off. If you toggle the first one on, the bool isMale is set to true. If you toggle it off, the bool isMale is set to false. I'm using this bool on another page where I get the bool from this page. If isMale is true I'm gonna show a male model and opposite of isMale is false. That part is working. The problem is when I change back to the scene, all the bools are reset. Any ideas?

The code:

using UnityEngine;
using UnityEngine.UI;

public class SettingsScript : MonoBehaviour {

    // Mikael Paaske
    public static bool isMale;
    public static bool showTutorial;
    public static bool openInApp;

    public GameObject canvas;
    public GameObject showSexText;
    public GameObject ønskeskyenText;
    public GameObject tutorialText;

    public Slider changeSexSlider;
    public Image backgroundchangeSexSlider;

    public Slider changeWebSiteSlider;
    public Image backggroundchangeWebSiteSlider;

    public Slider changeTutorial;
    public Image backgroundchangeTutorial;

    public void Start()
    {
        // Finder alle de nødvendige gameobjekter i scenen 
        canvas = GameObject.Find("SettingsCanvas");

        showSexText = canvas.transform.Find("ShowSexText").gameObject;
        ønskeskyenText = canvas.transform.Find("ShowØnskeskyenText").gameObject;
        tutorialText = canvas.transform.Find("ShowToturialText").gameObject;

        changeSexSlider = GameObject.Find("ChangeSexSlider").GetComponent<Slider>();
        backgroundchangeSexSlider = GameObject.FindGameObjectWithTag("background").GetComponent<Image>();

        changeWebSiteSlider = GameObject.Find("ØnskeskyenSlider").GetComponent<Slider>();
        backggroundchangeWebSiteSlider = GameObject.FindGameObjectWithTag("backgground2").GetComponent<Image>();

        changeTutorial = GameObject.Find("TutorialSlider").GetComponent<Slider>();
        backgroundchangeTutorial = GameObject.FindGameObjectWithTag("backgground3").GetComponent<Image>();

    }

    public void Update()
    {
        // Ændrer farve på gameobjektet alt efter hvilken tekst der står ovenover
        if (showSexText.GetComponent<Text>().text == "Kvinde")
        {
            backgroundchangeSexSlider.color = Color.red;
        }


        if (tutorialText.GetComponent<Text>().text == "Vis ikke")
        {
            backgroundchangeTutorial.color = Color.red;
        }


        if (ønskeskyenText.GetComponent<Text>().text == "Hjemmeside")
        {
            backggroundchangeWebSiteSlider.color = Color.red;
        }
    }
    public void ChangeSex()
    {
        // Hvis slider bliver rykket til den ene ende, bliver boolen sat til det modsatte af før
        isMale = !isMale;
        Debug.Log(isMale);
        // Ændrer gameobjektets tekst til det modsatte efter slideren er rykket
        if (isMale == true)
        {
            showSexText.GetComponent<Text>().text = "Mand";
        }
        else if (isMale == false)
        {
            showSexText.GetComponent<Text>().text = "Kvinde";      
        }
    }

    public void ShowToturial()
    {
        // Hvis slider bliver rykket til den ene ende, bliver boolen sat til det modsatte af før
        showTutorial = !showTutorial;

        // Ændrer gameobjektets tekst ud fra boolen er sand eller falsk
        if (showTutorial == true)
        {
            tutorialText.GetComponent<Text>().text = "Vis";
        }
        else if (showTutorial == false)
        {
            tutorialText.GetComponent<Text>().text = "Vis ikke";
        }  
    }
    public void WishList()
    {
        // Hvis slider bliver rykket til den ene ende, bliver boolen sat til det modsatte af før
        openInApp = !openInApp;

        // Ændrer gameobjektets tekst ud fra boolen er sand eller falsk
        if (openInApp == true)
        {
            ønskeskyenText.GetComponent<Text>().text = "App";
        }
        else if (openInApp == false)
        {
            ønskeskyenText.GetComponent<Text>().text = "Hjemmeside";
        }
    }
}

You'll want to utilize Object.DontDestroyOnLoad . This will keep your object and script alive between scenes. You'll also have to take care that there is not more than one object of this type on every scene load, through the use of the Singleton Pattern .

Another way to access information would be to save them to the drive through PlayerPrefs . You could save these on leaving the scene or whenever the value is changed with C# Properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM