Unity editor control

From IvanYangYangXi/artclaw_bridge · View interactive version

Unity editor control is an AI agent skill from the GitHub repository IvanYangYangXi/artclaw_bridge. Unity Editor state control via run_unity_python: undo/redo, play mode, build, console, menu execution, and editor preferences. Use when AI needs to: (1) undo or redo editor operations, (2) enter/exit Play Mode, (3) trigger builds (Build Player), (4) read Unity console logs, (5) execute editor menu i

How to use this skill

Skill file

Unity 编辑器控制

通过 run_unity_python 控制 Unity Editor 状态:撤销、Play Mode、构建、控制台日志等。

调用方式

run_unity_python(code="<C# 代码>")

操作示例

撤销(Undo)

UnityEditor.Undo.PerformUndo();
return "{\"undone\": true}";

重做(Redo)

UnityEditor.Undo.PerformRedo();
return "{\"redone\": true}";

获取当前 Undo 历史

return Newtonsoft.Json.JsonConvert.SerializeObject(new {
    undoName = UnityEditor.Undo.GetCurrentGroupName(),
    undoGroupIndex = UnityEditor.Undo.GetCurrentGroup()
});

进入 Play Mode

if (!UnityEditor.EditorApplication.isPlaying) {
    UnityEditor.EditorApplication.isPlaying = true;
    return "{\"entering_play_mode\": true}";
}
return "{\"already_playing\": true}";

退出 Play Mode

if (UnityEditor.EditorApplication.isPlaying) {
    UnityEditor.EditorApplication.isPlaying = false;
    return "{\"exiting_play_mode\": true}";
}
return "{\"not_playing\": true}";

获取编辑器状态

return Newtonsoft.Json.JsonConvert.SerializeObject(new {
    isPlaying = UnityEditor.EditorApplication.isPlaying,
    isPaused = UnityEditor.EditorApplication.isPaused,
    isCompiling = UnityEditor.EditorApplication.isCompiling,
    isUpdating = UnityEditor.EditorApplication.isUpdating,
    unityVersion = UnityEngine.Application.unityVersion,
    productName = UnityEngine.Application.productName,
    companyName = UnityEngine.Application.companyName,
    dataPath = UnityEngine.Application.dataPath
});

执行菜单项

// 执行 Assets/Reimport All 菜单
UnityEditor.EditorApplication.ExecuteMenuItem("Assets/Reimport All");
return "{\"executed\": true}";

读取控制台日志

// 通过 LogEntries 反射读取 Unity 控制台(Unity 内部 API)
var logEntriesType = System.Type.GetType("UnityEditor.LogEntries, UnityEditor");
var getCountMethod = logEntriesType?.GetMethod("GetCount");
var getEntryMethod = logEntriesType?.GetMethod("GetEntryInternal");
int count = (int)(getCountMethod?.Invoke(null, null) ?? 0);
return Newtonsoft.Json.JsonConvert.SerializeObject(new { logCount = count });

刷新项目(等价于 Ctrl+R)

UnityEditor.AssetDatabase.Refresh();
return "{\"refreshed\": true}";

触发构建(Build Player)

// 警告:构建操作耗时,会阻塞编辑器
var buildOptions = new UnityEditor.BuildPlayerOptions {
    scenes = new[] { "Assets/Scenes/Main.unity" },
    locationPathName = "Builds/MyGame.exe",
    target = UnityEditor.BuildTarget.StandaloneWindows64,
    options = UnityEditor.BuildOptions.None
};
var report = UnityEditor.BuildPipeline.BuildPlayer(buildOptions);
return Newtonsoft.Json.JsonConvert.SerializeObject(new {
    result = report.summary.result.ToString(),
    totalErrors = report.summary.totalErrors,
    totalWarnings = report.summary.totalWarnings,
    outputPath = report.summary.outputPath
});

获取项目设置

return Newtonsoft.Json.JsonConvert.SerializeObject(new {
    productName = UnityEditor.PlayerSettings.productName,
    companyName = UnityEditor.PlayerSettings.companyName,
    bundleVersion = UnityEditor.PlayerSettings.bundleVersion,
    apiCompatibilityLevel = UnityEditor.PlayerSettings.GetApiCompatibilityLevel(
        UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup
    ).ToString(),
    scriptingBackend = UnityEditor.PlayerSettings.GetScriptingBackend(
        UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup
    ).ToString(),
    activeBuildTarget = UnityEditor.EditorUserBuildSettings.activeBuildTarget.ToString()
});

打开 Prefab Stage(编辑 Prefab)

var prefabAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.GameObject>("Assets/Prefabs/Enemy.prefab");
if (prefabAsset == null) return "{\"error\": \"Prefab not found\"}";
UnityEditor.SceneManagement.PrefabStageUtility.OpenPrefab("Assets/Prefabs/Enemy.prefab");
return "{\"opened\": true}";

聚焦 Game Object(在 Scene 视图中)

var go = UnityEngine.GameObject.Find("Player");
if (go == null) return "{\"error\": \"Not found\"}";
UnityEditor.Selection.activeGameObject = go;
UnityEditor.SceneView.FrameLastActiveSceneView();
return "{\"focused\": true}";

注意事项

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.