CacheDataController

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
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值