Unity3D 能够很方便的集成一些外部插件,以便调用现有的动态链接库。下面就给大家介绍Unity中调用动态库dll的方法。其实就是c#调用dll。

一、生成64位的dll

1.用vs新建个工程,随便写个函数
NaviteCode.h

AخA
 
1
    #ifndef __NativeCode_H__
2
    #define __NativeCode_H__
3
    #ifndef EXPORT_DLL
4
    #define EXPORT_DLL __declspec(dllexport) //导出dll声明
5
    #endif
6
    extern "C" 
7
        EXPORT_DLL int MyAddFunc(int _a, int _b);
8
    
9
    #endif

NaviteCode.cpp

7
 
1
#include "NaviteCode.h"
2
extern "C" 
3
    int MyAddFunc(int _a, int _b)
4
    
5
        return _a + _b;
6
    
7
extern “C” 就不用说明了吧,指定c编译器编译,后面打Android的so库也是用相同的代码

2.修改vs导出配置,导出为64位Release的dll(应为用的是unity是64位的)
Unity调用动态库dll(windows篇)

Unity调用动态库dll(windows篇)

3.生成,就出来了这个 NativeCode.dll

二、拷贝 NativeCode.dll 到unity工程中

unity存放动态库是由规则的不同平台放置的目录不同。
所以按照规则,把 NativeCode.dll 放入 Assets\Plugins\x86_64 目录中

三、c#中调用一下,随便create个c#挂在场景的对象中

12
 
1
using UnityEngine;
2
using System.Collections;
3
using System.Runtime.InteropServices; //DllImport需要的namespace
4
public class testDll : MonoBehaviour 
5
    [DllImport("NativeCode")] //这里就是调用的dll名字
6
    public static extern int MyAddFunc(int x, int y);
7
    // Use this for initialization
8
    void Start () 
9
        int ret = MyAddFunc(200, 200);
10
        Debug.LogFormat("--- ret:{0}", ret);
11
    
12
Unity调用动态库dll(windows篇)
www.nainaitea.com

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2021-10-25
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-27
  • 2021-05-22
相关资源
相似解决方案