unity调用C++动态链接库dll

unity调用C++的dll。传入摄像头的图片,传出三个参数。

1.C++要设置release X64版本的,生成的dll文件要放到Assets的plugins文件夹内(没有自己新建)
2.因为unity和opencv的坐标原点不同,所以要flip(一个左上角一个左下角);通道不同,所以要用CV_RGBA2BGR转换
3.如果像是x,y,z这种要传回来的指针,需要在c#中加ref
4.在C#中调用dll要先申明如下
[DllImport("PHOTOJ")]
    private static extern void Unity2OpenCVImage(IntPtr data, int width, int height,ref float x,ref float  y,ref float z);
5.memcpy图片数据是要width*heiget*4,因为mat和webCamTexture.GetPixels32()都是4通道
6.我暂时就想到这么多。。
   

   C++头文件

extern "C" _declspec(dllexport)void Unity2OpenCVImage(char* imageData, int width, int height , float*x, float*y, float*z);


    C++ cpp文件

#include<iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include "main.h"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

extern "C" _declspec(dllexport) void Unity2OpenCVImage(char* imageData, int width, int height, float*x,float*y,float*z)
{
	if (NULL == imageData)
	{
		return;
	}
	cv::Mat opencvImage(height, width, CV_8UC4);

	
	
	memcpy(opencvImage.data, imageData, width*height*4);
	
	cvtColor(opencvImage, opencvImage, CV_RGBA2BGR);
	
	flip(opencvImage, opencvImage, 0);
	 
	cv::imshow("11",opencvImage);  
<span style="white-space:pre">	</span> cv::waitKey(1); 
	*x = 0;
	*y = 0;
	*z = 0;
}
C#代码

using UnityEngine;
using System.Collections;
using OpenCVForUnity;
using System.Runtime.InteropServices;
using System;

public class opcnecvtest : MonoBehaviour {
    public WebCamTexture webCamTexture;
    int width = 640;
    int height = 480;
    Mat rgbaMat;
    private string cameraName = "";
    public float x =1;
    float y;
    float z;
    IntPtr pixelPointer;
    bool show = false;

    // Use this for initialization
    [DllImport("PHOTOJ")]
    private static extern void Unity2OpenCVImage(IntPtr data, int width, int height,ref float x,ref float  y,ref float z);
   
    void Start () {
        StartCoroutine (Test());
        

    }
     IEnumerator Test()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if(Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
<span style="white-space:pre">	</span>//开启摄像头
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            webCamTexture = new WebCamTexture(cameraName, width, height, 15);
            webCamTexture.Play();
            

        }
    }

    // Update is called once per frame
    void Update () {
//用opencv中的imshow函数显示unity调用的摄像头捕捉到的画面
        if (show)
        {
            Color32[] data = new Color32[width * height];
            data = webCamTexture.GetPixels32();           
            GCHandle pixelHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            pixelPointer = pixelHandle.AddrOfPinnedObject();
//分别表示传入图片data,图片宽度,图片高度,x,y,z,后面的三个参数是要在dll中赋值后传回来的,所以前面要加ref,表示传入地址        
            Unity2OpenCVImage(pixelPointer, width, height, ref x, ref y, ref z);
            pixelHandle.Free();
        }

    }
   void OnGUI()  
    {
        
        if (GUI.Button(new UnityEngine.Rect(50, 50, 100, 100), "test"))
        {
            show = !show;
            Debug.Log(x + y + z);//如果得数为1,表示没有成功调用dll中的函数,如果为0,结果正确

        }
        GUI.DrawTexture(new UnityEngine.Rect(200, 200, 400, 300), webCamTexture, ScaleMode.ScaleToFit);
        
    }
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值