分享

在Linux系统中,通过Unity代码将某个目录下的所以文件拷贝到系统路径下

 鸿蛟家平 2023-10-19 发布于江苏

这个需求是由于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
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多