diff --git a/Runtime/Code/Component.meta b/Runtime/Code/Component.meta new file mode 100644 index 0000000..2d359b4 --- /dev/null +++ b/Runtime/Code/Component.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1d20da1a87484f37bc53360cc2414030 +timeCreated: 1712622721 \ No newline at end of file diff --git a/Runtime/Code/Component/IUpdater.cs b/Runtime/Code/Component/IUpdater.cs new file mode 100644 index 0000000..05a55b7 --- /dev/null +++ b/Runtime/Code/Component/IUpdater.cs @@ -0,0 +1,23 @@ +namespace Guru +{ + /// + /// 更新器 + /// + public interface IUpdater + { + UpdaterState State { get; } + void Start(); + void OnUpdate(); + void Pause(bool pause); + void Kill(); + } + + + public enum UpdaterState + { + Prepare, + Running, + Pause, + Kill, + } +} \ No newline at end of file diff --git a/Runtime/Code/Component/IUpdater.cs.meta b/Runtime/Code/Component/IUpdater.cs.meta new file mode 100644 index 0000000..6ed6042 --- /dev/null +++ b/Runtime/Code/Component/IUpdater.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ab7dd209c3a494eab0dfbedce0700d8 +timeCreated: 1712622736 \ No newline at end of file diff --git a/Runtime/Code/Component/ThreadHandler.cs b/Runtime/Code/Component/ThreadHandler.cs new file mode 100644 index 0000000..d417ca4 --- /dev/null +++ b/Runtime/Code/Component/ThreadHandler.cs @@ -0,0 +1,82 @@ + + + +namespace Guru +{ + using System; + using System.Collections; + using System.Collections.Generic; + + + public class ThreadHandler: IUpdater + { + private Queue _actions; + public Queue Actions + { + get + { + if(_actions == null) + _actions = new Queue(10); + return _actions; + } + + set + { + if (value != null) _actions = value; + } + } + + private UpdaterState _state; + public UpdaterState State => _state; + + + /// + /// 启动 Updater + /// + public void Start() + { + _state = UpdaterState.Running; + } + + /// + /// 执行方案 + /// + 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; + } + + /// + /// 删除对象 + /// + 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); + } + + } +} \ No newline at end of file diff --git a/Runtime/Code/Component/ThreadHandler.cs.meta b/Runtime/Code/Component/ThreadHandler.cs.meta new file mode 100644 index 0000000..a05c989 --- /dev/null +++ b/Runtime/Code/Component/ThreadHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dc05f1b1ff5947c19ce6af1db301398d +timeCreated: 1712624042 \ No newline at end of file diff --git a/Runtime/Code/SDK/GuruSDK.Analytics.cs b/Runtime/Code/SDK/GuruSDK.Analytics.cs index 17e6c7e..76f0644 100644 --- a/Runtime/Code/SDK/GuruSDK.Analytics.cs +++ b/Runtime/Code/SDK/GuruSDK.Analytics.cs @@ -39,7 +39,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: LogEvent {eventName} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: LogEvent {eventName} :: Please call first, before you call ."); return; } Analytics.Track(eventName, data); @@ -49,7 +49,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: SetScreen {screen} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: SetScreen {screen} :: Please call first, before you call ."); return; } Analytics.SetCurrentScreen(screen, extra); @@ -74,7 +74,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: LogLevelStart {levelId} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: LogLevelStart {levelId} :: Please call first, before you call ."); return; } @@ -116,7 +116,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: LogLevelEnd {levelId} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: LogLevelEnd {levelId} :: Please call first, before you call ."); return; } @@ -182,7 +182,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: LogLevelUp {playerLevel} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: LogLevelUp {playerLevel} :: Please call first, before you call ."); return; } Analytics.LevelUp(playerLevel, playerName); @@ -196,7 +196,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: LogAchievement {achievementName} :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: LogAchievement {achievementName} :: Please call first, before you call ."); return; } Analytics.UnlockAchievement(achievementName); @@ -213,7 +213,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: InitUserProperties :: Please call first, before you call ."); + Debug.LogError($"{Tag} :: InitUserProperties :: Please call first, before you call ."); return; } @@ -233,7 +233,7 @@ namespace Guru { if (!IsInitialSuccess) { - Debug.LogError($"{Tag} :: SetUserProperty {key}:{value} ::Please call first, before you call ."); + Debug.LogError($"{Tag} :: SetUserProperty {key}:{value} ::Please call first, before you call ."); return; } Analytics.SetUserProperty(key, value); diff --git a/Runtime/Code/SDK/GuruSDK.IAP.cs b/Runtime/Code/SDK/GuruSDK.IAP.cs index 6d479a6..a192e52 100644 --- a/Runtime/Code/SDK/GuruSDK.IAP.cs +++ b/Runtime/Code/SDK/GuruSDK.IAP.cs @@ -18,7 +18,7 @@ namespace Guru public const string BuyFail_DuplicateTransaction = "DuplicateTransaction"; public const string BuyFail_Unknown = "Unknown"; - #region Init + #region Start /// /// 初始化IAP 功能 diff --git a/Runtime/Code/SDK/GuruSDK.Threading.cs b/Runtime/Code/SDK/GuruSDK.Threading.cs new file mode 100644 index 0000000..2c5e2bb --- /dev/null +++ b/Runtime/Code/SDK/GuruSDK.Threading.cs @@ -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); + } + + + } +} \ No newline at end of file diff --git a/Runtime/Code/SDK/GuruSDK.Threading.cs.meta b/Runtime/Code/SDK/GuruSDK.Threading.cs.meta new file mode 100644 index 0000000..dbc9f0e --- /dev/null +++ b/Runtime/Code/SDK/GuruSDK.Threading.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8a0605c6b4b64b37a0036b34cbadb9ca +timeCreated: 1712622534 \ No newline at end of file diff --git a/Runtime/Code/SDK/GuruSDK.cs b/Runtime/Code/SDK/GuruSDK.cs index 1d824ff..92c77c7 100644 --- a/Runtime/Code/SDK/GuruSDK.cs +++ b/Runtime/Code/SDK/GuruSDK.cs @@ -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 _updaterRunningList; + private List _updaterRemoveList; + + private void InitUpdaters() + { + _updaterRunningList = new List(20); + _updaterRemoveList = new List(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(); + } + } + + /// + /// 注册帧更新器 + /// + /// + public static void RegisterUpdater(IUpdater updater) + { + Instance.AddUpdater(updater); + updater.Start(); + } + + + private void AddUpdater(IUpdater updater) + { + if (_updaterRunningList == null) _updaterRunningList = new List(20); + _updaterRunningList.Add(updater); + } + + private void RemoveUpdater(IUpdater updater) + { + if (_updaterRunningList != null && updater != null) + { + _updaterRunningList.Remove(updater); + } + } + + #endregion } } \ No newline at end of file diff --git a/Tests/GuruSDK.Tests.asmdef b/Tests/GuruSDK.Tests.asmdef index f440ba5..bd33202 100644 --- a/Tests/GuruSDK.Tests.asmdef +++ b/Tests/GuruSDK.Tests.asmdef @@ -4,7 +4,8 @@ "references": [ "UnityEngine.TestRunner", "UnityEditor.TestRunner", - "Guru.Runtime" + "Guru.Runtime", + "GuruSDK" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Tests/IAP.meta b/Tests/IAP.meta deleted file mode 100644 index 8aeb423..0000000 --- a/Tests/IAP.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: c3f29532318446f2aa35c275859dad0d -timeCreated: 1712580248 \ No newline at end of file diff --git a/Tests/IAP/Tests.meta b/Tests/IAP/Tests.meta deleted file mode 100644 index 8d03cc8..0000000 --- a/Tests/IAP/Tests.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 80c78f22e10d0411d85fb2a24c69cab2 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Tests/IAP/Test_IAP.cs b/Tests/Test_IAP.cs similarity index 100% rename from Tests/IAP/Test_IAP.cs rename to Tests/Test_IAP.cs diff --git a/Tests/IAP/Test_IAP.cs.meta b/Tests/Test_IAP.cs.meta similarity index 100% rename from Tests/IAP/Test_IAP.cs.meta rename to Tests/Test_IAP.cs.meta diff --git a/Tests/Test_Threading.cs b/Tests/Test_Threading.cs new file mode 100644 index 0000000..fb45df0 --- /dev/null +++ b/Tests/Test_Threading.cs @@ -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(); + } + + } +} \ No newline at end of file diff --git a/Tests/Test_Threading.cs.meta b/Tests/Test_Threading.cs.meta new file mode 100644 index 0000000..96015e8 --- /dev/null +++ b/Tests/Test_Threading.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 67d9cfd9949c48f3a8f6e6821b733ff1 +timeCreated: 1712649738 \ No newline at end of file