update: 更新 Inventory 数据模块
							parent
							
								
									784f9da3a0
								
							
						
					
					
						commit
						6eb2b9da3a
					
				|  | @ -0,0 +1,117 @@ | |||
| 
 | ||||
| using UnityEngine.Serialization; | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     using System; | ||||
|     using UnityEngine; | ||||
|     using System.Collections.Generic; | ||||
| 
 | ||||
|      | ||||
|     [Serializable] | ||||
|     public class GuruInventoryModel: GuruModelBase | ||||
|     { | ||||
|         public Dictionary<string, UserItemInfo> data; | ||||
|         public Dictionary<string, UserItemInfo> Data => data ??= new Dictionary<string, UserItemInfo>(20); | ||||
|          | ||||
|         public bool RegisterItem(string itemName, int initCount = 0, string type = "") | ||||
|         { | ||||
|             if (!HasItem(itemName)) | ||||
|             { | ||||
|                 Data[itemName] = new UserItemInfo() | ||||
|                 { | ||||
|                     key = itemName, | ||||
|                     free_amount = initCount, | ||||
|                     paid_amount = 0, | ||||
|                     type = type | ||||
|                 }; | ||||
|                 return true; | ||||
|             } | ||||
|             return false; | ||||
|         } | ||||
|          | ||||
|          | ||||
|         public bool HasItem(string itemName) | ||||
|         { | ||||
|             return Data.ContainsKey(itemName); | ||||
|         } | ||||
| 
 | ||||
|         public UserItemInfo GetItemInfo(string itemName) | ||||
|         { | ||||
|             if (Data.TryGetValue(itemName, out var item)) | ||||
|             { | ||||
|                 return item; | ||||
|             } | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     [Serializable] | ||||
|     public class UserItemInfo | ||||
|     { | ||||
|         public bool enableZeroProtection = true; // 默认开启 0 值保护 | ||||
|         public string key; | ||||
|         public int free_amount; | ||||
|         public int paid_amount; | ||||
|         public string type; | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 道具总量 | ||||
|         /// </summary> | ||||
|         public int TotalAmount => free_amount + paid_amount; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 增减道具量 | ||||
|         /// </summary> | ||||
|         /// <param name="amount"></param> | ||||
|         /// <param name="isPaid"></param> | ||||
|         public bool Change(int amount, bool isPaid = false) | ||||
|         { | ||||
|             if (amount == 0) return false; | ||||
|              | ||||
|             if (isPaid) | ||||
|             { | ||||
|                 if (amount < 0 && enableZeroProtection && paid_amount + amount < 0) | ||||
|                     return false; | ||||
|                  | ||||
|                 paid_amount += amount; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 if (amount < 0 && enableZeroProtection && free_amount + amount < 0) | ||||
|                     return false; | ||||
|                  | ||||
|                 free_amount += amount; | ||||
|             } | ||||
| 
 | ||||
|             return true; | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 强制设置数量 | ||||
|         /// </summary> | ||||
|         /// <param name="freeAmount"></param> | ||||
|         /// <param name="paidAmount"></param> | ||||
|         public void Set(int freeAmount, int paidAmount = 0) | ||||
|         { | ||||
|             paid_amount = paidAmount; | ||||
|             free_amount = freeAmount; | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 数值清零 | ||||
|         /// </summary> | ||||
|         public void Clean() | ||||
|         { | ||||
|             paid_amount = 0; | ||||
|             free_amount = 0; | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 4d8be99a0fca428da9c6e70bc9cbea36 | ||||
| timeCreated: 1706787422 | ||||
|  | @ -0,0 +1,74 @@ | |||
| 
 | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
| 
 | ||||
|     using UnityEngine; | ||||
| 
 | ||||
|      | ||||
|     public abstract class GuruModelBase | ||||
|     { | ||||
| 
 | ||||
| 
 | ||||
|         #region DataIO | ||||
| 
 | ||||
|         private double _lastSavedTime = 0; | ||||
|          | ||||
|         protected double _saveInterval = 3; | ||||
|         protected string DefaultSaveKey => GetType().Name; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 加载数据 IO 接口 | ||||
|         /// </summary> | ||||
|         /// <param name="saveKey"></param> | ||||
|         /// <typeparam name="T"></typeparam> | ||||
|         /// <returns></returns> | ||||
|         internal static T LoadIO<T>(string saveKey = "") where T : GuruModelBase, new() | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(saveKey)) saveKey = typeof(T).Name; | ||||
|             if (PlayerPrefs.HasKey(saveKey)) | ||||
|             { | ||||
|                 var json = PlayerPrefs.GetString(saveKey, ""); | ||||
|                 if (!string.IsNullOrEmpty(json)) | ||||
|                 { | ||||
|                     // return JsonUtility.FromJson<T>(json); | ||||
|                     return JsonParser.ToObject<T>(json); | ||||
|                 } | ||||
|             } | ||||
|             return new T(); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 保存数据 IO 接口 | ||||
|         /// </summary> | ||||
|         /// <param name="saveKey"></param> | ||||
|         protected void SaveIO(string saveKey = "") | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(saveKey)) saveKey = DefaultSaveKey; | ||||
|             var json = JsonParser.ToJson(this); | ||||
|             PlayerPrefs.SetString(saveKey, json); | ||||
|             PlayerPrefs.Save(); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 保存数据 | ||||
|         /// </summary> | ||||
|         /// <param name="force"></param> | ||||
|         public virtual void Save(bool force = false) | ||||
|         { | ||||
|             bool save = force || (Time.realtimeSinceStartup - _lastSavedTime>= _saveInterval); | ||||
|             if (save) | ||||
|             { | ||||
|                 _lastSavedTime = Time.realtimeSinceStartup; | ||||
|                 SaveIO(); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         #endregion | ||||
|          | ||||
|          | ||||
|          | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: c99bee149c95459f93ccf3fe587788a0 | ||||
| timeCreated: 1706787492 | ||||
|  | @ -10,9 +10,8 @@ namespace Guru | |||
|      | ||||
|      | ||||
|     [Serializable] | ||||
|     internal class GuruSDKModel | ||||
|     internal class GuruSDKModel: GuruModelBase | ||||
|     { | ||||
|         private const float SaveInterval = 3; | ||||
|         private const string SaveKey = "com.guru.sdk.model.save"; | ||||
|          | ||||
|          | ||||
|  | @ -21,7 +20,7 @@ namespace Guru | |||
|         { | ||||
|             get | ||||
|             { | ||||
|                 if (null == _instance) _instance = Load(); | ||||
|                 if (null == _instance) _instance = Init(); | ||||
|                 return _instance; | ||||
|             } | ||||
|         } | ||||
|  | @ -35,7 +34,7 @@ namespace Guru | |||
|         public List<PurchasedProduct> purchased; | ||||
|         //-------------- data --------------- | ||||
| 
 | ||||
|         private float _lastSavedTime = 0; | ||||
|          | ||||
|          | ||||
|         public int SuccessLevelCount | ||||
|         { | ||||
|  | @ -108,39 +107,13 @@ namespace Guru | |||
| 
 | ||||
|         #region 初始化 | ||||
| 
 | ||||
|          | ||||
|         public static GuruSDKModel Load() | ||||
|         private static GuruSDKModel Init() | ||||
|         { | ||||
|             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(); | ||||
|             GuruSDKModel model = LoadIO<GuruSDKModel>(SaveKey); | ||||
|             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); | ||||
|  | @ -150,20 +123,6 @@ namespace Guru | |||
| 
 | ||||
|             purchased = new List<PurchasedProduct>(20); | ||||
|         } | ||||
|          | ||||
|         /// <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 | ||||
|  |  | |||
|  | @ -262,6 +262,21 @@ namespace Guru | |||
|         } | ||||
|          | ||||
|          | ||||
|         #endregion | ||||
| 
 | ||||
|         #region 经济打点 | ||||
| 
 | ||||
|         public static void EarnVirtualCurrency() | ||||
|         { | ||||
|              | ||||
|         } | ||||
| 
 | ||||
|         public static void SpendVirtualCurrency() | ||||
|         { | ||||
|              | ||||
|         } | ||||
|          | ||||
|          | ||||
|         #endregion | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue