1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System.Diagnostics;
- using System.IO;
- using UnityEngine;
- public class JavaScriptRunner : MonoBehaviour
- {
- void Start()
- {// 在 Android 平台上执行安装脚本
- #if UNITY_ANDROID && !UNITY_EDITOR
- string nodePath = Path.Combine(Application.streamingAssetsPath, "node-v14.21.3-linux-x64/bin/node");
- string installScript = $"echo \"export PATH=\\\"$PATH:{nodePath}\\\"\" >> ~/.bashrc";
- string[] arguments = { "-c", installScript };
- Process.Start(new ProcessStartInfo("/system/bin/sh", string.Join(" ", arguments)));
- #endif
- // 启动 Node.js 进程
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = "node"; // Node.js 可执行文件的名称或路径
- startInfo.Arguments = Application.streamingAssetsPath+ "/js/build/index.js"; // 要运行的脚本文件的名称或路径
- startInfo.UseShellExecute = false;
- startInfo.RedirectStandardOutput = true;
- startInfo.RedirectStandardError = true;
- startInfo.CreateNoWindow = true;
- Process process = new Process();
- process.StartInfo = startInfo;
- process.OutputDataReceived += OnOutputDataReceived;
- process.ErrorDataReceived += OnErrorDataReceived;
- process.Start();
- process.BeginOutputReadLine();
- process.BeginErrorReadLine();
- }
- void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
- {
- // 处理标准输出
- UnityEngine.Debug.Log(e.Data);
- }
- void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
- {
- // 处理错误输出
- UnityEngine.Debug.LogError(e.Data);
- }
- }
|