步骤:
1.创建C# dll 空工程TestCoreDll
2.添加库引用(该unity3d所需要用到dll,unity3d工程目录下Library\ScriptAssemblies中的库)
3.拷贝unity客户端代码至TestCoreDll工程目录下
4.点击菜单栏生成按钮下重新生成TestCoreDll,即生成TestCoreDll.dll文件
5.将TestCoreDll.dll备份至unity3d工程目录下,并修改器后缀名为TestCoreDll.bytes
6.使用BuildPipeline.BuildAssetBundle将TestCoreDll.bytes编译成对应的assetbundle,即TestCoreDll.assetbundle
7.使用程序集Assembly加载使用TestCoreDll.assetbundle中代码,至此C#代码自动更新原理结束
代码片段:如下
生成dll文件:
msbuild "%targetProjectName%.csproj" /p:Configuration=Release /p:DefineConstants="TRACE;UNITY_5;USE_ANIMEVENT_INT_PARAMETER;UNITY_ANDROID;LOAD_DLL" /m /nologo /clp:ErrorsOnly /flp1:logfile=errors.txt;errorsonly
生成assetBundle文件
BuildPipeline.BuildAssetBundle(obj, null, "Assets/StreamingAssets/ClientCore.assetbundle", BuildAssetBundleOptions.CollectDependencies, target);
使用程序集加载引用代码:
WWW www = new WWW(path);
AssetBundle bundle = www.assetBundle;
TextAsset asset = bundle.LoadAsset("TestCoreDll", typeof(TextAsset)) as TextAsset;
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(asset.bytes);
Type script = assembly.GetType("TestScript");
if (script != null)
{
gameObject.AddComponent(script);
}