Skip to content

Instantly share code, notes, and snippets.

@yKimisaki
Last active March 16, 2016 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yKimisaki/8620e832b46986fb58e8 to your computer and use it in GitHub Desktop.
Save yKimisaki/8620e832b46986fb58e8 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using Tonari.Text;
using UnityEngine;
namespace Tonari.UnityEngine
{
public abstract class TypedPlayerPrefs<TKey, TValue>
{
// TODO: JsonUtilityが配列に対応したら直す
private static readonly string _keysKeySurfix = "<Keys>";
private static readonly char _dividor = '^'; // JSONと変数名で使わなさそうな文字
private readonly string _tableName;
private readonly string _keysKey;
private readonly IEqualityComparer<TKey> _keyComparer;
private List<TKey> _keys;
protected TypedPlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer)
{
_tableName = tableName;
_keysKey = tableName + _keysKeySurfix;
if (PlayerPrefs.HasKey(_keysKey))
{
_keys = new List<TKey>(PlayerPrefs.GetString(_keysKey)
.Split(_dividor)
.Select(x => JsonUtility.FromJson<TKey>(x)));
}
else
{
_keys = new List<TKey>();
}
_keyComparer = keyComparer;
}
public void SaveAll(IEnumerable<KeyValuePair<TKey, TValue>> data)
{
foreach (var datum in data)
{
SaveCore(datum.Key, datum.Value);
}
PlayerPrefs.SetString(_keysKey, GetSerializedKeysValue());
PlayerPrefs.Save();
}
public void Save(TKey key, TValue value)
{
SaveCore(key, value);
PlayerPrefs.SetString(_keysKey, GetSerializedKeysValue());
PlayerPrefs.Save();
}
private void SaveCore(TKey key, TValue value)
{
var fixedKey = _tableName + key;
SetValueCore(fixedKey, value);
_keys.Add(key);
}
public IDictionary<TKey, TValue> LoadAll()
{
return _keys.ToDictionary(x => x, x => LoadOrDefault(x));
}
public TValue LoadOrDefault(TKey key)
{
return LoadOrDefault(key, default(TValue));
}
public TValue LoadOrDefault(TKey key, TValue defaultValue)
{
var fixedKey = _tableName + key;
if (PlayerPrefs.HasKey(fixedKey))
{
return GetValueCore(fixedKey);
}
return defaultValue;
}
protected abstract void SetValueCore(string fixedKey, TValue value);
protected abstract TValue GetValueCore(string fixedKey);
// JsonUtilityが配列に対応していないから自力でシリアライズする
private string GetSerializedKeysValue()
{
_keys = new List<TKey>(_keyComparer != null ? _keys.Distinct(_keyComparer) : _keys.Distinct());
var sb = new StringBuilder(1024);
for (var i = 0; i < _keys.Count; ++i)
{
sb.Append(JsonUtility.ToJson(_keys[i]));
if (i < (_keys.Count - 1))
{
sb.Append(_dividor.ToString());
}
}
return sb.ToString();
}
}
public class BoolValuePlayerPrefs<TKey> : TypedPlayerPrefs<TKey, bool>
{
public BoolValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public BoolValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
protected override sealed bool GetValueCore(string fixedKey)
{
return PlayerPrefs.GetInt(fixedKey) == 1;
}
protected override sealed void SetValueCore(string fixedKey, bool value)
{
PlayerPrefs.SetInt(fixedKey, value ? 1 : 0);
}
}
public class IntValuePlayerPrefs<TKey> : TypedPlayerPrefs<TKey, int>
{
public IntValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public IntValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
protected override sealed int GetValueCore(string fixedKey)
{
return PlayerPrefs.GetInt(fixedKey);
}
protected override sealed void SetValueCore(string fixedKey, int value)
{
PlayerPrefs.SetInt(fixedKey, value);
}
}
public class FloatValuePlayerPrefs<TKey> : TypedPlayerPrefs<TKey, float>
{
public FloatValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public FloatValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
protected override sealed float GetValueCore(string fixedKey)
{
return PlayerPrefs.GetFloat(fixedKey);
}
protected override sealed void SetValueCore(string fixedKey, float value)
{
PlayerPrefs.SetFloat(fixedKey, value);
}
}
public class StringValuePlayerPrefs<TKey> : TypedPlayerPrefs<TKey, string>
{
public StringValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public StringValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
protected override sealed string GetValueCore(string fixedKey)
{
return PlayerPrefs.GetString(fixedKey);
}
protected override sealed void SetValueCore(string fixedKey, string value)
{
PlayerPrefs.SetString(fixedKey, value);
}
}
public class SerializableValuePlayerPrefs<TKey, TValue> : TypedPlayerPrefs<TKey, TValue>
{
public SerializableValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public SerializableValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
protected override sealed TValue GetValueCore(string fixedKey)
{
return JsonUtility.FromJson<TValue>(PlayerPrefs.GetString(fixedKey));
}
protected override sealed void SetValueCore(string fixedKey, TValue value)
{
PlayerPrefs.SetString(fixedKey, JsonUtility.ToJson(value));
}
}
public class Vector2ValuePlayerPrefs<TKey> : SerializableValuePlayerPrefs<TKey, Vector2>
{
public Vector2ValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public Vector2ValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
}
public class Vector3ValuePlayerPrefs<TKey> : SerializableValuePlayerPrefs<TKey, Vector3>
{
public Vector3ValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public Vector3ValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
}
public class ColorValuePlayerPrefs<TKey> : SerializableValuePlayerPrefs<TKey, Color>
{
public ColorValuePlayerPrefs(string tableName) : base(tableName, null)
{
}
public ColorValuePlayerPrefs(string tableName, IEqualityComparer<TKey> keyComparer) : base(tableName, keyComparer)
{
}
}
}
@yKimisaki
Copy link
Author

var _playerPrefs = new BoolValuePlayerPrefs<int>("SomeTableName");

_playerPrefs.Save(765, true);
_playerPrefs.Save(346, false);

_playerPrefs.LoadOrDefault(765); // true
_playerPrefs.LoadOrDefault(573); // false

@yKimisaki
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment