2

Estou desenvolvendo um jogo para android mas estou com uma dificuldade na hora de salvar o tipo de input. Por exemplo nesse jogo o usuário pode escolher se quer jogar no touch ou acelerômetro, na unity + remote funciona bem essa troca mas quando eu compilo e instalo o apk no celular não funciona. Alguém pode me ajudar?

    void Start()
    {

        if (PlayerPrefs.HasKey("playerChoice"))
        {

            if (value == 1)
            {
                inputAC.SetActive(true);
                inputTC.SetActive(false);
            }

            if (value == 0)
            {
                inputAC.SetActive(false);
                inputTC.SetActive(true);
            }
        }
    }

    void Update()
    {
        if (gameRunning)
            uiManagerScript.ShowScoreText(playerScript.score);


    }


    public void GameOver(int score)
    {
        scoreManger.AddScore(score);
        gameRunning = false;
        playerScript.gameObject.SetActive(false);
        uiManagerScript.GameEnd();
        plataformSpawnerScript.enabled = false;
        playerScript.isDead = true;
        plataform.gameObject.SetActive(false);
    }


    public void StartGame()
    {

        plataformSpawnerScript.enabled = true;
        cameraScript.ResetCam();
        uiManagerScript.GameStart();
        playerScript.score = 0;
        playerScript.isDead = false;
        gameRunning = true;
        plataform.gameObject.SetActive(true);

    }


    public void InputChoiceFalse()
    {
        if (PlayerPrefs.HasKey("playerChoice"))
        {

            PlayerPrefs.SetInt("playerChoice", playerChoice ? 0:1);

            value = 1;
            PlayerPrefs.Save();
        }

    }


    public void InputChoiceTrue()
    {
        if (PlayerPrefs.HasKey("playerChoice"))
        {

            PlayerPrefs.SetInt("playerChoice", playerChoice ? 0 : 1);

            value = 0;
            PlayerPrefs.Save();
        }


    }


    public void QuitGame()
    {
        Application.Quit();
    }

}

1 Resposta 1

1

Olá, Parece que a variável value não está com o valor do PlayerPrefs quando você faz a condição no Start(). Você verifica se existe playerChoice no arquivo salvo(PlayerPrefs), mas não carrega para o value. Acho que é o que falta no seu código. Porque o Start() é executado no frame em que o script é habilitado, o valor de value então, seria o valor previamente estabelecido, possivelmente no Inspector.

EDIT: Eu faria assim:

...
if (PlayerPrefs.HasKey("playerChoice"))
   {
   value = PlayerPrefs.GetInt("playerChoice");

       if (value == 1)
       {
...

Espero ter ajudado.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .