diff --git a/UnityProject/Assets/Dependencies/JEngine/Core/Manager/LifeCycleMgr.cs b/UnityProject/Assets/Dependencies/JEngine/Core/Manager/LifeCycleMgr.cs index d6ef9487..2c17d465 100644 --- a/UnityProject/Assets/Dependencies/JEngine/Core/Manager/LifeCycleMgr.cs +++ b/UnityProject/Assets/Dependencies/JEngine/Core/Manager/LifeCycleMgr.cs @@ -163,6 +163,7 @@ private void Awake() /// private void OnDestroy() { + ExecuteItems(_onDestroyTaskItems); UnsafeUtility.Free(ItemList, Allocator.Persistent); UnsafeUtility.Free(UsageList, Allocator.Persistent); GC.RemoveMemoryPressure(sizeof(LifeCycleItem) * MaxSize); @@ -204,6 +205,11 @@ private void OnDestroy() /// private readonly List _onceTaskItems = new List(100); + /// + /// All on destory task methods + /// + private readonly List _onDestroyTaskItems = new List(100); + /// /// no gc search for awake objs /// @@ -287,7 +293,8 @@ public void AddUpdateItem(object instance, MethodInfo method) /// /// /// - public void AddUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) where T : class + public void AddUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) + where T : class { void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address); _updateItems.Add(GetLifeCycleItem(in ptr, in address, @@ -362,11 +369,12 @@ public void AddLateUpdateItem(object instance, MethodInfo method) /// /// /// - public void AddLateUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) where T : class + public void AddLateUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) + where T : class { void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address); _lateUpdateItems.Add(GetLifeCycleItem(in ptr, in address, - () => method?.Invoke(instance, ConstMgr.NullObjects), + () => method?.Invoke(instance, ConstMgr.NullObjects), () => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke())); } @@ -408,11 +416,12 @@ public void AddFixedUpdateItem(object instance, MethodInfo method) /// /// /// - public void AddFixedUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) where T : class + public void AddFixedUpdateItem(T instance, MethodInfo method, GameObject parent, Func cond = null) + where T : class { void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address); _fixedUpdateItems.Add(GetLifeCycleItem(in ptr, in address, - () => method?.Invoke(instance, ConstMgr.NullObjects), + () => method?.Invoke(instance, ConstMgr.NullObjects), () => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke())); } @@ -465,7 +474,8 @@ public Guid AddTask(Action action, Func condition) /// /// /// - public void AddTask(T instance, Action action) => AddTask(action, () => true); + public void AddTask(T instance, Action action) where T : class + => AddTask(instance, action, () => true); /// /// Add a task that will call once in the main thread when condition is true @@ -480,6 +490,55 @@ public void AddTask(T instance, Action action, Func condition) where T _onceTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition)); } + /// + /// Add a task that will call once in the main thread when application is quitting + /// + /// + /// + public Guid AddOnDestroyTask(Action action) => AddOnDestroyTask(action, () => true); + + /// + /// Add a task that will call once in the main thread when condition is true when application is quitting + /// + /// + /// + /// + public Guid AddOnDestroyTask(Action action, Func condition) + { + Guid guid = Guid.NewGuid(); + var guidIdent = new IntPtr(guid.GetHashCode()); + while (_onDestroyTaskItems.Exists(i => ((LifeCycleItem*)i)->InstancePtr == guidIdent)) + { + guid = Guid.NewGuid(); + guidIdent = new IntPtr(guid.GetHashCode()); + } + + _onDestroyTaskItems.Add(GetLifeCycleItem((void*)guidIdent, 0, action, condition)); + return guid; + } + + /// + /// Add a task that will call once in the main thread when application is quitting + /// + /// + /// + /// + public void AddOnDestroyTask(T instance, Action action) where T : class + => AddOnDestroyTask(instance, action, () => true); + + /// + /// Add a task that will call once in the main thread when condition is true when application is quitting + /// + /// + /// + /// + /// + public void AddOnDestroyTask(T instance, Action action, Func condition) where T : class + { + void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address); + _onDestroyTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition)); + } + /// /// Remove a task that will call once in the main thread /// @@ -717,7 +776,7 @@ private void LateUpdate() _instances.Add(item->InstancePtr); } } - + //调用start,并记录本帧处理的对象 ExecuteItems(_startItems, true, InstancesContains, iterate: il => _startObjs.Remove(il.InstancePtr)); @@ -731,7 +790,7 @@ private void LateUpdate() item = (LifeCycleItem*)_startItems[i]; _instances.Add(item->InstancePtr); } - + ExecuteItems(_startItems, iterate: il => _startObjs.Remove(il.InstancePtr)); } } diff --git a/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/ChangeScene.cs b/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/ChangeScene.cs index 4ff05069..97c7a99e 100644 --- a/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/ChangeScene.cs +++ b/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/ChangeScene.cs @@ -28,7 +28,6 @@ private static async void DoChange() var op = SceneManager.LoadSceneAsync(name); while (SceneManager.GetActiveScene().path != path) { - if (!Application.isPlaying) return; EditorUtility.DisplayProgressBar("JEngine", Setting.GetString(SettingString.JumpToStartUpScene), op.progress); await Task.Delay(100); } @@ -36,10 +35,10 @@ private static async void DoChange() DynamicGI.UpdateEnvironment(); } - var comp = Object.FindFirstObjectByType(); + var comp = Object.FindObjectOfType(); if (comp == null) { - Debug.LogWarning("没有找到InitJEngine脚本,无法检验秘钥是否正确"); + // Debug.LogWarning("没有找到InitJEngine脚本,无法检验秘钥是否正确"); return; } var key = comp.key; diff --git a/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/SetData.cs b/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/SetData.cs index d8418c9b..fee61bfe 100644 --- a/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/SetData.cs +++ b/UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/EditorUpdates/SetData.cs @@ -11,7 +11,7 @@ internal static class SetData { public static bool HasAdded; private static string _path = "JEngine.proj"; - private static JEngineProjData _data = new JEngineProjData(); + private static JEngineProjData _data; public static void UpdateData(Action func) { @@ -23,54 +23,59 @@ public static void UpdateData(Action func) public static string GetPrefix() { - //看看文件存不存在,不存在就创建和提示 - string fPath = Path.Combine(Application.dataPath, _path); - if (!File.Exists(fPath)) + if (_data == null) { _data = new JEngineProjData(); - //兼容老版本 - bool flag = false; - if (File.Exists(Path.Combine(Application.dataPath, "JEngine.lock"))) + //看看文件存不存在,不存在就创建和提示 + string fPath = Path.Combine(Application.dataPath, _path); + if (!File.Exists(fPath)) { - _data.Prefix = File.ReadAllText(Path.Combine(Application.dataPath, "JEngine.lock")); - _data.EncryptPassword = PlayerPrefs.GetString($"{_data.Prefix}.EncryptPassword", ""); - File.Delete(Path.Combine(Application.dataPath, "JEngine.lock")); - } - else - { - _data.Prefix = Guid.NewGuid().ToString(); - flag = true; - } - Span data = stackalloc byte[_data.Size()]; - _data.AsBinary(ref data); - File.WriteAllBytes(fPath, data.ToArray()); - if (flag) - { - //提示看文档 - Debug.LogError(Setting.GetString(SettingString.NoticeText)); - EditorUtility.DisplayDialog(Setting.GetString(SettingString.Notice), - Setting.GetString(SettingString.NoticeText), Setting.GetString(SettingString.Done)); - if (Setting.Language == JEngineLanguage.English) + //兼容老版本 + bool flag = false; + if (File.Exists(Path.Combine(Application.dataPath, "JEngine.lock"))) { - Application.OpenURL("https://docs.xgamedev.net/documents/0.8/"); + _data.Prefix = File.ReadAllText(Path.Combine(Application.dataPath, "JEngine.lock")); + _data.EncryptPassword = PlayerPrefs.GetString($"{_data.Prefix}.EncryptPassword", ""); + File.Delete(Path.Combine(Application.dataPath, "JEngine.lock")); } else { - Application.OpenURL("https://docs.xgamedev.net/zh/documents/0.8/"); + _data.Prefix = Guid.NewGuid().ToString(); + flag = true; + } + + Span data = stackalloc byte[_data.Size()]; + _data.AsBinary(ref data); + File.WriteAllBytes(fPath, data.ToArray()); + if (flag) + { + //提示看文档 + Debug.LogError(Setting.GetString(SettingString.NoticeText)); + EditorUtility.DisplayDialog(Setting.GetString(SettingString.Notice), + Setting.GetString(SettingString.NoticeText), Setting.GetString(SettingString.Done)); + if (Setting.Language == JEngineLanguage.English) + { + Application.OpenURL("https://docs.xgamedev.net/documents/0.8/"); + } + else + { + Application.OpenURL("https://docs.xgamedev.net/zh/documents/0.8/"); + } } + + InjectDefineSymbol(); + } + else + { + //读取文件 + Span data = File.ReadAllBytes(fPath); + _data.FromBinary(ref data); } - InjectDefineSymbol(); - } - else - { - //读取文件 - Span data = File.ReadAllBytes(fPath); - _data.FromBinary(ref data); } return _data.Prefix; } - + public static void Update() { string prefix = GetPrefix(); diff --git a/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.dll b/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.dll index 59b2ac10..d78a0da0 100644 Binary files a/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.dll and b/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.dll differ diff --git a/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.pdb b/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.pdb index ea73cdeb..4d57bd18 100644 Binary files a/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.pdb and b/UnityProject/Assets/HotUpdateResources/Main/Dll/Hidden~/HotUpdateScripts.pdb differ diff --git a/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.bytes b/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.bytes index fea81cbe..9a50c794 100644 Binary files a/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.bytes and b/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.bytes differ diff --git a/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.pdb.bytes b/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.pdb.bytes index ea73cdeb..4d57bd18 100644 Binary files a/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.pdb.bytes and b/UnityProject/Assets/HotUpdateResources/Main/Dll/HotUpdateScripts.pdb.bytes differ diff --git a/UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs b/UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs index 5173eb86..953896e7 100644 --- a/UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs +++ b/UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs @@ -76,6 +76,16 @@ static JBehaviour() { CoroutineMgr.Instance.StartCoroutine(JBehavioursLoop()); }); + LifeCycleMgr.Instance.AddOnDestroyTask(() => + { + foreach (var jb in JBehaviours.Values.ToList()) + { + if (!jb._hidden && !jb._paused) + { + jb.Destroy(); + } + } + }); } /// @@ -86,7 +96,6 @@ private static IEnumerator JBehavioursLoop() Stopwatch sw = new Stopwatch(); for (;;) { - if (!Application.isPlaying) break; yield return ConstMgr.WaitFor1Sec; int cnt = LoopJBehaviours.Count; for (int i = 0; i < cnt; i++) @@ -575,10 +584,7 @@ private void Destroy() JBehaviours.Remove(_instanceID); GameObjectJBehaviours.Remove(_gameObject); _gameObject = null; - if (Application.isPlaying) - { - End(); - } + End(); }