CacheDataController
来源:互联网 发布:百度云盘mac破解补丁 编辑:程序博客网 时间:2024/06/05 06:46
using UnityEngine;using System.Collections;using System.Collections.Generic;public class CacheDataController{ private static CacheDataController instance; private Dictionary<string, string> datas; public static CacheDataController Instance { get { if (instance == null) { instance = new CacheDataController(); } return CacheDataController.instance; } } public bool HasCacheKey(ECacheDataType type) {string key = type.ToString (); if(PlayerPrefs.HasKey(key)) { return true; } else { return false; } } public void SetCache<T>(ECacheDataType type, T t) {string key = type.ToString (); if(typeof(T)==typeof(int)) { PlayerPrefs.SetInt(key, int.Parse(t.ToString())); } else if (typeof(T) == typeof(string)) { PlayerPrefs.SetString(key, t.ToString()); } else if (typeof(T) == typeof(float)) { PlayerPrefs.SetFloat(key, float.Parse(t.ToString())); } else if (typeof(T) == typeof(bool)) { PlayerPrefs.SetString(key, t.ToString()); } }public T GetCache<T>(ECacheDataType type) {string key = type.ToString (); T t = default(T); if (!HasCacheKey(type)) { } else { if (typeof(T) == typeof(int)) { t = (T)System.Convert.ChangeType(PlayerPrefs.GetInt(key), typeof(T)); } else if (typeof(T) == typeof(string)) { t = (T)System.Convert.ChangeType(PlayerPrefs.GetString(key), typeof(T)); } else if (typeof(T) == typeof(float)) { t = (T)System.Convert.ChangeType(PlayerPrefs.GetFloat(key), typeof(T)); } else if (typeof(T) == typeof(bool)) { t = (T)System.Convert.ChangeType(PlayerPrefs.GetString(key), typeof(T)); } } return t; } public void DeleteCache(ECacheDataType type) {string key = type.ToString (); PlayerPrefs.DeleteKey(key); } public void DeleteAllCache() { PlayerPrefs.DeleteAll(); }}public enum ECacheDataType{HadConnectBLEAddress}