登录
  • 欢迎访问 Sharezer Blog

打包自己的dll并在Unity中调用

Unity sharezer 2368次浏览 已收录 0个评论

1、在 VS 中新建一个类库 MyLib

打包自己的dll并在Unity中调用

2、新建一个 Class1

增加变量_value 和方法 SetValue

打包自己的dll并在Unity中调用

并点击生成解决方案

打包自己的dll并在Unity中调用

在输出窗口,可以看到 dll 路径

打包自己的dll并在Unity中调用

将 dll 拷贝到 Unity Assets 目录下。

3、在 Untiy 的脚本中引用 MyLib,就可以调用 Class1 中的方法了

打包自己的dll并在Unity中调用

现在,假如我要写一个类,继承于 MonoBehaviour,并且在 Unity 编辑器中使用,那又要怎么做呢。

打开刚刚新建的 MyLib 的类库,添加 UnityEngine.dll

打包自己的dll并在Unity中调用

打包自己的dll并在Unity中调用

新建类 Class2,引入 UnityEngine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace MyLib
{
    class Class2 : MonoBehaviour
    {
        public int test = 0;
        void Start() {
            Debug.Log("test: " + test);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
 
namespace MyLib
{
    class Class2 : MonoBehaviour
    {
        public int test = 0;
        void Start() {
            Debug.Log("test: " + test);
        }
    }
}

重新生成解决方案,把 dll 拷贝到 Unity 目录下

在编辑器下就可以看到 Class2,和 test 变量

打包自己的dll并在Unity中调用

打包自己的dll并在Unity中调用

但是会出现错误提示:TypeLoadException: Could not load type ‘System.Runtime.Versioning.TargetFrameworkAttribute’ from assembly ‘MyLib’.
System.MonoCustomAttrs.GetCustomAttributesBase

打包自己的dll并在Unity中调用

只需要把 MyLib 中的目标框架改成 Unity 的,删除 using System.Threading.Tasks,重新生成一下 dll

打包自己的dll并在Unity中调用

错误不见了,可以正常运行

打包自己的dll并在Unity中调用


Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明打包自己的dll并在Unity中调用
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址