java的代码:https://blog.csdn.net/zhangminflying/article/details/22098779
private void copyDir(final Context activity, String sourcePath, String destPath) {
Log.i("sourcepath="+sourcePath, "destpath="+destPath);
String[] files;
try {
files = activity.getAssets().list(sourcePath);
if (files.length == 0)
{
copyFile(activity, sourcePath, destPath);
}
else
{
String filePath = destPath +"/"+sourcePath;
File file = new File(filePath);
file.mkdirs();
for (int i = 0; i < files.length; i++)
{
String[] files2 = activity.getAssets().list(sourcePath+"/"+files[i]);
if (files2.length == 0)
{
copyFile(activity, sourcePath + "/" + files[i], destPath);
}
else
{
copyDir(activity, sourcePath + "/" + files[i], destPath);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void copyFile(final Context activity, String sourcePath, String destPath) {
Log.v("file", sourcePath);
InputStream in = null;
FileOutputStream out = null;
try {
String filePath = destPath+"/"+sourcePath;
File file = new File(filePath);
if (!file.exists())
{
in = activity.getAssets().open(sourcePath);
out = new FileOutputStream(file);
int length = -1;
byte[] buf = new byte[1024];
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
out.flush();
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
C#这边的调用:
public void CopyFile()
{
AndroidJavaObject androidObject = new AndroidJavaObject("com.cf.perfectsdk.api");
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
if (activity == null)
return;
androidObject.Call("copyDir", activity, "lua", Application.persistentDataPath);
}
然后是执行luac转bytes的代码:
#!/bin/bash
srcPath=$(pwd)/../Assets/Scripts/lua
destPath=$(pwd)/../Assets/StreamingAssets/lua
luaPath=$(pwd)/lua53/luac.exe
if [ -d $destPath ]
then
rm -rf $destPath
fi
mkdir $destPath
function genluabytes(){
for file in ` ls $1 `
do
if [ -d $1"/"$file ]
then
srcDir=$1"/"$file
descDir=${srcDir/Scripts/StreamingAssets}
mkdir $descDir
genluabytes $1"/"$file
else
filename=$1"/"$file
if [[ ! $filename =~ \.meta$ ]];then
srcFile=$1"/"$file
destFile=${srcFile/Scripts/StreamingAssets}
#echo $srcFile
#echo $destFile
$luaPath -o $destFile $srcFile
fi
fi
done
}
genluabytes $srcPath
完整的C#代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;
using System.IO;
using UnityEngine.UI;
namespace Tutorial
{
public class CSCallLua : MonoBehaviour
{
public Text m_text;
private AndroidJavaObject m_javaObject;
LuaEnv luaenv = null;
public class DClass
{
public int f1;
public int f2;
}
[CSharpCallLua]
public delegate int FDelegate(int a, string b, out DClass c, ref int x, ref string y);
void Start()
{
m_javaObject = new AndroidJavaObject("com.cf.perfectsdk.MainActivity");
luaenv = new LuaEnv();
luaenv.AddLoader(SelfDefineLoader);
luaenv.DoString("require 'hello'");
}
public byte[] SelfDefineLoader(ref string filepath)
{
filepath = filepath.Replace('.', '/');
filepath = Application.persistentDataPath + "/lua/" + filepath + ".lua.txt";
Debug.Log(filepath);
if (File.Exists(filepath))
{
return File.ReadAllBytes(filepath);
}
else
{
return null;
}
}
void OnDestroy()
{
luaenv.Dispose();
}
public void DoFile(string lua, LuaTable env = null)
{
string path = Application.streamingAssetsPath + "/lua/" + lua + ".lua.txt";
var txt = File.ReadAllText(path);
luaenv.DoString(txt, lua, env);
}
public void Add()
{
int add = m_javaObject.Call<int>("add", 1, 2);
m_text.text = "add=" + add;
}
public void SayHello()
{
AndroidJavaObject androidObject = new AndroidJavaObject("com.cf.perfectsdk.api");
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
if (activity == null)
return;
string str = androidObject.Call<string>("Unzip", activity);
m_text.text = str;
}
public void CopyFile()
{
AndroidJavaObject androidObject = new AndroidJavaObject("com.cf.perfectsdk.api");
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
if (activity == null)
return;
androidObject.Call("copyDir", activity, "lua", Application.persistentDataPath);
}
}
}
|