LoginSignup
2
3

More than 3 years have passed since last update.

Unity C#はEXE化すれば標準入力を受け取れる

Posted at

タイトルの通りです。動機とか実行例を載せておきます。

動機

Windows環境のUnityでプロセス間通信(IPC)する!

NamedPipeServerStreamが使えない1んだが!!!!どうせAnonymousPipeServerStreamも使えないんだろ?TCPやUDPは違う。何か実装できそうな(面白い)方法はないか・・・・・・・・・・・・・・・・標準入力いけるか?

環境

  • Windows 10
  • Unity 2018.4.9f1

コード・ビルド・テスト

  • Assets\Scripts\StdinReader.cs を作成
Assets\Scripts\StdinReader.cs
using UnityEngine;
using System;
using System.Threading.Tasks;

public class StdinReader : MonoBehaviour
{
    void Start()
    {
        Task.Run(() =>
        {
            string line;
            Debug.LogError("WhileStart");
            while ((line = Console.ReadLine()) != null)
            {
                Debug.LogError("GetLine:" + line);
            }
            Debug.LogError("WhileEnd");
        });
    }
}
  • Main Cameraにコンポーネントを追加
  • [File]→[Build Settings...]の Development Build を☑して Build、適当にフォルダを作成して選択

コメント 2020-05-13 114434.png

  • UnityStdinTest.exeが生成できていることを確認
  • Powershellから起動
echo "first line `n second line `n" | .\UnityStdinTest.exe

コメント 2020-05-14 134220.png

コメント 2020-05-14 134604.png

  • WSLの実行結果も持ってこれます

コメント 2020-05-14 135209.png

参考

感想

出来た時は爆笑した。基本的には参考の2番目のように子プロセスを作って標準出力を読み込めばいいと思う。


  1. C#をdll化すれば使えなくもない(sh-akira/UnityNamedPipe: 名前付きパイプでUnityを外部アプリからコントロールLachee/unity-named-pipes: A native named pipe wrapper for Unity3D)。Unity内のC#ではないと思ったので採用は見送った。 

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3