Unity使用native读取streamingasset里文件
需求是,使用native方式,读取apk包里的lua代码,读进c#,做解密
一准备unity工程
public class GameMain : MonoBehaviour {
public const string libName = "TGNative";
public Text content;
[DllImport(libName)]
public static extern int ReadAsset(string fileName, byte[] buffer, int size);
// Use this for initialization
void Start () {
Debug.Log("Start################");
var size = * ;
var buffer = new byte[size];
var num = ReadAsset("test.txt", buffer, size);
Debug.Log("ReadAsset ################"+ num);
if (num > )
{
Debug.Log("Read succ");
var str = UTF8Encoding.UTF8.GetString(buffer,, num);
content.text = str;
Debug.Log(str);
}
}
// Update is called once per frame
void Update () {
}
}
二unity导出android工程

注意playersetting 里设置包名
三android studio 打开导出的工程

四 添加代码
UnityPlayerActivity里

新增TGNative.java,增加native方法
public native static void InitAssetManager( AssetManager am );
五使用javah生成头文件
javah -d jni -classpath D:\work\ndkread\AssetMgr\AndroidAssetMgr\NativeAssetMgr\build\intermediates\classes\debug;C:\Users\Topjoy\AppData\Local\Android\Sdk\platforms\android-\android.jar com.topjoy.JniTest.TGNative
六新增NativeUtil.c和Android.mk Applicatioin,mk ,build.bat
build.bat
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk APP_PLATFORM=android-
NativeUtil.c
//
// Created by Topjoy on 2018/12/7.
//
#include <jni.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include "com_topjoy_JniTest_TGNative.h" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"Unity",__VA_ARGS__) // 定义LOGD类型
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"Unity",__VA_ARGS__) static AAssetManager* mgr = NULL;
JNIEXPORT void JNICALL Java_com_topjoy_JniTest_TGNative_InitAssetManager
(JNIEnv * env, jclass tis, jobject assetmanager){
mgr = AAssetManager_fromJava(env, assetmanager);
} extern int ReadAsset(char* fileName, char* buffer, int size)
{
if(mgr == NULL)
{
LOGW( "mgr is null" );
return ;
}
AAsset* asset = AAssetManager_open(mgr, fileName, AASSET_MODE_UNKNOWN);
if( asset == NULL )
{
LOGW( "open asset (%s) failed" , fileName);
return ;
}
int len = (int)AAsset_getLength(asset);
if(len >size)
{
LOGW("Buffer less than length");
return ;
}
int nread = AAsset_read(asset, buffer, size);
AAsset_close(asset);
if(nread != len)
{
LOGW("Read num %d not equal length %d", nread, len);
return ;
}
return nread;
}
七运行build.bat 生成so文件

八使用jar命令生成jar文件
@echo off
set app=%cd%
set debugp=%cd%\build\intermediates\classes\debug
set classp=%debugp%\com\topjoy\JniTest
cd %classp% pause
for %%x in (*) do (
if not "%%x"=="UnityPlayerActivity.class" (
if not "%%x"=="TGNative.class" (
del %%x
)
)
) cd %debugp%
jar -cvf class.jar com move class.jar %app%\
九把生成的class.jar 和so文件拷贝进unity工程

十生成apk,拷贝进木木测试

成功!
Unity使用native读取streamingasset里文件的更多相关文章
- unity Android在streamingAssets路径下文件无法读取的的解决方法
unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...
- JAVA读取本地html文件里的html文本
/** * 读取本地html文件里的html代码 * @param file File file=new File("文件的绝对路径") * @return */ public s ...
- Unity Assets目录下的特殊文件夹名称
1.隐藏文件夹以.开头的文件夹会被Unity忽略.在这种文件夹中的资源不会被导入,脚本不会被编译.也不会出现在Project视图中.2.Standard Assets在这个文件夹中的脚本最先被编译.这 ...
- 【Unity基础知识之三】Unity Assets目录下的特殊文件夹名称
Unity3D的特殊目录名称 Unity预留了一些目录名称,这些目录有着特殊的含义.比较重要的有: Resources这个目录下的所有文件都会被打包到发布版本中,程序可以通过文件路径来访问它们.这 ...
- JNI读取assets资源文件
源自:http://www.rosoo.net/a/201112/15459.html assets目录底下的文件会被打包到一个apk文件里,这些资源在安装时他们并没被解压,使用时是直接从apk中读取 ...
- (转)Unity Assets目录下的特殊文件夹名称(作用和是否会被打包到build中)
原文:http://wiki.unity3d.com/index.php/Special_Folder_Names_in_your_Assets_Folder 1.隐藏文件夹以.开头的文件夹会被Uni ...
- 在Unity中使用LitJson解析json文件
LitJson 这个库需要找资源,找到LitJson.dll后将它放在Assets文件夹下,在脚本中使用using引入即可 测试代码 json文件: {"Archice":[{&q ...
- 如何读取Access里的OLE类型的图片
身份证一类读卡器读取的照片信息,保存在Access数据库中一般为OLE型字段,图片为BMP格式,因为是用其读卡器写入的,其数据类型为常二进制数据. 再用报表或EXCEL读取这些图片时,如果将该图片字段 ...
- 不可或缺 Windows Native (10) - C 语言: 文件
[源码下载] 不可或缺 Windows Native (10) - C 语言: 文件 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 文件 示例cFile.h #ifn ...
随机推荐
- C#绑定事件时使用匿名函数
当使用一些临时的函数 可以预知这些函数基本不会被复用时 可以使用匿名函数简化代码 public static void startCoupons() { //绑定一些事件 userGetCoupon ...
- TCP/IP ---分层
TCP/IP的分层 ICMP是IP协议的附属协议.IP层用它来与其他主机或路由器交换错误报文和其他重要信息.尽管ICMP主要被IP使用,但应用程序也有可能访问它.我们将分析两个流行的诊断工具,Ping ...
- git 命令使用速查手册( 个人版)
1. 克隆远程库 git clone repository_address 通过 git clone 获取的git库只是远程库中的当前工作分支,如果想获取其它分支信息,可参考下面. 2. 查看远程 ...
- 在训练CNN时,loss稳定在log(类别数)
参见知乎问题! https://www.zhihu.com/question/275774218 很多框架都会有一个问题,当卷积 weight NaN 之后,卷积的 output 会变成 NaN.然后 ...
- ping的原理以及ICMP
ping 的原理: ping 程序是用来探测主机到主机之间是否可通信,如果不能ping到某台主机,表明不能和这台主机建立连接. ping 使用的是ICMP协议,它发送icmp回送请求消 ...
- Linux Tools
WinSCP http://winscp.net/eng/download.php Xshell 5
- Atitit.eclipse comment template注释模板
Atitit.eclipse comment template注释模板 1. Code templet1 1.1. Settpath1 1.2. 设置存储1 1.3. 导出设置1 2. Java d ...
- PILE读书笔记_进程环境
进程是操作系统运行程序的一个实例, 也是操作系统分配资源的单位. 在Linux环境中, 每个进程都有独立的进程空间, 以便对不同的进程进行隔离, 使之不会互相影响. atexit函数 #include ...
- java json字符串和对象互转
/** * Created by admin on 2017/7/26. */ public class NewPost { private String title; private String ...
- Lucene.Net 介绍
1 lucene简介1.1 什么是lucenepowered by 25175.netLucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desk ...