unity+ndk+android+.so+apk r囧r小猫 2022-02-16 15:20 275阅读 0赞 系统环境:**win10** \+ **Unity 2018.3.13f1 (64-bit)** # 1. [安装NDK][NDK] # 解压下载后的文件夹,将文件夹路径添加到系统环境变量Path中 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70] # 2. 编写文件 # makeSo文件夹名称可以修改,但jni不要修改 ![在这里插入图片描述][20190422170351353.png] 参考链接:[https://docs.unity3d.com/Manual/PluginInspector.html][https_docs.unity3d.com_Manual_PluginInspector.html] [Android.mk][] LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := NativeCode LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_SRC_FILES := NaviteCode.cpp LOCAL_LDLIBS := -llog -landroid LOCAL_CFLAGS := -DANDROID_NDK include $(BUILD_SHARED_LIBRARY) [Application.mk][] APP_STL := c++_static APP_CPPFLAGS := -frtti -std=c++11 APP_PLATFORM := android-19 APP_CFLAGS += -Wno-error=format-security APP_BUILD_SCRIPT := Android.mk APP_ABI := armeabi-v7a x86 NaviteCode.cpp #include "NaviteCode.h" extern "C" { int MyAddFunc(int _a, int _b) { return _a + _b; } } NaviteCode.h #ifndef __NativeCode_H__ #define __NativeCode_H__ extern "C" { int MyAddFunc(int _a, int _b); } #endif # 3. ndk-build # Win\+r:输入cmd ![在这里插入图片描述][2019042217122371.png] 会生成**obj libs**两个文件夹 ![在这里插入图片描述][20190422171249536.png] # 4. 新建unity工程 # 将libs文件夹复制到Assets/Plugins/Android,没有则自己创建 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70 1] show.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; public class show : MonoBehaviour { [DllImport("NativeCode")] public static extern int MyAddFunc(int x, int y); // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { gameObject.transform.Translate(new Vector3(0, 0, MyAddFunc(1, 1))); } } 将show.cs绑定到一个物体中,实现运动功能 # 5.生成apk # file->Build Settings ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70 2] # 6.注意事项 # 不论怎么选择,editor中是无法运行程序的,会一直报错**dllnotfoundexception**,发布成apk在安卓真机或模拟器上运行即可 [NDK]: https://developer.android.com/ndk/downloads/index.html?hl=zh-cn#download [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70]: /images/20220216/a72527fc975b49b4995237e87590c3d0.png [20190422170351353.png]: /images/20220216/1b067c0a5c8a47e9b8afb5f22b633082.png [https_docs.unity3d.com_Manual_PluginInspector.html]: https://docs.unity3d.com/Manual/PluginInspector.html [Android.mk]: http://Android.mk [Application.mk]: http://Application.mk [2019042217122371.png]: /images/20220216/12b8fe5d8e7e48abb607e84a17c33dd0.png [20190422171249536.png]: /images/20220216/c7b6a98ba77e42b6bfc20966cca5e07e.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70 1]: /images/20220216/c2b4b225bd0d42ce90dd55495e454dac.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxpZmVf_size_16_color_FFFFFF_t_70 2]: /images/20220216/dc0753d482f7410497639c2df464a767.png
还没有评论,来说两句吧...