68 lines
1.6 KiB
C#
68 lines
1.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class UnitySubApp : MonoBehaviour
|
|
{
|
|
[SerializeReference,SubclassSelector] private IReference path;
|
|
[SerializeReference,SubclassSelector] private IReference arguments;
|
|
[SerializeField] private bool isStreamingAssets;
|
|
|
|
private void Start()
|
|
{
|
|
Execute();
|
|
}
|
|
public void Execute()
|
|
{
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName =isStreamingAssets ? Path.Combine(Application.streamingAssetsPath, path.Value) : path.Value,
|
|
WorkingDirectory =Environment.CurrentDirectory,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError =true,
|
|
StandardErrorEncoding = System.Text.Encoding.GetEncoding("gb2312"),
|
|
//StandardInputEncoding = System.Text.Encoding.GetEncoding("gb2312"),
|
|
StandardOutputEncoding = System.Text.Encoding.GetEncoding("gb2312"),
|
|
};
|
|
if(arguments != null)
|
|
{
|
|
startInfo.Arguments = arguments.Value;
|
|
}
|
|
var process = new Process()
|
|
{
|
|
StartInfo = startInfo,
|
|
};
|
|
|
|
process.OutputDataReceived += (sender, args) =>
|
|
{
|
|
BIT4Log.Log<UnitySubApp>(args.Data);
|
|
};
|
|
process.ErrorDataReceived += (sender, args) =>
|
|
{
|
|
BIT4Log.Warning<UnitySubApp>(args.Data);
|
|
};
|
|
|
|
BIT4Log.Log<UnitySubApp>($"已创建进程:{startInfo.FileName}");
|
|
|
|
process.Start();
|
|
|
|
process.BeginErrorReadLine();
|
|
process.BeginOutputReadLine();
|
|
|
|
destroyCancellationToken.Register(()=>
|
|
{
|
|
if(process.HasExited is false)
|
|
process.Kill();
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|