update: 更新线程控制
							parent
							
								
									ef3a11c382
								
							
						
					
					
						commit
						baae2a6eb4
					
				|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 1d20da1a87484f37bc53360cc2414030 | ||||
| timeCreated: 1712622721 | ||||
|  | @ -0,0 +1,23 @@ | |||
| namespace Guru | ||||
| { | ||||
|     /// <summary> | ||||
|     /// 更新器 | ||||
|     /// </summary> | ||||
|     public interface IUpdater | ||||
|     { | ||||
|         UpdaterState State { get; } | ||||
|         void Start(); | ||||
|         void OnUpdate(); | ||||
|         void Pause(bool pause); | ||||
|         void Kill(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public enum UpdaterState | ||||
|     { | ||||
|         Prepare, | ||||
|         Running, | ||||
|         Pause, | ||||
|         Kill, | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 6ab7dd209c3a494eab0dfbedce0700d8 | ||||
| timeCreated: 1712622736 | ||||
|  | @ -0,0 +1,82 @@ | |||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     using System; | ||||
|     using System.Collections; | ||||
|     using System.Collections.Generic; | ||||
| 
 | ||||
|      | ||||
|     public class ThreadHandler: IUpdater | ||||
|     { | ||||
|         private Queue<Action> _actions; | ||||
|         public Queue<Action> Actions | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 if(_actions == null)  | ||||
|                     _actions = new Queue<Action>(10); | ||||
|                 return _actions; | ||||
|             } | ||||
| 
 | ||||
|             set | ||||
|             { | ||||
|                 if (value != null) _actions = value; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         private UpdaterState _state; | ||||
|         public UpdaterState State => _state; | ||||
| 
 | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 启动 Updater | ||||
|         /// </summary> | ||||
|         public void Start() | ||||
|         { | ||||
|             _state = UpdaterState.Running; | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 执行方案 | ||||
|         /// </summary> | ||||
|         public void OnUpdate() | ||||
|         { | ||||
|             if (Actions.Count > 0) | ||||
|             { | ||||
|                 // 消耗对垒 | ||||
|                 while (Actions.Count > 0) | ||||
|                 { | ||||
|                     Actions.Dequeue()?.Invoke(); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|          | ||||
|         public void Pause(bool pause = true) | ||||
|         { | ||||
|             _state = pause ? UpdaterState.Pause : UpdaterState.Running; | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 删除对象 | ||||
|         /// </summary> | ||||
|         public void Kill() | ||||
|         { | ||||
|             _state = UpdaterState.Kill; | ||||
|         } | ||||
| 
 | ||||
|         public void Dispose() | ||||
|         { | ||||
|             _actions.Clear(); | ||||
|             _state = UpdaterState.Kill; | ||||
|         } | ||||
|          | ||||
|         public void AddAction(Action action) | ||||
|         { | ||||
|             if (action == null) return; | ||||
|             Actions.Enqueue(action); | ||||
|         } | ||||
|          | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: dc05f1b1ff5947c19ce6af1db301398d | ||||
| timeCreated: 1712624042 | ||||
|  | @ -39,7 +39,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: LogEvent {eventName} :: Please call <GuruSDK.Init()> first, before you call <LogEvent>."); | ||||
|                 Debug.LogError($"{Tag} :: LogEvent {eventName} :: Please call <GuruSDK.Start()> first, before you call <LogEvent>."); | ||||
|                 return; | ||||
|             } | ||||
|             Analytics.Track(eventName, data); | ||||
|  | @ -49,7 +49,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: SetScreen {screen} :: Please call <GuruSDK.Init()> first, before you call <SetScreen>."); | ||||
|                 Debug.LogError($"{Tag} :: SetScreen {screen} :: Please call <GuruSDK.Start()> first, before you call <SetScreen>."); | ||||
|                 return; | ||||
|             } | ||||
|             Analytics.SetCurrentScreen(screen, extra); | ||||
|  | @ -74,7 +74,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: LogLevelStart {levelId} :: Please call <GuruSDK.Init()> first, before you call <LogLevelStart>."); | ||||
|                 Debug.LogError($"{Tag} :: LogLevelStart {levelId} :: Please call <GuruSDK.Start()> first, before you call <LogLevelStart>."); | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|  | @ -116,7 +116,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: LogLevelEnd {levelId} :: Please call <GuruSDK.Init()> first, before you call <LogLevelEnd>."); | ||||
|                 Debug.LogError($"{Tag} :: LogLevelEnd {levelId} :: Please call <GuruSDK.Start()> first, before you call <LogLevelEnd>."); | ||||
|                 return; | ||||
|             } | ||||
|              | ||||
|  | @ -182,7 +182,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: LogLevelUp {playerLevel} :: Please call <GuruSDK.Init()> first, before you call <LogLevelUp>."); | ||||
|                 Debug.LogError($"{Tag} :: LogLevelUp {playerLevel} :: Please call <GuruSDK.Start()> first, before you call <LogLevelUp>."); | ||||
|                 return; | ||||
|             } | ||||
|             Analytics.LevelUp(playerLevel, playerName); | ||||
|  | @ -196,7 +196,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: LogAchievement {achievementName} :: Please call <GuruSDK.Init()> first, before you call <LogAchievement>."); | ||||
|                 Debug.LogError($"{Tag} :: LogAchievement {achievementName} :: Please call <GuruSDK.Start()> first, before you call <LogAchievement>."); | ||||
|                 return; | ||||
|             } | ||||
|             Analytics.UnlockAchievement(achievementName); | ||||
|  | @ -213,7 +213,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: InitUserProperties :: Please call <GuruSDK.Init()> first, before you call <InitUserProperties>."); | ||||
|                 Debug.LogError($"{Tag} :: InitUserProperties :: Please call <GuruSDK.Start()> first, before you call <InitUserProperties>."); | ||||
|                 return; | ||||
|             } | ||||
|              | ||||
|  | @ -233,7 +233,7 @@ namespace Guru | |||
|         { | ||||
|             if (!IsInitialSuccess) | ||||
|             { | ||||
|                 Debug.LogError($"{Tag} :: SetUserProperty {key}:{value} ::Please call <GuruSDK.Init()> first, before you call <SetUserProperty>."); | ||||
|                 Debug.LogError($"{Tag} :: SetUserProperty {key}:{value} ::Please call <GuruSDK.Start()> first, before you call <SetUserProperty>."); | ||||
|                 return; | ||||
|             } | ||||
|             Analytics.SetUserProperty(key, value); | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ namespace Guru | |||
|         public const string BuyFail_DuplicateTransaction = "DuplicateTransaction"; | ||||
|         public const string BuyFail_Unknown = "Unknown"; | ||||
|          | ||||
|         #region Init | ||||
|         #region Start | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 初始化IAP 功能 | ||||
|  |  | |||
|  | @ -0,0 +1,29 @@ | |||
| namespace Guru | ||||
| { | ||||
|     using System; | ||||
|     using UnityEngine; | ||||
|      | ||||
|     public partial class GuruSDK | ||||
|     { | ||||
|         private ThreadHandler _threadHandler; | ||||
| 
 | ||||
|         private void InitThreadHandler() | ||||
|         { | ||||
|             _threadHandler = new ThreadHandler(); | ||||
|             RegisterUpdater(_threadHandler); | ||||
|         } | ||||
| 
 | ||||
|         private void AddActionToMainThread(Action action) | ||||
|         { | ||||
|             _threadHandler?.AddAction(action); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         public static void RunOnMainThread(Action action) | ||||
|         { | ||||
|            Instance.AddActionToMainThread(action); | ||||
|         } | ||||
|          | ||||
|          | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 8a0605c6b4b64b37a0036b34cbadb9ca | ||||
| timeCreated: 1712622534 | ||||
|  | @ -131,11 +131,13 @@ namespace Guru | |||
|             _initConfig = config; | ||||
|             _onCompleteCallback = onComplete; | ||||
|             _isDebugEnabled = config.DebugMode; | ||||
| 
 | ||||
|             InitThreadHandler(); // 初始化线程处理器 | ||||
|         } | ||||
| 
 | ||||
|         void Start() | ||||
|         { | ||||
|             //---- Init All tools ---- | ||||
|             //---- Start All tools ---- | ||||
|             LogI($"#2 --- InitFirebase ---"); | ||||
|             //---------- Start Firebase ------------ | ||||
|             FirebaseUtil.onInitComplete += OnFirebaseReady; | ||||
|  | @ -217,7 +219,15 @@ namespace Guru | |||
|             ABTestManager.Init(); // 启动AB测试解析器 | ||||
|             Callbacks.Remote._onRemoteFetchComplete?.Invoke(success); | ||||
|         } | ||||
|          | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         private void Update() | ||||
|         { | ||||
|             UpdateAllUpdates(); // 驱动所有的更新器 | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         #endregion | ||||
| 
 | ||||
|         #region App Remote Update | ||||
|  | @ -248,7 +258,7 @@ namespace Guru | |||
|                  | ||||
|                 Try(() => | ||||
|                 { | ||||
|                     LogI($"#4.1 --- Init apply services ---"); | ||||
|                     LogI($"#4.1 --- Start apply services ---"); | ||||
|                     //---------------------------------------------------------------- | ||||
| 
 | ||||
|                     // 自打点设置错误上报 | ||||
|  | @ -265,7 +275,7 @@ namespace Guru | |||
|                         GuruSettings.UpdateAdjustEvents(_appServicesConfig.adjust_settings.events); | ||||
|                     } | ||||
|                  | ||||
|                     LogI($"#4.2 --- Init GuruSttings ---"); | ||||
|                     LogI($"#4.2 --- Start GuruSttings ---"); | ||||
|                     // GuruSettings 设置 | ||||
|                     if (null != _appServicesConfig.app_settings) | ||||
|                     { | ||||
|  | @ -313,7 +323,7 @@ namespace Guru | |||
|                 // InitIAP(_initConfig.GoogleKeys, _initConfig.AppleRootCerts); // 初始化IAP | ||||
|                 Try(() => | ||||
|                 { | ||||
|                     LogI($"#4.3 --- Init IAP ---"); | ||||
|                     LogI($"#4.3 --- Start IAP ---"); | ||||
|                     InitIAP(_initConfig.GoogleKeys, _initConfig.AppleRootCerts); // 初始化IAP | ||||
|                 }, ex => | ||||
|                 { | ||||
|  | @ -326,7 +336,7 @@ namespace Guru | |||
|                 // KeywordsManager.Install(Model.IsIAPUser, Model.SuccessLevelId); // 启动Keyword管理器 | ||||
|                 Try(() => | ||||
|                 { | ||||
|                     LogI($"#4.4 --- Init Keywords ---"); | ||||
|                     LogI($"#4.4 --- Start Keywords ---"); | ||||
|                     KeywordsManager.Install(Model.IsIAPUser, Model.SuccessLevelId); // 启动Keyword管理器 | ||||
|                 }, ex => | ||||
|                 { | ||||
|  | @ -596,6 +606,89 @@ namespace Guru | |||
|         } | ||||
|          | ||||
|         #endregion | ||||
| 
 | ||||
|         #region 帧更新 Updater | ||||
| 
 | ||||
|          | ||||
|         private List<IUpdater> _updaterRunningList; | ||||
|         private List<IUpdater> _updaterRemoveList; | ||||
| 
 | ||||
|         private void InitUpdaters() | ||||
|         { | ||||
|             _updaterRunningList = new List<IUpdater>(20); | ||||
|             _updaterRemoveList = new List<IUpdater>(20); | ||||
|         } | ||||
| 
 | ||||
|         private void UpdateAllUpdates() | ||||
|         { | ||||
|             int i = 0; | ||||
|             IUpdater updater; | ||||
|             // ---- Updater Trigger ---- | ||||
|             if (_updaterRunningList.Count > 0) | ||||
|             { | ||||
|                 i = 0; | ||||
|                 while (i < _updaterRunningList.Count) | ||||
|                 { | ||||
|                     updater = _updaterRunningList[i]; | ||||
|                     if (updater != null)  | ||||
|                     { | ||||
|                         if (updater.State == UpdaterState.Running) | ||||
|                         { | ||||
|                             updater.OnUpdate(); | ||||
|                         } | ||||
|                         else if(updater.State == UpdaterState.Kill) | ||||
|                         { | ||||
|                             _updaterRemoveList.Add(updater); | ||||
|                         } | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         _updaterRunningList.RemoveAt(i); | ||||
|                         i--; | ||||
|                     } | ||||
|                     i++; | ||||
|                 } | ||||
| 
 | ||||
|             } | ||||
| 
 | ||||
|             if (_updaterRemoveList.Count > 0) | ||||
|             { | ||||
|                 i = 0; | ||||
|                 while (i < _updaterRemoveList.Count) | ||||
|                 { | ||||
|                     RemoveUpdater(_updaterRemoveList[i]); | ||||
|                     i++; | ||||
|                 } | ||||
|                 _updaterRemoveList.Clear(); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 注册帧更新器 | ||||
|         /// </summary> | ||||
|         /// <param name="updater"></param> | ||||
|         public static void RegisterUpdater(IUpdater updater) | ||||
|         { | ||||
|             Instance.AddUpdater(updater); | ||||
|             updater.Start(); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         private void AddUpdater(IUpdater updater) | ||||
|         { | ||||
|             if (_updaterRunningList == null) _updaterRunningList = new List<IUpdater>(20); | ||||
|             _updaterRunningList.Add(updater); | ||||
|         } | ||||
| 
 | ||||
|         private void RemoveUpdater(IUpdater updater) | ||||
|         { | ||||
|             if (_updaterRunningList != null && updater != null) | ||||
|             { | ||||
|                 _updaterRunningList.Remove(updater); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         #endregion | ||||
|          | ||||
|     } | ||||
| } | ||||
|  | @ -4,7 +4,8 @@ | |||
|     "references": [ | ||||
|         "UnityEngine.TestRunner", | ||||
|         "UnityEditor.TestRunner", | ||||
|         "Guru.Runtime" | ||||
|         "Guru.Runtime", | ||||
|         "GuruSDK" | ||||
|     ], | ||||
|     "includePlatforms": [], | ||||
|     "excludePlatforms": [], | ||||
|  |  | |||
|  | @ -1,3 +0,0 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: c3f29532318446f2aa35c275859dad0d | ||||
| timeCreated: 1712580248 | ||||
|  | @ -1,8 +0,0 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 80c78f22e10d0411d85fb2a24c69cab2 | ||||
| folderAsset: yes | ||||
| DefaultImporter: | ||||
|   externalObjects: {} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
|  | @ -0,0 +1,49 @@ | |||
| 
 | ||||
| 
 | ||||
| namespace Guru.Tests | ||||
| { | ||||
|     using NUnit.Framework; | ||||
|     using System.Threading; | ||||
|     using UnityEngine; | ||||
|      | ||||
|     public class Test_Threading | ||||
|     { | ||||
| 
 | ||||
|         private int TestCount | ||||
|         { | ||||
|             get => PlayerPrefs.GetInt(nameof(TestCount), 0); | ||||
|             set => PlayerPrefs.SetInt(nameof(TestCount), value); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         [Test] | ||||
|         public void Test_ThreadingCall() | ||||
|         { | ||||
|             GuruSDK.Init(success => | ||||
|             { | ||||
|                 GuruSDK.Delay(0.1f, () => | ||||
|                 { | ||||
|                     CallThreading(); | ||||
|                 }); | ||||
|             }); | ||||
|         } | ||||
| 
 | ||||
|         private void CallThreading() | ||||
|         { | ||||
|             Debug.Log($"--------- CallThreading -------------"); | ||||
|             var t = new Thread(() => | ||||
|             { | ||||
|                 Debug.Log($"--------- Thread Start -------------"); | ||||
|                 Thread.Sleep(2000); | ||||
|                 GuruSDK.RunOnMainThread(() => | ||||
|                 { | ||||
|                     TestCount++; | ||||
|                     Debug.Log($">>>>> CallThreading: {TestCount}"); | ||||
|                 }); | ||||
|             }); | ||||
|              | ||||
|             t.Start(); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 67d9cfd9949c48f3a8f6e6821b733ff1 | ||||
| timeCreated: 1712649738 | ||||
		Loading…
	
		Reference in New Issue