当前位置:   article > 正文

unity 调用C++库(android+jni+window dll平台实现)_android jni调用dll

android jni调用dll

方便不知道如何再unity调用各平台动态库的同学参考,实测可用:

window案例


动态库名

tracker_model.dll

位置

unity工程 Assets\Plugins目录下

c# 关键调用

代码 

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

//[StructLayout(LayoutKind.Sequential)]
public struct Point3F {
 public float x;
 public float y;
 public float z;
}

public class Control : MonoBehaviour {

System.IntPtr center_data_prt;

System.IntPtr fingertips_data_prt;

[DllImport("tracker_model")]
 public static extern long points_init (string iin);

........

void Start () {
  handle = points_init(fileName);

...................

center_data_prt = Marshal. AllocHGlobal(Marshal.SizeOf(typeof(Point3F)));

fingertips_data_prt = Marshal. AllocHGlobal(Marshal.SizeOf(typeof(Point3F))*maxFingerTips);

tri_cnt = points_get_fingertips(handle, center_data_prt, fingertips_data_prt


}

 void Update () {

Point3F center = (Point3F)Marshal.PtrToStructure(
    (System.IntPtr)(center_data_prt.ToInt64()),typeof(Point3F));


for (int i = 0; i < tri_cnt; i++) {
    Point3F point = (Point3F)Marshal.PtrToStructure(
     (System.IntPtr)(fingertips_data_prt.ToInt64()+i*Marshal.SizeOf(typeof(Point3F))),typeof(Point3F));

}

}

void OnDestroy(){

Marshal.FreeHGlobal(center_data_prt);

Marshal.FreeHGlobal(fingertips_data_prt);

}


对应tracker_model.dll c++头文件接口

#ifndef _model_api_H_
#define _model_api_H_

#ifdef WIN32
#ifdef HTM_DLL_EXPORT
#define HTM_API extern "C" _declspec(dllexport)
#else
#define HTM_API extern "C"  _declspec(dllimport)
#endif
#else
#define HTM_API
#endif

struct Point3F
{
 float x;
 float y;
 float z;
};

HTM_API long points_init(const char *config_file);

HTM_API int  points_get_tri(long handle, Point3F* triangle_vertex_list, int list_len);

~~~~~~~

具体动态库编译过程不叙述



Android jni

(android ndk 编译c++库过程 具体不叙述  jni工程制作也不叙述,网上有  )

jni方法:
/*编译接口类:
 *
 * http://www.2cto.com/kf/201409/330892.html
 *
进入项目文件夹, 生成JNI的头文件, 使用命令:
javah -classpath bin/classes -d jni com.example.hellomyjni.JniClient
命令解析:
javah 生成头文件;
-classpath 使用类的位置(bin/classes), 都是.class文件;
-d jni 需要生成JNI的类(com.example.hellomyjni.JniClient), 包括[package].[classname].
按F5刷新项目, 项目会自动生成jni文件夹, 并包含一个头文件com_example_hellomyjni_JniClient.h.
右键点击项目在Android Tools里面点击Add NativeSupport, 就会自动生成: HelloMyJni.cpp和Android.mk.
*/

1 unity使用c#调用android函数案例

 制作unity的android插件(具体流程百度)
 创建完插件和目录后 unity工程下有Assets\Plugins\Android\libs目录



2  eclipse下利用jni制作c++库如下

</
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/810492
推荐阅读
相关标签
  

闽ICP备14008679号