1、首先要在unity中编写好需要打包的脚本。
2、在VS中创建一个 . NET Framework 类库,把编写好的脚本内容copy到类库中,生成类库
3、生成的类库是 .dll 文件,把 .dll 后缀改成 .bytes
4、把改好的类库导入unity工程中,设置AssetBundle包名和后缀就可以打包了
5、加载脚本AssetBundle包时,只用加载出来就可以使用了
IEnumerator Load()
{
WWW www = new WWW( AssetBundle包文件夹 + "scripts.assetbundle");
yield return www;
AssetBundle ab = www.assetBundle;
TextAsset text = ab.LoadAsset("类库名", typeof(TextAsset)) as TextAsset;
byte[] by = text.bytes;
Assembly am = Assembly.Load(by);
Type type = am.GetType("类库名.类名");
gameObject.AddComponent(type);
}
注意:
1、最好是在unity中编写并测试好再打包成类库
2、在VS中要引用 UnityEngine 类库,到unity安装程序中搜索一下就找到了
3、上面的代码是使用unity反射来获取类库的