///作者Unity3d师兄---LeroyYang
通过网上大神们的资料以及自己的整理,学习一下用vs2013简单的封装dll文件,方便接口模式下开发,使得逻辑层更为清晰。
操作步骤
1、打开vs2013,新建项目 -新建类库 (注意最上面.NET Framework选择3.5版本以下的,因为Unity3D(当前的Unity3D版本是3.5版) 支持的 .Net 是3.5版。 如果选择的是4.0版会出现 Internal compiler error. See the console log for more information. output was:Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning. 错误。
)
2、项目新建完成之后,编写简单的测试代码
using System;
namespace yanglei {
public class Yl
{
public static int Attack(int hp)
{
return hp;
}
}
}
3、生成解决方案,找到vs项目工程文件夹目录在E:\ProjectYang\bin\Debug下的dll文件
4、导入dll到Unity中,在Unity中Asset文件夹下新建文件夹名为Plugins目录
5、在Unity新建脚本
using UnityEngine;
using System.Collections;
using yanglei;
public class actionscript : MonoBehaviour {
void Start () {
int hp = Yl.Attack(100);
Debug.Log(hp);
}
}
运行结果

本文介绍如何使用Visual Studio 2013创建并封装DLL文件,以便在Unity3D项目中调用。主要步骤包括:创建.NET Framework 3.5版本的类库项目、编写测试代码、生成DLL、导入Unity并编写调用脚本。
1666

被折叠的 条评论
为什么被折叠?



