Skip to content

Commit 941988e

Browse files
Merge pull request #464 from JasonXuDeveloper/development
merge dev
2 parents b05e496 + ee87a7e commit 941988e

File tree

14 files changed

+171
-81
lines changed

14 files changed

+171
-81
lines changed

UnityProject/Assets/Dependencies/JEngine/Core/ILRuntimeHelper/RegisterMethodRedirection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,17 +659,16 @@ private static object DoInstantiate(GameObject ins, GameObject res, AppDomain do
659659
{
660660
if (res.GetComponentsInChildren<ClassBind>(true).Length > 0)
661661
{
662-
ClassBindMgr.DoBind(res.GetComponentsInChildren<ClassBind>(true).ToList());
662+
ClassBindMgr.DoBind(res.GetComponentsInChildren<ClassBind>(true));
663663
}
664664

665665
return res;
666666
}
667667

668668
//如果同时有adaptor和classbind,肯定是复制的,要给删了
669-
foreach (var t in res.GetComponentsInChildren<Transform>(true))
669+
foreach (var cb in res.GetComponentsInChildren<ClassBind>(true))
670670
{
671-
var go = t.gameObject;
672-
var cb = go.GetComponent<ClassBind>();
671+
var go = cb.gameObject;
673672
if (cb != null && go.GetComponent<CrossBindingAdaptorType>() != null)
674673
{
675674
UnityEngine.Object.DestroyImmediate(cb); //防止重复的ClassBind
@@ -754,6 +753,7 @@ private static object DoInstantiate(GameObject ins, GameObject res, AppDomain do
754753
if (awakeMethod != null)
755754
{
756755
awakeMethod.Invoke(clrInstance, null);
756+
LifeCycleMgr.Instance.ExecuteOnceTask();
757757
}
758758
else
759759
{

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
26+
2627
using UnityEngine;
27-
using System.Threading.Tasks;
2828
using System.Collections.Generic;
2929
using UnityEngine.SceneManagement;
3030

3131
namespace JEngine.Core
3232
{
3333
public partial class ClassBindMgr : MonoBehaviour
3434
{
35-
public static async Task Instantiate()
35+
public static void Instantiate()
3636
{
3737
if (_instance != null)
3838
return;
@@ -42,11 +42,11 @@ public static async Task Instantiate()
4242
SceneManager.sceneLoaded += _instance.OnSceneLoaded;
4343
SceneManager.sceneUnloaded += _instance.OnSceneUnloaded;
4444
LoadedScenes.Add(SceneManager.GetActiveScene());
45-
await DoBind();
45+
DoBind();
4646
}
4747

4848
private static ClassBindMgr _instance;
49-
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>() { };
49+
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>();
5050
private static readonly List<ClassBind> Cbs = new List<ClassBind>(30);
5151

5252
private void Awake()
@@ -60,7 +60,7 @@ private void Awake()
6060
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
6161
{
6262
LoadedScenes.Add(scene);
63-
_ = DoBind();
63+
DoBind();
6464
}
6565

6666
private void OnSceneUnloaded(Scene scene)
@@ -80,7 +80,7 @@ private void OnDestroy()
8080
}
8181
}
8282

83-
public static async Task DoBind(List<ClassBind> cbs)
83+
public static void DoBind(ICollection<ClassBind> cbs)
8484
{
8585
foreach (var cb in cbs)
8686
{
@@ -106,10 +106,13 @@ public static async Task DoBind(List<ClassBind> cbs)
106106
continue;
107107
}
108108

109-
await cb.SetVal(data);
109+
cb.SetVal(data);
110110
}
111111
}
112112

113+
//确保任务全执行了
114+
LifeCycleMgr.Instance.ExecuteOnceTask();
115+
113116
//激活
114117
foreach (var cb in cbs)
115118
{
@@ -120,22 +123,47 @@ public static async Task DoBind(List<ClassBind> cbs)
120123
continue;
121124
}
122125

123-
await cb.Active(data);
126+
cb.Active(data);
124127
}
125128
}
129+
130+
//确保任务全执行了
131+
LifeCycleMgr.Instance.ExecuteOnceTask();
126132
}
127133

128-
public static async Task DoBind(ClassBind cb)
134+
private static readonly List<ClassBind> Temp = new List<ClassBind>(1);
135+
136+
public static void DoBind(ClassBind cb)
129137
{
130138
if (Cbs.Contains(cb)) return;
131-
await DoBind(new List<ClassBind> { cb });
139+
Cbs.Add(cb);
140+
if (Temp.Count == 1)
141+
{
142+
if (Temp[0] == null)
143+
{
144+
Temp[0] = cb;
145+
DoBind(Temp);
146+
}
147+
else
148+
{
149+
DoBind(new List<ClassBind>(1)
150+
{
151+
cb
152+
});
153+
}
154+
}
155+
else
156+
{
157+
Temp.Add(cb);
158+
DoBind(Temp);
159+
}
132160
}
133161

134-
public static async Task DoBind()
162+
public static void DoBind()
135163
{
136164
var c = Tools.FindObjectsOfTypeAll<ClassBind>();
137165
Cbs.AddRange(c);
138-
await DoBind(c);
166+
DoBind(c);
139167
}
140168
}
141169
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,22 @@ private bool IgnoreWithInInstances(in LifeCycleItem* item)
576576
/// remove obj from instances
577577
/// </summary>
578578
private Predicate<IntPtr> RemoveInstanceIfContainsPredicate => RemoveInstanceIfContains;
579+
580+
/// <summary>
581+
/// execute once task
582+
/// </summary>
583+
private bool _onceTaskExecuting;
584+
585+
/// <summary>
586+
/// 处理只调用一次的任务
587+
/// </summary>
588+
public void ExecuteOnceTask()
589+
{
590+
if (_onceTaskExecuting) return;
591+
_onceTaskExecuting = true;
592+
ExecuteItems(_onceTaskItems);
593+
_onceTaskExecuting = false;
594+
}
579595

580596
/// <summary>
581597
/// unity周期
@@ -596,7 +612,7 @@ private void FixedUpdate()
596612
private void Update()
597613
{
598614
//处理只调用一次的任务
599-
ExecuteItems(_onceTaskItems);
615+
ExecuteOnceTask();
600616
//处理update
601617
//确保本帧没处理过这些对象
602618
//调用update

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

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reflection;
66
using ILRuntime.CLR.Utils;
77
using ILRuntime.Reflection;
8-
using System.Threading.Tasks;
98
using JEngine.Core.DO_NOT_USE;
109
using ILRuntime.CLR.TypeSystem;
1110
using UnityEngine.Serialization;
@@ -161,15 +160,41 @@ t.BaseType is ILRuntimeWrapperType wrapperType
161160
/// Set value
162161
/// </summary>
163162
/// <param name="classData"></param>
164-
public async Task SetVal(ClassData classData)
163+
public void SetVal(ClassData classData)
165164
{
166165
string classType =
167166
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
168167
Type t = classData.ClassType; //获取实际属性
169168
var clrInstance = classData.ClrInstance;
170169
//绑定数据
171170
classData.BoundData = false;
172-
var fields = classData.fields.ToArray();
171+
var fields = classData.fields;
172+
173+
void BindVal(ClassField field, object obj)
174+
{
175+
try
176+
{
177+
var fi = t.GetField(field.fieldName, AllBindingFlags);
178+
if (fi == null) fi = t.BaseType?.GetField(field.fieldName, AllBindingFlags);
179+
if (fi != null)
180+
{
181+
fi.SetValue(clrInstance.ILInstance, obj);
182+
}
183+
else
184+
{
185+
var pi = t.GetProperty(field.fieldName, AllBindingFlags);
186+
if (pi == null) pi = t.BaseType?.GetProperty(field.fieldName, AllBindingFlags);
187+
if (pi == null)
188+
throw new NullReferenceException();
189+
pi.SetValue(clrInstance.ILInstance, obj);
190+
}
191+
}
192+
catch (Exception e)
193+
{
194+
Log.PrintError(
195+
$"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{e.Message},已跳过");
196+
}
197+
}
173198

174199
foreach (ClassField field in fields)
175200
{
@@ -375,21 +400,28 @@ void SetField(Type fieldType)
375400
}
376401
else if (field.fieldType == ClassField.FieldType.HotUpdateResource)
377402
{
378-
//Unity 编辑器下AssetDatabase读取图片会变texture2d导致无法给sprite赋值
379-
var fieldType = t.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
380-
(t.BaseType?.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
381-
(t.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType ??
382-
t.BaseType?.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType));
383-
fieldType = fieldType is ILRuntimeWrapperType wrapperType ? wrapperType.RealType : fieldType;
384-
var o = await AssetMgr.LoadAsync(field.value, fieldType);
385-
if (fieldType == typeof(Sprite) && o is Texture2D tx)
403+
LifeCycleMgr.Instance.AddTask(async () =>
386404
{
387-
o = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(0.5f, 0.5f),
388-
100.0f);
389-
}
405+
//Unity 编辑器下AssetDatabase读取图片会变texture2d导致无法给sprite赋值
406+
var fieldType = t.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
407+
(t.BaseType?.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
408+
(t.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType ??
409+
t.BaseType?.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType));
410+
fieldType = fieldType is ILRuntimeWrapperType wrapperType
411+
? wrapperType.RealType
412+
: fieldType;
413+
var o = await AssetMgr.LoadAsync(field.value, fieldType);
414+
if (fieldType == typeof(Sprite) && o is Texture2D tx)
415+
{
416+
o = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(0.5f, 0.5f),
417+
100.0f);
418+
}
390419

391-
obj = o;
420+
obj = o;
421+
BindVal(field,obj);
422+
});
392423
classData.BoundData = true;
424+
continue;
393425
}
394426
}
395427
catch (Exception except)
@@ -401,41 +433,7 @@ void SetField(Type fieldType)
401433
//如果有数据再绑定
402434
if (classData.BoundData)
403435
{
404-
void BindVal(MemberInfo mi)
405-
{
406-
try
407-
{
408-
switch (mi)
409-
{
410-
case null:
411-
throw new NullReferenceException();
412-
case FieldInfo info:
413-
info.SetValue(clrInstance.ILInstance, obj);
414-
break;
415-
case PropertyInfo inf:
416-
inf.SetValue(clrInstance.ILInstance, obj);
417-
break;
418-
}
419-
}
420-
catch (Exception e)
421-
{
422-
Log.PrintError(
423-
$"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{e.Message},已跳过");
424-
}
425-
}
426-
427-
var fi = t.GetField(field.fieldName, AllBindingFlags);
428-
if (fi == null) fi = t.BaseType?.GetField(field.fieldName, AllBindingFlags);
429-
if (fi != null)
430-
{
431-
BindVal(fi);
432-
}
433-
else
434-
{
435-
var pi = t.GetProperty(field.fieldName, AllBindingFlags);
436-
if (pi == null) pi = t.BaseType?.GetProperty(field.fieldName, AllBindingFlags);
437-
BindVal(pi);
438-
}
436+
BindVal(field, obj);
439437
}
440438
}
441439
}
@@ -444,7 +442,7 @@ void BindVal(MemberInfo mi)
444442
/// Active
445443
/// </summary>
446444
/// <param name="classData"></param>
447-
public async Task Active(ClassData classData)
445+
public void Active(ClassData classData)
448446
{
449447
string classType =
450448
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
@@ -494,18 +492,17 @@ public async Task Active(ClassData classData)
494492
}
495493
}
496494

497-
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
498495
LifeCycleMgr.Instance.AddTask(() =>
499496
{
500-
501497
((MonoBehaviour)clrInstance).enabled = true;
502498
classData.Activated = true;
503-
tcs.SetResult(true);
499+
Remove();
504500
});
505-
await tcs.Task;
506501
}
507-
508-
Remove();
502+
else
503+
{
504+
Remove();
505+
}
509506
}
510507

511508
/// <summary>

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-
await ClassBindMgr.Instantiate();
84+
ClassBindMgr.Instantiate();
8585
//调用RunGame周期
8686
Tools.InvokeHotMethod(HotMainType, RunGameMethod);
8787
//调用在主工程的热更代码加载完毕后的周期

UnityProject/Assets/HotUpdateResources/Main/Common/Prefab/Prefab.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)