Menu
CoddyTech

바람 저장하기

Coddy Unity 여정의 Unity 게임 아키텍처 섹션에 포함된 레슨. 64개 중 26번째.

빌드는 시작할 때마다 모든 에셋을 빌드된 상태 그대로 불러오므로, 플레이어의 바람은 다시 2가 됩니다. PlayerPrefs는 바람을 저장하고, Start는 그 값을 에셋에 다시 기록합니다.

challenge icon

챌린지

쉬움

다음 게임에서도 플레이어의 바람 설정을 유지하세요.

  1. WindVane에서 화살표를 눌러 바람을 변경할 때마다 PlayerPrefs.SetFloat("Wind", wind.strength);를 호출하세요.
  2. Start에서 Show()를 호출하기 전에 PlayerPrefs.HasKey("Wind")가 참이면 wind.strength를 PlayerPrefs.GetFloat("Wind")로 설정하세요. 새 플레이어는 에셋의 2를 유지합니다.

‘플레이’를 누르고 바람을 변경한 다음, 중지를 누르고 다시 ‘플레이’를 누르세요. 바람 설정이 그대로 유지됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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;
    }
}

Unity 게임 아키텍처의 모든 레슨

직접 연습해 보세요: Unity 플레이그라운드