如何在Unity3d中运行进程时执行某些操作
我正在运行一个统一的进程,需要一些时间(实际上可能需要30分钟),但是我希望统一运行它最多5分钟如果没有输出,则返回。 我也想在这个等待5分钟的时间里展示这样的东西如何在Unity3d中运行进程时执行某些操作
任何人都有一个想法如何做到这一点?我试着用这行代码
myProcess.WaitForExit(1000 * 60 * 5);
,但我不能做任何事情,而它的等待,我想这是我阻止或东西,任何人可以帮助?
编辑:
public void onClickFindSol(){
paused=true;
ReadRedFace();
ReadGreenFace();
ReadBlueFace();
ReadYellowFace();
ReadOrangeFace();
ReadWhiteFace();
if (File.Exists (path))
File.Delete (path);
System.IO.File.WriteAllText(path,InputToAlgo);
myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.FileName = (System.Environment.CurrentDirectory)+Path.DirectorySeparatorChar+"rubik3Sticker.ida2";
myProcess.EnableRaisingEvents = true;
myProcess.StartInfo.WorkingDirectory = (System.Environment.CurrentDirectory)+Path.DirectorySeparatorChar;
myProcess.StartInfo.Arguments = "corner.bin edge1.bin edge2.bin";
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data)){
timer = 0f;
StepsOfSolution++;
print(e.Data);
solution.output.Add(e.Data);
}
});
myProcess.Start();
myProcess.BeginOutputReadLine();
}
void Update(){
if (myProcess != null){
if(timer>fiveMinutes){
myProcess.Kill();
myProcess.Close();
badExit=true;
myProcess=null;
return;
}
timer += Time.deltaTime;
if (!myProcess.HasExited){
RubikScene.PleaseWait.SetActive(true);
}
else{
if(badExit){
RubikScene.PleaseWait.SetActive(false);
RubikScene.TooLong.SetActive(true);
print("TimeOut!");
}else{
paused=false;
Application.LoadLevel("solution");
}
}
}
}
不要使用myProcess.WaitForExit()
。它会阻塞,直到它返回。使用myProcess.Start()
,然后在Update功能中,在if !myProcess.HasExited
内运行动画。你的代码是不完整的,所以我会提供不完整的解决方案,但这应该工作
void Start()
{
timer = 0;//Reset Timer
myProcess.Start();
}
检查的过程中从进程读取更新功能
float timer = 0f;
float fiveMinutes = 300; //300 seconds = 5minutes
bool badExit = false;
void Update()
{
if (myProcess != null)
{
//Check if Time has reached
if(timer>fiveMinutes){
myProcess.Kill();
myProcess.Close();
badExit = true;
return;
}
timer += Time.deltaTime;
if (!myProcess.HasExited)
{
//Do Your Animation Stuff Here
}else{
//Check if this was bad or good exit
if(badExit){
//Bad
}else{
//Good
}
}
}
}
然后别的地方在你的回调函数,你接受/完成,如果这些字节读的是> 0然后总是将计时器重置为。所以定时器只会计数到5分钟当它没有收到任何东西5分钟。
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
// Prepend line numbers to each line of the output.
if (!String.IsNullOrEmpty(e.Data))
{
timer = 0f; //Reset Process Timer
lineCount++;
output.Append("\n[" + lineCount + "]: " + e.Data);
}
});
或用回调功能
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
timer = 0f; //Reset Process Timer
numOutputLines++;
// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}
如果你设置'timer = Time.time',然后使用'timer
我避开'Time.time'。这是比赛开始的时候。枪越多,数字越大,编号越多。在某些时候,你会得到一个非常大的数字,例如93848589323 + fiveMinutes,没有人知道它何时重置。 Time.deltaTime返回的数字很小,不需要大数量的操作,它也取决于帧速率,这意味着它将在每个设备中以相同的方式运行。 – Programmer
哥们你是救命恩人!谢谢你,谢谢你,谢谢你 ! – msLangdon95
使输出定时检查每X秒,而计时器不超过5分钟,如果它然后停止的过程中,没有必要阻止任何东西 –
@ user2320445 WaitForExit阻止一切。 – Programmer