Skip to content

Commit b05e496

Browse files
Merge pull request #462 from JasonXuDeveloper/development
fix async lifecycle bug
2 parents b053836 + 646f42d commit b05e496

File tree

13 files changed

+56
-38
lines changed

13 files changed

+56
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333

34-
# JENGINE v0.8.0
34+
# JENGINE v0.8.0f1
3535

3636
**JEngine is an out-of-the-box framework designed for Unity developers. It encapsulates powerful functions. Beginners can also get started quickly and easily create games that can be updated in runtime.**
3737

README_zh_cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232

33-
# JENGINE v0.8.0
33+
# JENGINE v0.8.0f1
3434

3535
JEngine是针对Unity开发者设计的**开箱即用**的框架,封装了强大的功能,小白也能**快速上手****轻松制作**可以**热更新的游戏**
3636

UnityProject/Assets/Dependencies/JEngine/Core/Manager/ClassBindMgr.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ namespace JEngine.Core
3232
{
3333
public partial class ClassBindMgr : MonoBehaviour
3434
{
35-
public static void Instantiate()
35+
public static async Task Instantiate()
3636
{
3737
if (_instance != null)
3838
return;
3939

4040
_instance = new GameObject("ClassBindMgr").AddComponent<ClassBindMgr>();
4141
DontDestroyOnLoad(_instance);
42+
SceneManager.sceneLoaded += _instance.OnSceneLoaded;
43+
SceneManager.sceneUnloaded += _instance.OnSceneUnloaded;
44+
LoadedScenes.Add(SceneManager.GetActiveScene());
45+
await DoBind();
4246
}
4347

4448
private static ClassBindMgr _instance;
@@ -51,11 +55,6 @@ private void Awake()
5155
{
5256
DestroyImmediate(this);
5357
}
54-
55-
SceneManager.sceneLoaded += OnSceneLoaded;
56-
SceneManager.sceneUnloaded += OnSceneUnloaded;
57-
LoadedScenes.Add(SceneManager.GetActiveScene());
58-
_ = DoBind();
5958
}
6059

6160
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
@@ -73,11 +72,11 @@ private void OnDestroy()
7372
{
7473
if (_instance == this)
7574
{
76-
_instance = null;
77-
SceneManager.sceneLoaded -= OnSceneLoaded;
78-
SceneManager.sceneUnloaded -= OnSceneUnloaded;
75+
SceneManager.sceneLoaded -= _instance.OnSceneLoaded;
76+
SceneManager.sceneUnloaded -= _instance.OnSceneUnloaded;
7977
LoadedScenes.Clear();
8078
Cbs.Clear();
79+
_instance = null;
8180
}
8281
}
8382

@@ -121,7 +120,7 @@ public static async Task DoBind(List<ClassBind> cbs)
121120
continue;
122121
}
123122

124-
cb.Active(data);
123+
await cb.Active(data);
125124
}
126125
}
127126
}

UnityProject/Assets/Dependencies/JEngine/Core/Util/ClassBind.cs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using ILRuntime.CLR.Utils;
77
using ILRuntime.Reflection;
88
using System.Threading.Tasks;
9+
using JEngine.Core.DO_NOT_USE;
910
using ILRuntime.CLR.TypeSystem;
1011
using UnityEngine.Serialization;
1112
using ILRuntime.Runtime.Enviorment;
@@ -443,7 +444,7 @@ void BindVal(MemberInfo mi)
443444
/// Active
444445
/// </summary>
445446
/// <param name="classData"></param>
446-
public void Active(ClassData classData)
447+
public async Task Active(ClassData classData)
447448
{
448449
string classType =
449450
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
@@ -458,31 +459,50 @@ public void Active(ClassData classData)
458459
Log.PrintError($"自动绑定{name}出错:{classType}没有成功绑定数据,自动激活成功,但可能会抛出空异常!");
459460
}
460461

461-
//不管是啥类型,直接invoke这个awake方法
462-
var flags = BindingFlags.Default | BindingFlags.Public
463-
| BindingFlags.Instance | BindingFlags.FlattenHierarchy |
464-
BindingFlags.NonPublic | BindingFlags.Static;
465-
var awakeMethod = clrInstance.GetType().GetMethod("Awake",flags);
466-
if (awakeMethod == null)
462+
if (classData.ClrInstance is MonoBehaviourAdapter.Adaptor mb)
467463
{
468-
awakeMethod = t.GetMethod("Awake", flags);
464+
mb.Awake();
469465
}
470-
else
466+
else if (classData.ClrInstance is ClassBindNonMonoBehaviourAdapter.Adaptor mb2)
471467
{
472-
awakeMethod.Invoke(clrInstance, null);
473-
classData.Activated = true;
468+
mb2.Awake();
474469
}
475-
476-
if (awakeMethod == null)
470+
else
477471
{
478-
Log.PrintError($"{t.FullName}不包含Awake方法,无法激活,已跳过");
472+
//不管是啥类型,直接invoke这个awake方法
473+
var flags = BindingFlags.Default | BindingFlags.Public
474+
| BindingFlags.Instance | BindingFlags.FlattenHierarchy |
475+
BindingFlags.NonPublic | BindingFlags.Static;
476+
var awakeMethod = clrInstance.GetType().GetMethod("Awake",flags);
477+
if (awakeMethod == null)
478+
{
479+
awakeMethod = t.GetMethod("Awake", flags);
480+
}
481+
else
482+
{
483+
awakeMethod.Invoke(clrInstance, null);
484+
classData.Activated = true;
485+
}
486+
487+
if (awakeMethod == null)
488+
{
489+
Log.PrintError($"{t.FullName}不包含Awake方法,无法激活,已跳过");
490+
}
491+
else if (!classData.Activated)
492+
{
493+
awakeMethod.Invoke(clrInstance, null);
494+
}
479495
}
480-
else if (!classData.Activated)
496+
497+
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
498+
LifeCycleMgr.Instance.AddTask(() =>
481499
{
482-
awakeMethod.Invoke(clrInstance, null);
483-
}
484-
((MonoBehaviour)clrInstance).enabled = true;
485-
classData.Activated = true;
500+
501+
((MonoBehaviour)clrInstance).enabled = true;
502+
classData.Activated = true;
503+
tcs.SetResult(true);
504+
});
505+
await tcs.Task;
486506
}
487507

488508
Remove();

UnityProject/Assets/Dependencies/JEngine/Core/Util/InitJEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public async Task LoadHotUpdateCallback()
8181
//调用SetupGame周期
8282
Tools.InvokeHotMethod(HotMainType, SetupGameMethod);
8383
//初始化ClassBind
84-
ClassBindMgr.Instantiate();
84+
await ClassBindMgr.Instantiate();
8585
//调用RunGame周期
8686
Tools.InvokeHotMethod(HotMainType, RunGameMethod);
8787
//调用在主工程的热更代码加载完毕后的周期

UnityProject/Assets/Dependencies/JEngine/Editor/JEngineTools/CryptoWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void OnGUI()
5656
//文本
5757
Key = EditorGUILayout.TextField("Encrypt Key (加密密码)", Key);
5858
GUILayout.Space(30);
59-
if (GUILayout.Button("Build Bundles (打包资源)"))
59+
if (GUILayout.Button("Encrypt Dll (加密Dll)"))
6060
{
6161
if (Key == null)
6262
{
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)