【问题标题】:Can I use powershell commands in Unity?我可以在 Unity 中使用 powershell 命令吗?
【发布时间】:2018-06-04 18:26:09
【问题描述】:

第一次在这里提问所以希望它符合礼仪。 有谁知道如何使用C#

process.UseShellExecute = false;

什么时候在 Unity3D 中?使用它设置为 false 会导致 exe 崩溃。但是将其设置为 true 会禁用发送 writeline 命令的能力。欢迎提出想法、建议和解决方案。感谢大家!下面的代码示例:

    ProcessStartInfo processinfo = new 
    ProcessStartInfo("C:/ConsoleExample/bin/Debug/ConsoleExample.exe");
    processinfo.RedirectStandardOutput = false;
    processinfo.RedirectStandardError = false;
    processinfo.RedirectStandardInput = true;
    processinfo.UseShellExecute = false; //<----Causes program to crash exe when launched, but is required for write console key.
    processinfo.CreateNoWindow = false;

    Process.Start(processinfo);

    Thread.Sleep(1000);


    //Write text to console exe file from unity as a command.
    processinfo.StandardInput.Write(ConsoleKey.Escape);

【问题讨论】:

  • 崩溃或冻结 Unity?这是两种不同的东西。顺便说一句,不要在主线程上使用Thread.Sleep(1000)
  • 它在 Process.Start(processinfo); 行上使 exe 文件崩溃;换句话说,exe 将无法正常启动。 Unity 很好,不会崩溃或失败。
  • 只需使用Process 而不是ProcessStartInfo 看看会发生什么
  • 好的。现在就试试吧。
  • 如果我将 Process.Start(processinfo); 更改为 Process.Start(Process); 会出现调试错误:进程是一种类型,在给定的上下文中无效

标签: unity3d process shellexecute console.writeline


【解决方案1】:

所以我最终得到了这个工作。感谢@Hristo 和@Programmer 的帮助和cmets。非常感激。这是启动控制台应用程序并发送密钥的最终代码。如果有人有问题或需要更多详细信息,请通过帖子或消息告诉我,我会在此发布更多信息。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Threading;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

public class Camera_controller : MonoBehaviour {
public int caminterrupt;
private IEnumerator coroutine;

// Use this for initialization
void Start () {
    caminterrupt = 0;
    coroutine = startcamera();
}

// Update is called once per frame
void Update () {

}

public void button1(string pressed)
{

    if (caminterrupt == 0)
    {
        caminterrupt = 1;

        StartCoroutine(coroutine);



    }
    else
    { caminterrupt = 0;
        StopCoroutine("coroutine");
    }


}

public IEnumerator startcamera() {

    //string strOutput;

    //Starting Information for process like its path, use system shell i.e. 
control process by system etc.

    ProcessStartInfo psi = new 
ProcessStartInfo(@"C:\ConsoleExample\bin\Debug\ConsoleExample.exe");
    // its states that system shell will not be used to control the process 
instead program will handle the process
    psi.UseShellExecute = false;
    psi.ErrorDialog = false;
    // Do not show command prompt window separately
    //psi.CreateNoWindow = true;
    //psi.WindowStyle = ProcessWindowStyle.Hidden;
    //redirect all standard inout to program
    psi.RedirectStandardError = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    //create the process with above infor and start it
    Process plinkProcess = new Process();
    plinkProcess.StartInfo = psi;
    plinkProcess.Start();
    //link the streams to standard inout of process
    StreamWriter myStream = new 
 StreamWriter(plinkProcess.StandardInput.BaseStream, Encoding.ASCII);

    //send command to cmd prompt and wait for command to execute with thread 
 sleep


    refire:
        if (caminterrupt == 1)
        {

            myStream.WriteLine("y");
        yield return new WaitForSeconds(1);
        goto refire;

        }
        if (caminterrupt == 0)
        {
            myStream.WriteLine("e");

        }


    myStream.WriteLine("e");

    // flush the input stream before sending exit command to end process for 
any unwanted characters

    myStream.Close();

    plinkProcess.Close();
    caminterrupt = 0;

    }
 }

这是调用控制台文件并通过发送键命令进行交互的 Unity 端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2011-01-23
    • 2013-07-14
    • 1970-01-01
    • 2011-03-20
    相关资源
    最近更新 更多