【Unity演習の解答】PlayerPrefsを使ってデータの保存・取り出しを行う

演習問題の解答ページです。演習を行った後に確認をお願いします。

演習解答

問題1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefsPractice : MonoBehaviour
{
    public int SaveTest;
    void Start()
    {
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            if(PlayerPrefs.HasKey("save_test"))
            {
                Debug.Log(PlayerPrefs.GetInt("save_test", 0));
            }
            else
            {
                Debug.Log("save_testに値は保存されていません");
            }
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
        }
    }
}

問題2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefsPractice : MonoBehaviour
{
    public int SaveTest;
    void Start()
    {
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            if(PlayerPrefs.HasKey("save_test"))
            {
                Debug.Log(PlayerPrefs.GetInt("save_test", -1));
            }
            else
            {
                Debug.Log("save_testに値は保存されていません");
            }
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            SaveTest = PlayerPrefs.GetInt("save_test", -1);
            SaveTest += 1;
            PlayerPrefs.SetInt("save_test", SaveTest);
            PlayerPrefs.Save();
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
        }
    }
}

問題3

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefsPractice : MonoBehaviour
{
    public int SaveTest;
    void Start()
    {
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            if(PlayerPrefs.HasKey("save_test"))
            {
                Debug.Log(PlayerPrefs.GetInt("save_test", 0));
            }
            else
            {
                Debug.Log("save_testに値は保存されていません");
            }
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            SaveTest = PlayerPrefs.GetInt("save_test", 0);
            SaveTest += 1;
            PlayerPrefs.SetInt("save_test", SaveTest);
            PlayerPrefs.Save();
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (PlayerPrefs.HasKey("save_test"))
            {
                PlayerPrefs.DeleteKey("save_test");
            }
        }
    }
}