Unity5熱更新ILRuntime 使用 Protobuf3.0
須知:
1.pb3官方用到了C#很多的新語法.所以在unity主工程中直接擼碼是不可以的.還好github上面有同僚作了framework35版的.
2.ILrt中的類目前是不能多繼承主程序中的多接口的這是值得注意的地方.也就說repeat目前是沒法用的.也是本文不完美的地方.只能用除了repeat之外的功能.具體見下文.
3.親測ios是運行沒有問題的.有問題給本人留言.
原料獲取:
1.https://github.com/bitcraftCoLtd/protobuf3-for-unity 獲取該處的pb3.
2.https://github.com/Ourpalm/ILRuntime 獲取最新的ILrt源碼.
原理:
首先說明一下我說的DLL指的就是ILrt的DLL熱更新環境. 主程序指的是Unity環境.pb指的是上面給的github裏的pb. ILrt指的是ILRuntime.
pb會用proto文件生成一個對應語言的code代碼這是大家都知道的.問題主要集中在ILrt要支持這個code代碼.由於ILrt多繼承接口的限制所以repeat我是沒有解決的.其他的正常使用還沒發現有什麼問題.
本使用方法是把pb的源碼全部放到DLL中的.1.第一步
PB的源碼全部copy到DLL中.ILrt是支持高版本的語法的所以這裏直接你選4.5是可以直接編譯通過的,如果你想實現dll放到主程序也能跑起.那麼需要你在工程中填入宏 DOTNET35. 這是pb裏給的宏.具體看pb的github頁面.可以降到35.
2.第二步
編譯你的proto文件放到DLL項目中
3.第三步
由於你的pb裏面有繼承主程序裏的接口所以需要寫adapter適配他們.見下文.
完成上面三步應該就可以運行了.祝你好運.有問題請到ILrt的官方羣討論.QQ羣: 512079820
//適配文件放到主程序中
using System;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.CLR.Method;
using System.IO;
using System.Collections.Generic;
using System.Collections;
public class Adapter_Protobuf : CrossBindingAdaptor
{
public override Type BaseCLRType
{
get
{
return null;
}
}
public override Type[] BaseCLRTypes
{
get
{
return new Type[] {typeof(IEquatable<ILTypeInstance>), typeof(IComparable<ILTypeInstance>), typeof(IEnumerable<System.Byte>)};
}
}
public override Type AdaptorType
{
get
{
return typeof(Adaptor);
}
}
public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
{
return new Adaptor(appdomain, instance);
}
internal class Adaptor : IEquatable<ILTypeInstance>, IComparable<ILTypeInstance>, IEnumerable<System.Byte>, CrossBindingAdaptorType
{
ILTypeInstance instance;
ILRuntime.Runtime.Enviorment.AppDomain appdomain;
public Adaptor()
{
}
public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
{
this.appdomain = appdomain;
this.instance = instance;
}
public object[] data1 = new object[1];
public ILTypeInstance ILInstance { get { return instance; } }
IMethod mEquals = null;
bool mEqualsGot = false;
public bool Equals(ILTypeInstance other)
{
if (!mEqualsGot)
{
mEquals = instance.Type.GetMethod("Equals", 1);
if (mEquals == null)
{
mEquals = instance.Type.GetMethod("System.IEquatable.Equals", 1);
}
mEqualsGot = true;
}
if (mEquals != null)
{
data1[0] = other;
return (bool)appdomain.Invoke(mEquals, instance, data1);
}
return false;
}
IMethod mCompareTo = null;
bool mCompareToGot = false;
public int CompareTo(ILTypeInstance other)
{
if (!mCompareToGot)
{
mCompareTo = instance.Type.GetMethod("CompareTo", 1);
if (mCompareTo == null)
{
mCompareTo = instance.Type.GetMethod("System.IComparable.CompareTo", 1);
}
mCompareToGot = true;
}
if (mCompareTo != null)
{
data1[0] = other;
return (int)appdomain.Invoke(mCompareTo, instance, data1);
}
return 0;
}
public IEnumerator<byte> GetEnumerator()
{
IMethod method = null;
method = instance.Type.GetMethod("GetEnumerator", 0);
if (method == null)
{
method = instance.Type.GetMethod("System.Collections.IEnumerable.GetEnumerator", 0);
}
if (method != null)
{
var res = appdomain.Invoke(method, instance, null);
return (IEnumerator<byte>)res;
}
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
IMethod method = null;
method = instance.Type.GetMethod("GetEnumerator", 0);
if (method == null)
{
method = instance.Type.GetMethod("System.Collections.IEnumerable.GetEnumerator", 0);
}
if (method != null)
{
var res = appdomain.Invoke(method, instance, null);
return (IEnumerator)res;
}
return null;
}
}
}