unity3D 调用android .so

来源:互联网 发布:淘宝清洗订单会降权吗 编辑:程序博客网 时间:2024/06/04 19:56

首先明白.so分为两种,一种是linux下的.so,一种是NDK下的.so;本文主要讲解加载后一种.so

先来了解如何生成.so;

1、建立jni文件夹,创建android.mk和main.cpp文件

.mk内容如下:

LOCAL_PATH := $(call my-dir)# compile VitClient.soinclude $(CLEAR_VARS)   LOCAL_MODULE    := libgame   //.so名称LOCAL_SRC_FILES := main.cpp //需要直接的.cpp/.c文件LOCAL_C_INCLUDES := $(LOCAL_PATH)/../includeinclude $(BUILD_SHARED_LIBRARY)#include $(BUILD_STATIC_LIBRARY).cpp 文件内容:extern "C"{    int AddNum(int a,int b)    {        return a+b;    }}

2、在jni同级目录创建build_native.sh脚本

内容如下:

# set paramsNDK_ROOT_LOCAL=/Users/wuwuyuan/Desktop/android-sdk-macosx/android-ndk-r7b   //ndk 路径 设置成自己的# build# NDK 编译参数# V=1    显示编译详细信息# -B    强制重新编译$NDK_ROOT_LOCAL/ndk-build 


3、mac下命令行   执行 sh build_native.sh   结果如图:


4、在unity中穿件android工程,新建Plugins/Android   将libgame.so 放在之下

5、编辑C# 脚本:

using System.Runtime.InteropServices;public class PluginImport : MonoBehaviour {    [DllImport ("game")]  //省略掉lib前缀和.so 后缀    private static extern int AddNum(int a,int b);       void Start () {        Debug.Log("wwwwwww:"+AddNum(1,1));    }}

6、在android设备上可以看到打印出 wwwwwww:2

备注:必须在android真机或模拟器上能看到效果,在pc或mac上会报异常

工程下载地址

 补充:如何在mac下用NDK编.so

cd 到当前eclipse工程根目录,在根目录执行../android-ndk-r7b/ndk-build

编译过程:

Compile++ arm    : shadowmapping <= BaseGLSLProgram.cppCompile++ arm    : shadowmapping <= bitmap.cppCompile++ arm    : shadowmapping <= button.cppCompile++ arm    : shadowmapping <= main.cppCompile thumb  : android_native_app_glue <= android_native_app_glue.cStaticLibrary  : libandroid_native_app_glue.aPrebuilt       : libgnustl_static.a <= <NDK>/sources/cxx-stl/gnu-libstdc++/libs/armeabi/

原文地址:http://www.cnblogs.com/U-tansuo/archive/2013/04/02/unity3d_Plugin_android_so.html

0 0
原创粉丝点击