0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

(ほぼコピペだけで)Unityエディタ内にTCPサーバーを作成する

Last updated at Posted at 2025-11-20

はじめに

他言語などからAPI通信でUnityエディタを操縦したいとき、ありますよね。

そういうモジュールを作成したので公開します。

作り方

1. EditorTcpServerをコピペする

このコードをコピペする。

2. 継承クラスを作成

継承してビジネスロジック(あなたがそのTCPサーバーでやりたいこと)を実装します

#if UNITY_EDITOR

using System.Threading.Tasks;
using UnityEditor;

namespace EditorTcpServer.Sample
{
    public class SampleServer : EditorTcpServer
    {
        // ポップアップ名
        [MenuItem("Window/Sample TCP Server")]
        public static void ShowWindow()
        {
            // ウィンドウの表示名
            GetWindow<SampleServer>("Sample TCP Server");
        }

        public override Task<string> ProcessRequest(string request)
        {
            // ここにビジネスロジックを書く
            return Task.FromResult($"Dummy: {request}");
        }

        protected override void OnGUI()
        {
            base.OnGUI();

            // 何か追加GUIを作りたければ書く
        }
    }
}

#endif

#if UNITY_EDITORがないとビルド時にエラーが発生します

おわり。

使い方

1. TCPサーバーウィンドウを開く

MenuItemで指定したポップアップにあります。
image.png

2. 開始ボタンを押す

ボタンを押すと始まります
image.png

試し方

簡単なTCP送信Pythonスクリプトを用意したのでテスト用にお使いください

サンプルをそのまま使うとこの通り。
image.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?