using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
public sealed class StreamPrefs
{
public static List<string> Keys
{
get
{
return dictionary.Keys.ToList();
}
}
private static Dictionary<string, object> dictionary = new Dictionary<string, object>();
private static string path;
public static bool Load(string path, bool reload = false)
{
if (StreamPrefs.path == path && !reload)
return false;
StreamPrefs.path = path;
if (!File.Exists(path))
return false;
try
{
byte[] bytes = File.ReadAllBytes(path);
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream(bytes))
{
dictionary = formatter.Deserialize(stream) as Dictionary<string, object>;
}
}
catch (Exception e)
{
Debug.LogException(e);
return false;
}
return true;
}
public static void DeleteAll()
{
dictionary.Clear();
}
public static void DeleteKey(string key)
{
if (!HasKey(key))
return;
dictionary.Remove(key);
}
public static bool HasKey(string key)
{
return dictionary.ContainsKey(key);
}
public static void Save()
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, dictionary);
byte[] bytes = stream.ToArray();
string directoryName = Path.GetDirectoryName(path);
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
File.WriteAllBytes(path, bytes);
}
}
public static bool GetBool(string key, bool defaultValue = false)
{
if (!HasKey(key))
return defaultValue;
return (bool)dictionary[key];
}
public static byte GetByte(string key, byte defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (byte)dictionary[key];
}
public static byte[] GetBytes(string key, byte[] defaultValue = null)
{
if (!HasKey(key))
return defaultValue;
return (byte[])dictionary[key];
}
public static char GetChar(string key, char defaultValue = '\0')
{
if (!HasKey(key))
return defaultValue;
return (char)dictionary[key];
}
public static decimal GetDecimal(string key, decimal defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (decimal)dictionary[key];
}
public static double GetDouble(string key, double defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (double)dictionary[key];
}
public static short GetInt16(string key, short defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (short)dictionary[key];
}
public static int GetInt32(string key, int defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (int)dictionary[key];
}
public static long GetInt64(string key, long defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (long)dictionary[key];
}
public static sbyte GetSByte(string key, sbyte defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (sbyte)dictionary[key];
}
public static float GetFloat(string key, float defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (float)dictionary[key];
}
public static string GetString(string key, string defaultValue = "")
{
if (!HasKey(key))
return defaultValue;
return (string)dictionary[key];
}
public static ushort GetUInt16(string key, ushort defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (ushort)dictionary[key];
}
public static uint GetUInt32(string key, uint defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (uint)dictionary[key];
}
public static ulong GetUInt64(string key, ulong defaultValue = 0)
{
if (!HasKey(key))
return defaultValue;
return (ulong)dictionary[key];
}
public static object GetObject(string key, object defaultValue = null)
{
if (!HasKey(key))
return defaultValue;
return (object)dictionary[key];
}
public static void SetBool(string key, bool value)
{
dictionary[key] = value;
}
public static void SetByte(string key, byte value)
{
dictionary[key] = value;
}
public static void SetBytes(string key, byte[] value)
{
dictionary[key] = value;
}
public static void SetChar(string key, char value)
{
dictionary[key] = value;
}
public static void SetDecimal(string key, decimal value)
{
dictionary[key] = value;
}
public static void SetDouble(string key, double value)
{
dictionary[key] = value;
}
public static void SetInt16(string key, short value)
{
dictionary[key] = value;
}
public static void SetInt32(string key, int value)
{
dictionary[key] = value;
}
public static void SetInt64(string key, long value)
{
dictionary[key] = value;
}
public static void SetSByte(string key, sbyte value)
{
dictionary[key] = value;
}
public static void SetFloat(string key, float value)
{
dictionary[key] = value;
}
public static void SetString(string key, string value)
{
dictionary[key] = value;
}
public static void SetUInt16(string key, ushort value)
{
dictionary[key] = value;
}
public static void SetUInt32(string key, uint value)
{
dictionary[key] = value;
}
public static void SetUInt64(string key, ulong value)
{
dictionary[key] = value;
}
public static void SetObject(string key, object value)
{
dictionary[key] = value;
}
}
自定义类取代PlayerPrefs
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- RACDisposable算是三个核心组件里面最简单的了, 除了自身外, 还有4个子类:RACKVOTrampol...