Skip to content

merge dev #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -659,17 +659,16 @@ private static object DoInstantiate(GameObject ins, GameObject res, AppDomain do
{
if (res.GetComponentsInChildren<ClassBind>(true).Length > 0)
{
ClassBindMgr.DoBind(res.GetComponentsInChildren<ClassBind>(true).ToList());
ClassBindMgr.DoBind(res.GetComponentsInChildren<ClassBind>(true));
}

return res;
}

//如果同时有adaptor和classbind,肯定是复制的,要给删了
foreach (var t in res.GetComponentsInChildren<Transform>(true))
foreach (var cb in res.GetComponentsInChildren<ClassBind>(true))
{
var go = t.gameObject;
var cb = go.GetComponent<ClassBind>();
var go = cb.gameObject;
if (cb != null && go.GetComponent<CrossBindingAdaptorType>() != null)
{
UnityEngine.Object.DestroyImmediate(cb); //防止重复的ClassBind
Expand Down Expand Up @@ -754,6 +753,7 @@ private static object DoInstantiate(GameObject ins, GameObject res, AppDomain do
if (awakeMethod != null)
{
awakeMethod.Invoke(clrInstance, null);
LifeCycleMgr.Instance.ExecuteOnceTask();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using UnityEngine;
using System.Threading.Tasks;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

namespace JEngine.Core
{
public partial class ClassBindMgr : MonoBehaviour
{
public static async Task Instantiate()
public static void Instantiate()
{
if (_instance != null)
return;
Expand All @@ -42,11 +42,11 @@ public static async Task Instantiate()
SceneManager.sceneLoaded += _instance.OnSceneLoaded;
SceneManager.sceneUnloaded += _instance.OnSceneUnloaded;
LoadedScenes.Add(SceneManager.GetActiveScene());
await DoBind();
DoBind();
}

private static ClassBindMgr _instance;
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>() { };
public static readonly HashSet<Scene> LoadedScenes = new HashSet<Scene>();
private static readonly List<ClassBind> Cbs = new List<ClassBind>(30);

private void Awake()
Expand All @@ -60,7 +60,7 @@ private void Awake()
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
LoadedScenes.Add(scene);
_ = DoBind();
DoBind();
}

private void OnSceneUnloaded(Scene scene)
Expand All @@ -80,7 +80,7 @@ private void OnDestroy()
}
}

public static async Task DoBind(List<ClassBind> cbs)
public static void DoBind(ICollection<ClassBind> cbs)
{
foreach (var cb in cbs)
{
Expand All @@ -106,10 +106,13 @@ public static async Task DoBind(List<ClassBind> cbs)
continue;
}

await cb.SetVal(data);
cb.SetVal(data);
}
}

//确保任务全执行了
LifeCycleMgr.Instance.ExecuteOnceTask();

//激活
foreach (var cb in cbs)
{
Expand All @@ -120,22 +123,47 @@ public static async Task DoBind(List<ClassBind> cbs)
continue;
}

await cb.Active(data);
cb.Active(data);
}
}

//确保任务全执行了
LifeCycleMgr.Instance.ExecuteOnceTask();
}

public static async Task DoBind(ClassBind cb)
private static readonly List<ClassBind> Temp = new List<ClassBind>(1);

public static void DoBind(ClassBind cb)
{
if (Cbs.Contains(cb)) return;
await DoBind(new List<ClassBind> { cb });
Cbs.Add(cb);
if (Temp.Count == 1)
{
if (Temp[0] == null)
{
Temp[0] = cb;
DoBind(Temp);
}
else
{
DoBind(new List<ClassBind>(1)
{
cb
});
}
}
else
{
Temp.Add(cb);
DoBind(Temp);
}
}

public static async Task DoBind()
public static void DoBind()
{
var c = Tools.FindObjectsOfTypeAll<ClassBind>();
Cbs.AddRange(c);
await DoBind(c);
DoBind(c);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,22 @@ private bool IgnoreWithInInstances(in LifeCycleItem* item)
/// remove obj from instances
/// </summary>
private Predicate<IntPtr> RemoveInstanceIfContainsPredicate => RemoveInstanceIfContains;

/// <summary>
/// execute once task
/// </summary>
private bool _onceTaskExecuting;

/// <summary>
/// 处理只调用一次的任务
/// </summary>
public void ExecuteOnceTask()
{
if (_onceTaskExecuting) return;
_onceTaskExecuting = true;
ExecuteItems(_onceTaskItems);
_onceTaskExecuting = false;
}

/// <summary>
/// unity周期
Expand All @@ -596,7 +612,7 @@ private void FixedUpdate()
private void Update()
{
//处理只调用一次的任务
ExecuteItems(_onceTaskItems);
ExecuteOnceTask();
//处理update
//确保本帧没处理过这些对象
//调用update
Expand Down
111 changes: 54 additions & 57 deletions UnityProject/Assets/Dependencies/JEngine/Core/Util/ClassBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Reflection;
using ILRuntime.CLR.Utils;
using ILRuntime.Reflection;
using System.Threading.Tasks;
using JEngine.Core.DO_NOT_USE;
using ILRuntime.CLR.TypeSystem;
using UnityEngine.Serialization;
Expand Down Expand Up @@ -161,15 +160,41 @@ t.BaseType is ILRuntimeWrapperType wrapperType
/// Set value
/// </summary>
/// <param name="classData"></param>
public async Task SetVal(ClassData classData)
public void SetVal(ClassData classData)
{
string classType =
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
Type t = classData.ClassType; //获取实际属性
var clrInstance = classData.ClrInstance;
//绑定数据
classData.BoundData = false;
var fields = classData.fields.ToArray();
var fields = classData.fields;

void BindVal(ClassField field, object obj)
{
try
{
var fi = t.GetField(field.fieldName, AllBindingFlags);
if (fi == null) fi = t.BaseType?.GetField(field.fieldName, AllBindingFlags);
if (fi != null)
{
fi.SetValue(clrInstance.ILInstance, obj);
}
else
{
var pi = t.GetProperty(field.fieldName, AllBindingFlags);
if (pi == null) pi = t.BaseType?.GetProperty(field.fieldName, AllBindingFlags);
if (pi == null)
throw new NullReferenceException();
pi.SetValue(clrInstance.ILInstance, obj);
}
}
catch (Exception e)
{
Log.PrintError(
$"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{e.Message},已跳过");
}
}

foreach (ClassField field in fields)
{
Expand Down Expand Up @@ -375,21 +400,28 @@ void SetField(Type fieldType)
}
else if (field.fieldType == ClassField.FieldType.HotUpdateResource)
{
//Unity 编辑器下AssetDatabase读取图片会变texture2d导致无法给sprite赋值
var fieldType = t.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
(t.BaseType?.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
(t.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType ??
t.BaseType?.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType));
fieldType = fieldType is ILRuntimeWrapperType wrapperType ? wrapperType.RealType : fieldType;
var o = await AssetMgr.LoadAsync(field.value, fieldType);
if (fieldType == typeof(Sprite) && o is Texture2D tx)
LifeCycleMgr.Instance.AddTask(async () =>
{
o = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(0.5f, 0.5f),
100.0f);
}
//Unity 编辑器下AssetDatabase读取图片会变texture2d导致无法给sprite赋值
var fieldType = t.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
(t.BaseType?.GetField(field.fieldName, AllBindingFlags)?.FieldType ??
(t.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType ??
t.BaseType?.GetProperty(field.fieldName, AllBindingFlags)?.PropertyType));
fieldType = fieldType is ILRuntimeWrapperType wrapperType
? wrapperType.RealType
: fieldType;
var o = await AssetMgr.LoadAsync(field.value, fieldType);
if (fieldType == typeof(Sprite) && o is Texture2D tx)
{
o = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(0.5f, 0.5f),
100.0f);
}

obj = o;
obj = o;
BindVal(field,obj);
});
classData.BoundData = true;
continue;
}
}
catch (Exception except)
Expand All @@ -401,41 +433,7 @@ void SetField(Type fieldType)
//如果有数据再绑定
if (classData.BoundData)
{
void BindVal(MemberInfo mi)
{
try
{
switch (mi)
{
case null:
throw new NullReferenceException();
case FieldInfo info:
info.SetValue(clrInstance.ILInstance, obj);
break;
case PropertyInfo inf:
inf.SetValue(clrInstance.ILInstance, obj);
break;
}
}
catch (Exception e)
{
Log.PrintError(
$"自动绑定{name}出错:{classType}.{field.fieldName}赋值出错:{e.Message},已跳过");
}
}

var fi = t.GetField(field.fieldName, AllBindingFlags);
if (fi == null) fi = t.BaseType?.GetField(field.fieldName, AllBindingFlags);
if (fi != null)
{
BindVal(fi);
}
else
{
var pi = t.GetProperty(field.fieldName, AllBindingFlags);
if (pi == null) pi = t.BaseType?.GetProperty(field.fieldName, AllBindingFlags);
BindVal(pi);
}
BindVal(field, obj);
}
}
}
Expand All @@ -444,7 +442,7 @@ void BindVal(MemberInfo mi)
/// Active
/// </summary>
/// <param name="classData"></param>
public async Task Active(ClassData classData)
public void Active(ClassData classData)
{
string classType =
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
Expand Down Expand Up @@ -494,18 +492,17 @@ public async Task Active(ClassData classData)
}
}

TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
LifeCycleMgr.Instance.AddTask(() =>
{

((MonoBehaviour)clrInstance).enabled = true;
classData.Activated = true;
tcs.SetResult(true);
Remove();
});
await tcs.Task;
}

Remove();
else
{
Remove();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task LoadHotUpdateCallback()
//调用SetupGame周期
Tools.InvokeHotMethod(HotMainType, SetupGameMethod);
//初始化ClassBind
await ClassBindMgr.Instantiate();
ClassBindMgr.Instantiate();
//调用RunGame周期
Tools.InvokeHotMethod(HotMainType, RunGameMethod);
//调用在主工程的热更代码加载完毕后的周期
Expand Down

This file was deleted.

Loading