PlayerPrefsでListやDictionaryを保存する

PlayerPrefsでListやDictionaryの保存【Unity】
PlayerPrefsでListやDictionaryの保存【Unity】. GitHub Gist: instantly share code, notes, and snippets.
// PlayerPrefsUtility.cs
// http://kan-kikuchi.hatenablog.com/entry/PlayerPrefsUtility
//
// Created by kan kikuchi on 2015.10.22.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
/// <summary>
/// PlayerPrefsに関する便利クラス
/// </summary>
public static class PlayerPrefsUtility {
//=================================================================================
//保存
//=================================================================================
/// <summary>
/// リストを保存
/// </summary>
public static void SaveList<T>(string key , List<T> value){
string serizlizedList = Serialize<List<T>> (value);
PlayerPrefs.SetString (key, serizlizedList);
}
/// <summary>
/// ディクショナリーを保存
/// </summary>
public static void SaveDict<Key, Value>(string key , Dictionary<Key, Value> value){
string serizlizedDict = Serialize<Dictionary<Key, Value>> (value);
PlayerPrefs.SetString (key, serizlizedDict);
}
//=================================================================================
//読み込み
//=================================================================================
/// <summary>
/// リストを読み込み
/// </summary>
public static List<T> LoadList<T> (string key){
//keyがある時だけ読み込む
if (PlayerPrefs.HasKey (key)) {
string serizlizedList = PlayerPrefs.GetString (key);
return Deserialize<List<T>> (serizlizedList);
}
return new List<T> ();
}
/// <summary>
/// ディクショナリーを読み込み
/// </summary>
public static Dictionary<Key, Value> LoadDict<Key, Value> (string key){
//keyがある時だけ読み込む
if (PlayerPrefs.HasKey (key)) {
string serizlizedDict = PlayerPrefs.GetString (key);
return Deserialize<Dictionary<Key, Value>> (serizlizedDict);
}
return new Dictionary<Key, Value> ();
}
//=================================================================================
//シリアライズ、デシリアライズ
//=================================================================================
private static string Serialize<T> (T obj){
BinaryFormatter binaryFormatter = new BinaryFormatter ();
MemoryStream memoryStream = new MemoryStream ();
binaryFormatter.Serialize (memoryStream , obj);
return Convert.ToBase64String (memoryStream .GetBuffer ());
}
private static T Deserialize<T> (string str){
BinaryFormatter binaryFormatter = new BinaryFormatter ();
MemoryStream memoryStream = new MemoryStream (Convert.FromBase64String (str));
return (T)binaryFormatter.Deserialize (memoryStream);
}
}
クエスト(ミッション)を実装
https://note.com/azumagoro/n/n1e915ab38ead 参考サイト
https://note.com/azumagoro/n/nbdc7d4b77482 (最後のソースは有料。購入しましたが、これだけでクエスト機能が動くという全ソースはないので注意)
こちらにはUnity Excel Import Makerを使うとありますが、個人的にはhttps://qiita.com/mikito/items/2ad911f69180c15102a1 Unity Excel Importerの方が使いやすいと思います。
かんたんな手書きエフェクトを実装
https://matcha-choco010.net/2019/01/24/unity-handdrawn-effect-timeline/ 参考サイト




コメント