1
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?

github copilotにunityで赤い球を作ってもらう

Last updated at Posted at 2025-06-24

image.png

一次ソース

前提

準備

  • プロジェクトルートに「.github」フォルダを作り中に「copilot-instructions.md」を作製
  • 中に次をコピペ
copilot-instructions.md
# カスタムコマンド

## 「Unityで○○をして」
### 指示の定義
  - ユーザーが「Unityで○○をして」と指示した場合、以下を実行します。
  - この指示はUnity MCPに基づき実行します
    - 既存のUnity MCPにより実行可能であれば、この指示はバイパスします。例えば次の処理です
        - Unity エディタのメニュー項目を実行
        - Unity コンソールログ操作
        - アセンブリ情報の取得
        - パッケージ情報の取得
  - 以下の手順に従って自動化フローを実行します。
   - 次を言う「★はい、Unityの自動実行拡張の生成実行を行います」
    - 鍵UUIDの作成
        - 操作を識別するためのUUIDを生成します
   - エディタ拡張スクリプトの作成
        - サンプルスクリプト(MCP_CreateOneSphereEditor.cs)を参考にしてAssets/MCPフォルダにエディタ拡張を作製します
            - このサンプルは「球を作って」と指示した場合の例です
        - 重要な点として、鍵UUIDのフラグファイルを認識し、リロード時にUnityが自動で実行するようにします
    - 鍵UUIDフラグファイルの作成
        - Assets/MCP/RunOnce_<UUID>.flagという名前でフラグファイルを作成します
        - 内容は空とします
    - Unityのリフレッシュ(スクリプトリロード)を実行
        - UnityMCP MenuItemCommandHandlerで{ "menuItem": "Assets/Refresh"}を実行

### サンプルスクリプト(MCP_CreateOneSphereEditor.cs)

```csharp
using UnityEditor;
using UnityEngine;
using System.IO;

[InitializeOnLoad]
public class MCP_CreateOneSphereEditor : EditorWindow
{
    // UUID: 7e2b8c1a-1f2a-4b3e-9c7d-8a6e5f4b2c1d
    private const string FlagPath = "Assets/MCP/RunOnce_7e2b8c1a-1f2a-4b3e-9c7d-8a6e5f4b2c1d.flag";

    static MCP_CreateOneSphereEditor()
    {
        if (File.Exists(FlagPath))
        {
            File.Delete(FlagPath);
            Debug.Log("[MCP] Detected flag file, executing creation of one sphere at origin.");
            CreateOneSphereAtOrigin();
        }
    }

    [MenuItem("MCP/Create One Sphere at Origin")]
    public static void CreateOneSphereAtOrigin()
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = Vector3.zero;
        sphere.name = "MCP_Sphere";
        Undo.RegisterCreatedObjectUndo(sphere, "Create Sphere at Origin");
        Debug.Log("[MCP] One sphere created at origin by editor extension.");
    }
}
```


動作確認

まず念のためチャット履歴をクリア

/clear

UnityMCPを認識してもらう&エディタ状況チェック

#unity_connectToProject

やってもらいたいこと

Unityで赤い球を作って

おわり

1
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
1
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?