PlayerPrefs可以將字串、浮點數和整數儲存到使用者的登錄檔中,但未加密並不推薦儲存重要的數據。
建立兩個按鈕分別在Inspector的OnClick裡放入存檔和讀檔功能來觸發
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveLoad : MonoBehaviour
{
[SerializeField]
PlayerData Data;
[System.Serializable]
public class PlayerData
{
public string name;
public float HP;
public int Lvl;
}
public void Save()
{
playcontrol player = GetComponent<playcontrol>();
int HP = player.hp;
PlayerPrefs.SetString("name", Data.name);
PlayerPrefs.SetFloat("HP", HP);
PlayerPrefs.SetInt("Lvl", Data.Lvl);
PlayerPrefs.Save();
}
public void Load()
{
Data.name = PlayerPrefs.GetString("name");
Data.HP = PlayerPrefs.GetFloat("HP");
Data.Lvl = PlayerPrefs.GetInt("Lvl");
}
}
待更
參考資料:
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
https://www.youtube.com/watch?v=mKhetIXa7KA&t
https://dev.twsiyuan.com/2018/06/how-to-save-and-load-gamesaves-in-unity.html