196 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			196 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C#
		
	
	
 | 
						|
namespace Guru
 | 
						|
{
 | 
						|
    using System;
 | 
						|
    using UnityEngine;
 | 
						|
    using System.IO;
 | 
						|
    
 | 
						|
    
 | 
						|
    [Serializable]
 | 
						|
    internal class GuruSDKModel
 | 
						|
    {
 | 
						|
        private const float SaveInterval = 3;
 | 
						|
        private const string SaveKey = "com.guru.sdk.model.save";
 | 
						|
        
 | 
						|
        
 | 
						|
        private static GuruSDKModel _instance;
 | 
						|
        public static GuruSDKModel Instance
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                if (null == _instance) _instance = Load();
 | 
						|
                return _instance;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        //-------------- data ---------------
 | 
						|
        public int b_level = 0;
 | 
						|
        public int b_play = 0;
 | 
						|
        public int buy_count = 0;
 | 
						|
        //-------------- data ---------------
 | 
						|
 | 
						|
        private float _lastSavedTime = 0;
 | 
						|
        
 | 
						|
        public int SuccessLevelCount
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                if(_successLevel == null) InitProperties();
 | 
						|
                return _successLevel.Value;
 | 
						|
            }
 | 
						|
            set => _successLevel.Value = value;
 | 
						|
        }
 | 
						|
 | 
						|
        public int TotalPlayedCount
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                if(_totalPlayed == null) InitProperties();
 | 
						|
                return _totalPlayed.Value;
 | 
						|
            }
 | 
						|
            set => _totalPlayed.Value = value;
 | 
						|
        }
 | 
						|
 | 
						|
        public int PurchasedCount
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                if(_purchasedCount == null) InitProperties();
 | 
						|
                return _purchasedCount.Value;
 | 
						|
            }
 | 
						|
            set => _purchasedCount.Value = value;
 | 
						|
        }
 | 
						|
 | 
						|
        public bool IsIAPUser => PurchasedCount > 0;
 | 
						|
 | 
						|
        
 | 
						|
        private BindableProperty<int> _successLevel;
 | 
						|
        private BindableProperty<int> _totalPlayed;
 | 
						|
        private BindableProperty<int> _purchasedCount;
 | 
						|
        
 | 
						|
        public BindableProperty<int> PropBLevel => _successLevel;
 | 
						|
        public BindableProperty<int> PropBPlay => _totalPlayed;
 | 
						|
        public BindableProperty<int> PropBuyCount => _purchasedCount;
 | 
						|
 | 
						|
 | 
						|
        #region 初始化
 | 
						|
 | 
						|
        
 | 
						|
        public static GuruSDKModel Load()
 | 
						|
        {
 | 
						|
            GuruSDKModel model = null;
 | 
						|
            if (PlayerPrefs.HasKey(SaveKey))
 | 
						|
            {
 | 
						|
                var json = PlayerPrefs.GetString(SaveKey, "");
 | 
						|
                if (!string.IsNullOrEmpty(json))
 | 
						|
                {
 | 
						|
                    try
 | 
						|
                    {
 | 
						|
                        model =  JsonUtility.FromJson<GuruSDKModel>(json);
 | 
						|
                    }
 | 
						|
                    catch (Exception e)
 | 
						|
                    {
 | 
						|
                        Debug.LogError(e);
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
            if(model == null) model = new GuruSDKModel();
 | 
						|
            model.InitProperties();
 | 
						|
            return model;
 | 
						|
        }
 | 
						|
        
 | 
						|
        /// <summary>
 | 
						|
        /// 保存至数据
 | 
						|
        /// </summary>
 | 
						|
        private void SaveToPlayerPrefs()
 | 
						|
        {
 | 
						|
            var json = JsonUtility.ToJson(this);
 | 
						|
            PlayerPrefs.SetString(SaveKey, json);
 | 
						|
        }
 | 
						|
 | 
						|
        public void InitProperties()
 | 
						|
        {
 | 
						|
            _successLevel = new BindableProperty<int>(b_level, OnLevelChanged);
 | 
						|
            _totalPlayed = new BindableProperty<int>(b_play, OnPlayedChanged);
 | 
						|
            _purchasedCount = new BindableProperty<int>(buy_count, OnPurchasedNumChanged);
 | 
						|
        }
 | 
						|
        
 | 
						|
        /// <summary>
 | 
						|
        /// 保存数据
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="force"></param>
 | 
						|
        public void Save(bool force = false)
 | 
						|
        {
 | 
						|
            bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= SaveInterval);
 | 
						|
            if (save)
 | 
						|
            {
 | 
						|
                _lastSavedTime = Time.realtimeSinceStartup;
 | 
						|
                SaveToPlayerPrefs();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 数据绑定变化
 | 
						|
        private void OnLevelChanged(int value)
 | 
						|
        {
 | 
						|
            b_level = value;
 | 
						|
            Save();
 | 
						|
        }
 | 
						|
        private void OnPlayedChanged(int value)
 | 
						|
        {
 | 
						|
            b_play = value;
 | 
						|
            Save();
 | 
						|
        }
 | 
						|
 | 
						|
        private void OnPurchasedNumChanged(int value)
 | 
						|
        {
 | 
						|
            buy_count = value;
 | 
						|
            Save();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 启动配置
 | 
						|
        
 | 
						|
        /// <summary>
 | 
						|
        /// 从 Streaming 加载 AppServices 配置
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public string LoadAppServicesConfigJson()
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                string path = Path.Combine(Application.streamingAssetsPath, $"{GuruSDK.ServicesConfigKey}.{GuruSDK.ServicesConfigExtension}");
 | 
						|
                return File.ReadAllText(path);
 | 
						|
            }
 | 
						|
            catch (Exception e)
 | 
						|
            {
 | 
						|
                Log.Exception(e);
 | 
						|
            }
 | 
						|
            return "";
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
        
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        
 | 
						|
 | 
						|
 | 
						|
       
 | 
						|
 | 
						|
 | 
						|
    }
 | 
						|
    
 | 
						|
    
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
} |