这个需求是由于unity调用了某个so库文件,这个so文件需要系统中有其他库文件,所以需要先判断系统中有没有这个库文件,没有的话就需要来拷贝。由于拷贝文件到系统目录下,需要有权限,所以我采取的办法是先判断系统有没有所需的库文件,没有的话弹出terminal窗口,让用户输入sudo密码,输入之后再自动将我的StreamingAssets目录中我的so文件拷贝进去。下面是我的代码: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Diagnostics;
public class test : MonoBehaviour
{
void Start()
{
#region
string targetFilePath = "/usr/lib/";
if (!File.Exists(targetFilePath+"/libsdkclient.so.0")){
UnityEngine.Debug.Log("test.so文件不存在");
string sourceFilePath = Application.streamingAssetsPath+"/sdk_linux_lib/*";
UnityEngine.Debug.Log(sourceFilePath);
UnityEngine.Debug.Log(Application.streamingAssetsPath);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
// 设置启动信息
startInfo.FileName = "gnome-terminal"; // 使用你的终端程序名称
startInfo.Arguments =$"-x bash -ic 'echo \"请输入sudo密码:\" ; sudo cp -r {sourceFilePath} {targetFilePath} && sleep 1 ; echo \"文件复制完成\" && read -p \"按 Enter 键关闭终端窗口...\"'";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
// 启动终端窗口并执行命令
process.Start();
process.WaitForExit();
}else{
UnityEngine. Debug.Log("库文件存在");
}
#endregion
}
}
|