![見出し画像](https://assets.st-note.com/production/uploads/images/123267350/rectangle_large_type_2_6f3b90eb18aa3eeaa0ea23f24be25808.jpeg?width=1200)
データ保存関連
ScriptableObjectについて
unity専用のデータベース
クラス定義には、「: ScriptableObject」
Attributeを使い、「Assets」の「Create」メニューに、「ScriptableObject」クラス専用のオブジェクトを作成するメニューを追加できる
「ScriptableObject」には、「List」配列が有効
「JsonUtility」を使って、「シリアライズ化」「デシリアライズ化」しておけば、あとは、「フォルダ」「ファイル」を指定してファイルの読み書きするだけ
using System.Collections.Generic;
using UnityEngine;
using System;
[CreateAssetMenu(fileName = "PlayerDataScriptable",menuName = "d/PlayerData1")]
public class PlayerDataScriptable : ScriptableObject
{
public string playerName;
public int hp;
public List<SceneParam> sceneParams;
[Serializable]
public class SceneParam
{
public string sceneName;
public bool isClear;
}
public void Save()
{
var data = JsonUtility.ToJson(this, true);
Debug.Log(data);
PlayerPrefs.SetString("PlayerData", data);
}
public void Load()
{
var data = PlayerPrefs.GetString("PlayerData");
Debug.Log(data);
JsonUtility.FromJsonOverwrite(data, this);
}
}
サポートエリアでのサポート、ありがとうございます