unity-调用动态库dll-windows篇


1、生成64位的dll

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

        #ifndef __NativeCode_H__
        #define __NativeCode_H__
    
        #ifndef EXPORT_DLL
        #define EXPORT_DLL __declspec(dllexport) //导出dll声明
        #endif
    
        extern "C" {
            EXPORT_DLL int MyAddFunc(int _a, int _b);
        }
        #endif

    NaviteCode.cpp

    
    #include "NaviteCode.h"
    
    
    extern "C" {
        int MyAddFunc(int _a, int _b)
        {
            return _a + _b;
        }
    }

    extern “C” 就不用说明了吧,指定c编译器编译,后面打Android的so库也是用相同的代码

  2. 修改vs导出配置,导出为64位Release的dll(应为用的是unity是64位的)

    这里写图片描述

    这里写图片描述

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


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

  • unity存放动态库是由规则的不同平台放置的目录不同,官网传送门,点我

  • 所以按照规则,把 NativeCode.dll 放入 Assets\Plugins\x86_64 目录中


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

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices; //DllImport需要的namespace

public class testDll : MonoBehaviour {

    [DllImport("NativeCode")] //这里就是调用的dll名字
    public static extern int MyAddFunc(int x, int y);

    // Use this for initialization
    void Start () {
        int ret = MyAddFunc(200, 200);
        Debug.LogFormat("--- ret:{0}", ret);
    }
}

done
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值