一、很好奇Unity中继承自MonoBehaviour的脚本和不继承MonoBehaviour的脚本能不能被打成Dll文件就测试了下,为了方便我是同时测试的。
准备工作
1、Unity中创建一个脚本是继承自MonoBehaviour的,我起名为 DllTest.cs
2、在创建一个脚本 是不继承MonoBehaviour的 ,Person.cs
DllTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DllTest : MonoBehaviour//继承了MonoBehaviour的类
{
private void Update()
{
transform.position += Vector3.forward * Time.deltaTime;
}
}
Person.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Person//没有继承MonoBehaviour的类
{
public string name;
public int age;
public float height;
public Person(string _name, int _age, float _height)
{
this.name = _name;
this.age = _age;
this.height = _height;
}
}
VS2017,创建项目
删除我们用不到的脚本
创建后 会默认帮我们创建一个脚本,我们用不到删掉
如果想使用unity的api,则需要引入unity的Dll,UnityEditor.dll 和 UnityEngine.dll
这两个Dll文件在Unity安装目录下的 Unity\Editor\Data\Managed
添加文件
添加我们需要打成Dll文件的脚本
把Unity中写的两个脚本添加进来
之后点击生成解决方案,让程序运行,运行之后看输出是否有失败,如果全部成功就说明成功了
之后在文件夹中的bin—》Debug中找到自己的dll文件导入到Unity
扫描二维码关注公众号,回复:
12884448 查看本文章
删除Unity中的DllTest和Person两个脚本,导入做好的Dll
因为DllTest.cs是继承自MonoBehaviour,我们运行挂到Cube上面,执行了代码:说明继承MonoBehaviour的类封装成Dll是可用的
测试没有继承MonoBehaviour的Person类
我们定义到DllTest,发现我们看不到方法体了只有方法名字
结语:测试ok都可以正常使用,并且如果继承自MonoBehaviour的还是可以挂在GameObject上。