步驟:
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);
}