http://www.voidcn.com/article/p-astqcqyq-bph.html
须知:
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; } } }