unity中调用dll文件总结

unity中调用dll文件总结

根据收集的资料,对unity中调用dll文件进行总结,目前常用的两种,在给出vs中封装dll文件的步骤。

一、调用c#中的dll文件

 

1.1封装dll文件

 

首先新建一个项目

然后创建一个类库,例如命名为Csharp

 

 

 

 

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Threading.Tasks;

  6.  
  7. namespace Csharp

  8. {

  9. public class Class1

  10. {

  11. public static string getName(string name)

  12. {

  13. return name;

  14. }

  15. }

  16. }

 

然后编译成dll文件,名字为Csharp.dll

1.2.在unity中使用自定义的dll组件

在unity中创建一个Plugins文件夹,所有的外部引用的dll组件必须要放在这个文件下,才能被using。如果是C#封装的dll,就用 using的方式引用,如果是C++的dll,就DllImport[""]的方式来添加对dll的引用。然后我在C#脚本中用这个dll

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using Csharp; //引用c#生成的dll文件,namespace的名字

  4.  
  5. public class TestDllOne : MonoBehaviour {

  6. // Use this for initialization

  7. void Start () {

  8.  
  9. }

  10.  
  11. // Update is called once per frame

  12.  
  13. void OnGUI()

  14. {

  15. if (GUILayout.Button("test c# dll"))

  16. {

  17. Debug.Log(Class1.getName("hello world "));

  18. }

  19.  
  20. }

 

二、调用c++中的dll文件

在本文中用VS2013进行 C++创建DLL图解

1.创建项目: Win32->Win32项目,名称:MyDll


.

 

 

单击下一步后选择如下图所示

 

单击完成后,右击选择头文件-->添加 ----> 新建项如下图操作

 

选择头文件并且命名,例如TestDll.h,命名后单击添加

 

 

在TestDll.h 头文件中写入代码如下

 

 
  1. # define _DLLExport __declspec (dllexport)

  2. # else

  3. # define _DLLExport __declspec (dllimport)

  4. #endif

  5.  
  6. extern "C" int _DLLExport MyADD(int x, int y);

 

 

 

选择源文件,如下图所示

 

 

选择添加后,在Test.cpp 源文件中写入代码如下

 

 
  1. //宏定义

  2. #define EXPORTBUILD

  3.  
  4. //加载头文件

  5. #include "TestDll.h"

  6.  
  7. //设置函数

  8. int _DLLExport MyADD(int x, int y)

  9. {

  10. return x + y;

  11. }


编译后将生成的MyDll.dll 文件导入unity中的Plugins文件中

 

如果是C++的dll,就DllImport[""]的方式来添加对dll的引用

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Runtime.InteropServices; //调用c++中dll文件需要引入

  4. public class TestDllOne : MonoBehaviour {

  5. [DllImport("MyDll")]

  6. static extern int MyADD(int x , int y);

  7.  
  8. // Use this for initialization

  9. void Start () {

  10.  
  11. }

  12.  
  13. // Update is called once per frame

  14.  
  15. void OnGUI()

  16. {

  17. if (GUILayout.Button("test c++ dll"))

  18. {

  19. int i = MyADD(12 , 23);

  20. Debug.Log("sum = :" + i.ToString());

  21. }

  22. }

  23.  
  24.  
  25. }

 

 

使用c#生产的dll会有如下问题:

 

Running into Issues?:

Internal compiler error ... System.Reflection.ReflectionTypeLoadException

 

If you are getting an error similar to Internal compiler error. System.Reflection.ReflectionTypeLoadException in Unity when trying to build your project; You need to change the targeted .NET version of your C# file to something like .NET 3.5.

  • Right click on your C# project and go to Properties
  • In the Application tab, change the Target Framework to .NET Framework 3.5
     

Failed to load ... expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386.

If you are getting this error, you probably need to recompile your libraries for x64 platform (64 bit).

In Visual Studio, go to the properties of your project, then at the top click the "Configuration Manager..." button. In the table, under the Platform column, change it to "x64" then recompile your project.

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity调用DLL有几种常见的方法。以下是其的两种方法: 1. 使用DllImport特性: - 将DLL文件放置在Unity项目的Assets/Plugins文件夹下(如果没有该文件夹,可以手动创建)。 - 在C#脚本,使用[System.Runtime.InteropServices.DllImport]特性声明要调用DLL函数。例如: ```csharp using System.Runtime.InteropServices; public class MyScript : MonoBehaviour { [DllImport("mydll")] // 替换为实际的DLL名称 private static extern void MyDllFunction(); void Start() { MyDllFunction(); // 调用DLL函数 } } ``` 2. 使用Unity的插件系统: - 创建一个新的C#类,然后将其放置在Unity项目的Assets/Editor文件夹下。 - 在该类,使用UnityEditor命名空间下的PluginImporter类来导入DLL并设置相关属性。例如: ```csharp using UnityEditor; public class MyDllImporter : AssetPostprocessor { private void OnPreprocessAssembly() { if (assetPath.Contains("mydll")) // 替换为实际的DLL名称 { PluginImporter pluginImporter = (PluginImporter)assetImporter; pluginImporter.SetCompatibleWithEditor(true); pluginImporter.SetCompatibleWithAnyPlatform(false); pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, true); // 替换为实际的目标平台 } } } ``` - 在Unity编辑器,导入DLL文件,并确保在Inspector窗口设置了正确的平台兼容性。 请注意,调用DLL可能涉及到特定的函数签名和参数传递方式,具体取决于DLL的实现。因此,请确保你了解DLL的使用方法和要求,并按照其提供的文档进行调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值