在HotFix项目中:
我为了保证下面这段代码顺利执行,我必须做的操作有哪些呢:
public class RecKnapItemData
{
public int m_nPlayerId;
public int m_nItemId;
public int m_nItemType;
public int m_nItemNum;
public int m_nItemSkillId;
public int m_nItemEndTime;
}
List<RecKnapItemData> list= stuList.OrderBy(x => x.m_nItemType).ThenBy(m => m.m_nItemId).ToList<RecKnapItemData>();
操作1:在unity 主工程中 => 程序的入口处 => ILRuntime的初始化函数void InitializeILRuntime()中,添加红框处 委托Function<TSource,Tkey> 的委托适配器(DelegateAdapter):
添加如下:
void InitializeILRuntime()
{
appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, System.UInt32>();
}
未完待续。。。
附件:
1:我的ILRuntime的程序入口脚本,暂不完整
//---------这是附件 1 :---------
// **********************************************************************
// Copyright (C)
// Author: wanAn
// Date: 2019-10-17
// Desc: ILRuntime的初始化,进行各种注册
// **********************************************************************
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
//using static UnityScript.Lang.Array;
public class ILMgr : MonoBehaviour
{
static ILRuntime.Runtime.Enviorment.AppDomain appdomain;
System.IO.MemoryStream fs;
System.IO.MemoryStream p;
// Use this for initialization
public static ILMgr instance { get; set; }
public static ILRuntime.Runtime.Enviorment.AppDomain domain
{
get { return appdomain; }
}
private void Awake()
{
instance = this;
}
void Start()
{
StartCoroutine(LoadHotFixAssembly());
}
IEnumerator LoadHotFixAssembly()
{
//首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
//正常项目中应该是自行从其他地方下载dll,或者打包在AssetBundle中读取,平时开发以及为了演示方便直接从StreammingAssets中读取,
//正式发布的时候需要大家自行从其他地方读取dll
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//这个DLL文件是直接编译HotFix_Project.sln生成的,已经在项目中设置好输出目录为StreamingAssets,在VS里直接编译即可生成到对应目录,无需手动拷贝
#if UNITY_ANDROID
WWW www = new WWW(Application.streamingAssetsPath + "/GDHotFix.dll");
#else
WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/GDHotFix.dll");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] dll = www.bytes;
www.Dispose();
//PDB文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将PDB去掉,下面LoadAssembly的时候pdb传null即可
#if UNITY_ANDROID
www = new WWW(Application.streamingAssetsPath + "/GDHotFix.pdb");
#else
www = new WWW("file:///" + Application.streamingAssetsPath + "/GDHotFix.pdb");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] pdb = www.bytes;
fs = new MemoryStream(dll);
p = new MemoryStream(pdb);
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
InitializeILRuntime();
OnHotFixLoaded();
}
void InitializeILRuntime()
{
appdomain.DebugService.StartDebugService(56000);
//这里做一些ILRuntime的注册,比如委托的适配器,但是为了演示不些适配器的报错,注册写在了OnHotFixLoaded里
//初始化CLR绑定,让DLL里面的调用更快
ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain);
//注册LitJson到DLL
LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);
//注册一些类到DLL
//注册适配器
//appdomain.RegisterCrossBindingAdaptor(new TKDataNodeBaseAdapter());//UNDOWN
//注册MonoBehaviour到DLL
appdomain.RegisterCrossBindingAdaptor(new MonoBehaviourAdapter());//TODO
//注册协程到DLL
appdomain.RegisterCrossBindingAdaptor(new CoroutineAdapter());//TODO
注册委托到DLL
//Action<string> 的参数为一个string
appdomain.DelegateManager.RegisterMethodDelegate<string>();
appdomain.DelegateManager.RegisterMethodDelegate<int>();
appdomain.DelegateManager.RegisterMethodDelegate<UnityEngine.GameObject>();
//带返回值的委托的话需要用RegisterFunctionDelegate,返回类型为最后一个
appdomain.DelegateManager.RegisterFunctionDelegate<int, string>();
appdomain.DelegateManager.RegisterFunctionDelegate<UInt32>();
注册unity点击事件
appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((action) =>
{
return new UnityEngine.Events.UnityAction(() =>
{
((System.Action)action)();
});
});
appdomain.DelegateManager.RegisterMethodDelegate<System.IAsyncResult>();
appdomain.DelegateManager.RegisterDelegateConvertor<System.AsyncCallback>((act) =>
{
return new System.AsyncCallback((ar) =>
{
((Action<System.IAsyncResult>)act)(ar);
});
});
appdomain.DelegateManager.RegisterDelegateConvertor<System.Threading.ThreadStart>((act) =>
{
return new System.Threading.ThreadStart(() =>
{
((Action)act)();
});
});
// 定义类
// 定义类
appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, ILTypeInstance, Int32>();
//appdomain.DelegateManager.RegisterDelegateConvertor<Comparison>((act)=>
//{
// return new Comparison((x,y) =>
// {
// return ((Func<ILTypeInstance, ILTypeInstance,Int32>)act)(x,y);
// });
//});
appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, System.Int32>();
appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, System.UInt32>();
}
object gameMgrObj = null;
void OnHotFixLoaded()
{
appdomain.Invoke("MainClass", "LogicStart", null, null);
//object obj = appdomain.Instantiate("GDHotFix.UIMainMgr", null);
//appdomain.Invoke("GDHotFix.UIMainMgr", "ShowUIMain", obj
// , null);
gameMgrObj = appdomain.Instantiate("GDHotFix.GameMgr", null);
appdomain.Invoke("GDHotFix.GameMgr", "GameStart", gameMgrObj, null);
}
private void Update()
{
appdomain.Invoke("GDHotFix.GameMgr", "GameUpdate", gameMgrObj, null);
}
private void LateUpdate()
{
//appdomain.Invoke("MainClass", "GameLateUpdate", null, null);
}
private void FixedUpdate()
{
appdomain.Invoke("MainClass", "GameFixedUpdate", null, null);
}
private void OnApplicationQuit()
{
appdomain.Invoke("MainClass", "GameQuit", null, null);
}
private void OnDestroy()
{
if (fs != null)
fs.Close();
if (p != null)
p.Close();
fs = null;
p = null;
}
}