Menu
CoddyTech

Guardar el viento

Parte de la sección Arquitectura de juegos en Unity del Journey de Unity de Coddy. Lección 26 de 64.

Una compilación carga todos los recursos tal como se compilaron cada vez que se inicia, así que el viento del jugador volvería a estar en 2. PlayerPrefs lo conserva y Start lo vuelve a escribir en el recurso.

challenge icon

Desafío

Fácil

Conserva el viento del jugador para la siguiente partida.

  1. En WindVane, cada vez que una flecha cambie el viento, llama a PlayerPrefs.SetFloat("Wind", wind.strength);.
  2. En Start, antes de Show(), si PlayerPrefs.HasKey("Wind"), establece wind.strength en PlayerPrefs.GetFloat("Wind"). Un jugador nuevo conserva el 2 del recurso.

Pulsa «Jugar», cambia el viento, pulsa «Detener» y luego «Jugar»: el viento es el mismo. Después, pulsa «Comprobar».

Pruébalo tú mismo

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 las lecciones de Arquitectura de juegos en Unity

Practica por tu cuenta: Playground de Unity