Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9360

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

$
0
0

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

動機

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

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

環境

  • Windows 10
  • Unity 2018.4.9f1

コード・ビルド・テスト

  • Assets\Scripts\StdinReader.csを作成
Assets\Scripts\StdinReader.cs
usingUnityEngine;usingSystem;usingSystem.Threading.Tasks;publicclassStdinReader:MonoBehaviour{voidStart(){Task.Run(()=>{stringline;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#ではないと思ったので採用は見送った。 


Viewing all articles
Browse latest Browse all 9360

Latest Images

Trending Articles