纳金网

标题: unity3d动态加载脚本 [打印本页]

作者: 狂风大尉    时间: 2014-12-30 00:37
标题: unity3d动态加载脚本
本文记录如何通过unity3d进行脚本资源打包加载

  1、创建TestDll.cs文件
  1. public class TestDll : MonoBehaviour {

  2. void Start () {

  3. print(“Hi U_tansuo!”);

  4. }

  5. }
复制代码
2、生成dll文件

  (1)使用vs打包

  (2) 使用mono打包

  (3) 命令行打包 mac下(亲测):  /Applications/Unity/Unity.app/Contents/Frameworks/Mono/bin/gmcs -r:/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll -target:library 脚本路径

  win下(未试过):mcs -r: /unity安装根目录\Unity\Editor\Data\Managed/UnityEngine.dll -target:library 脚本路径

  3、更改文件后缀

  至关重要一步  更改上一步生成的TestDLL.dll 为 TestDLL.bytes  否则 打包加载会错

  4、使用 BuildPipeline.BuildAssetBundle进行打包 资源为 TestDll.unity3d

  5、加载
  1. IEnumerator Test()

  2. {

  3. string url=“file://”+Application.dataPath+“/TestDll.unity3d”;

  4. print(url);

  5. WWW www = WWW.LoadFromCacheOrDownload (url, 1);

  6. // Wait for download to complete

  7. yield return www;

  8. // Load and retrieve the AssetBundle

  9. AssetBundle bundle = www.assetBundle;

  10. //TestDll 是资源的名字

  11. TextAsset txt = bundle.Load(“TestDll”, typeof(TextAsset)) as TextAsset;

  12. print(txt.bytes.Length);

  13. // Load the assembly and get a type (class) from it

  14. var assembly = System.Reflection.Assembly.Load(txt.bytes);

  15. var type = assembly.GetType(“TestDll”);

  16. // Instantiate a GameObject and add a component with the loaded class

  17. gameObject.AddComponent(type);

  18. }
复制代码

作者: heise    时间: 2014-12-30 02:16
感谢分享!!!!!!!
作者: 痞子    时间: 2014-12-30 09:08
大尉不愧是纳金网论坛的功臣之一




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5