Unity使用DLL
来源:互联网 发布:阿里云ecs扩容 编辑:程序博客网 时间:2024/06/02 03:00
引用C#DLL
- 首先,我们需要一个库文件,新建一个类库项目,这里就不多介绍了。
- 项目建好后,实现库文件功能。
这是一个简单的测试代码
namespace DllTest{ public class DllClass { public int Sum(int a,int b) { return a+b; } static public bool isMax(int a,int b) { return a>b?true:false; } }}
- 将项目属性 -> 应用程序 -> 目标框架:改为 .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.。
生成解决方案。到此,C# Dll工作完成,在项目的bin/debug 里面就会看到dll文件。
导入Dll到Unity中。将Dll拷贝到Assets目录下任何一个目录都可以。一般是在Plugins目录中,因为Dll大多是外部功能块,同时也方便跨语言调用。
在Unity中编写简单代码。
using UnityEngine;using System.Collections;using DllTest; //Dll的命名空间public class Test : MonoBehaviour{ void Start() { if(DllClass.isMax(10,5)) { DllClass cl = new DllClass(); Debug.Log(cl.Sum(20,10)); } }}
- 刚才我们使用的是原生的C#,假如我们需要在类库项目写调用Unity3D本身功能的方法怎样办呢?
在Unity3D的安装目录Editor\Data\Managed里面,找到UnityEditor.dll和UnityEngine.dll两个文件。然后在类库项目里面添加引用,把这两个dll添加进来.
引用C/C++库文件
1.建立C/C++库工程,编译,拿到DLL文件复制到Plugins文件夹下x86或者x86_x64。注意:是64位dll还是32位dll;
2.库工程文件
Test.h
#if _MSC_VER //当使用Visual Studio 编译时存在它#define EXPORT_API _declspec(dllexport) //Visual Studio 必须用它注释export 函数#else#define EXPORT_API // XCode 不需要export函数,ExPoRT_API 为空#endifextern "C"{ extern EXPORT_API char * PrintHello(); extern int EXPORT_API PrintANumber(); extern int EXPORT_API AddTwoIntegers(int a, int b); extern float EXPORT_API AddTwoFloats(float a, float b);}
Test.cpp
#include "TestDLL.h"extern EXPORT_API char * PrintHello(){ return "Hello\0";}extern int EXPORT_API PrintANumber(){ return 5;}extern int EXPORT_API AddTwoIntegers(int a, int b){ return a + b;}extern float EXPORT_API AddTwoFloats(float a, float b){ return a + b;}
Unity调用代码
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Runtime.InteropServices;//托管/非托管模块间的互相调用public class Test : MonoBehaviour { [DllImport("TestDLL")] private unsafe static extern char * PrintHello(); [DllImport("TestDLL")] private static extern int PrintANumber(); [DllImport("TestDLL")] private static extern int AddTwoIntegers(int a, int b); [DllImport("TestDLL")] private static extern float AddTwoFloats(float a, float b); private void Start() { //string hello = PrintHello(); unsafe { //在传递字符串时,将字符所在的内存固化, //并取出字符数组的指针 fixed (char* p = &("hello".ToCharArray()[0])){ //调用函数 p做参数 } char* a = PrintHello(); Debug.Log(PrintHello()->ToString()); } Debug.Log(PrintANumber()); Debug.Log(AddTwoIntegers(10, 20)); Debug.Log(AddTwoFloats(1.1f, 2.2f)); }}
阅读全文
0 0
- Unity使用DLL
- Unity 使用 Unity 直接编译外部 DLL
- unity 无法使用 dll 库
- C# 生成dll 导入unity中使用
- Unity下使用DLL热更新
- unity 使用Dotfuscator给dll混淆(原)
- Unity中使用和调试Dll
- [Unity3d] 在Unity中使用c++ dll
- Unity-使用System.Windows.Forms.dll
- 关于mono dll和native dll在unity使用
- 在 Unity 工程 (Project) 中使用 DLL(动态库)
- Unity里使用包含C#事件的Dll
- 使用.NET Reflector 查看Unity引擎里面的DLL文件
- 在Unity中使用动态库(Mono DLL)
- mac OS或window VS生成dll供unity使用
- 在Dll中使用Unity预编译宏的问题
- 关于unity使用dll和c#扩展方法的小结
- Unity与dll不得不说(编译-使用-反编译-混淆)
- Android Media(拍照)
- 让我试验一下
- 自用(Spring 和Hibernate 整合笔记和jar包)
- Activity是什么
- vector用法
- Unity使用DLL
- socket.io与node.js api 调用示例
- hdu 1873 看病要排队(优先队列)
- Kotlin for Android(五)Kotlin类和对象
- 串的基本操作(C++)
- E
- VS2015快捷键
- [FWT] 51nod 算法马拉松26 A. A国的贸易
- 一道android笔试题目