Menu
CoddyTech

Salve o Vento

Parte da seção Arquitetura de Jogos no Unity do Journey de Unity da Coddy. Lição 26 de 64.

Uma build carrega todos os assets exatamente como foram criados cada vez que inicia, então o vento do jogador voltaria para 2. PlayerPrefs mantém esse valor, e Start o grava de volta no asset.

challenge icon

Desafio

Fácil

Mantenha o vento do jogador para a próxima partida.

  1. Em WindVane, sempre que uma seta alterar o vento, chame PlayerPrefs.SetFloat("Wind", wind.strength);.
  2. Em Start, antes de Show(), se PlayerPrefs.HasKey("Wind"), defina wind.strength como PlayerPrefs.GetFloat("Wind"). Um novo jogador mantém o valor 2 do recurso.

Pressione “Jogar”, altere o vento, Parar e “Jogar”: o vento continua o mesmo. Depois, pressione “Verificar”.

Experimente você mesmo

using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class WindVane : MonoBehaviour
{
    public TMP_Text windText;
    public WindSettings wind;

    void Start()
    {
        Show();
    }

    void Update()
    {
        if (Keyboard.current.upArrowKey.wasPressedThisFrame)
        {
            wind.strength = Mathf.Min(wind.strength + 1f, 5f);
            Show();
        }
        if (Keyboard.current.downArrowKey.wasPressedThisFrame)
        {
            wind.strength = Mathf.Max(wind.strength - 1f, 0f);
            Show();
        }
    }

    void Show()
    {
        windText.text = "Wind: " + wind.strength;
    }
}

Todas as lições de Arquitetura de Jogos no Unity

Pratique por conta própria: Playground de Unity