微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🎮

规避 Unity 开发常见陷阱

Unity · v1.0.0
开发 安全 S 级 作者 ivangdavila 公开目录收录
2.5k 下载 3 安装 11.8k 收藏 34 浏览

简介

Unity开发者避坑指南,覆盖生命周期、性能优化、物理时序和伪空值等核心陷阱,提升代码健壮性与运行效率

原始描述

Avoid common Unity mistakes — lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null.

通过你的 Agent 添加

安装目录:unity/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。

点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。

或者手动安装

Claude Code~/.claude/skills/unity/
Codex CLI~/.codex/skills/unity/
DeepSeek Harness~/.agents/skills/unity/

包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可; 目录名不用换,也不要把包直接解压到 skills 根目录。 技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。

技能正文


name: unity description: "Automate Unity editor and runtime workflows with C#, Addressables, render pipelines, and batch builds." category: game-engines risk: safe source: self source_type: self date_added: "2026-08-26" tags: ["unity", "csharp", "editor-scripting", "urp", "addressables", "batchmode", "headless"] tools: ["claude", "cursor", "gemini", "codex"]

Unity Engine C# Editor & Runtime AI Skill Guide (Claude)

Overview & Engine Architecture

Unity 6000.x / 2022 LTS+ combines Scenes, Prefabs, ScriptableObjects, URP/HDRP/Built-in RP, and Editor-only automation via -batchmode -quit -executeMethod. Claude acts as a Principal Unity Engineer: CI builds, Addressables, deterministic player settings, and Editor/runtime assembly separation.

┌─────────────────────────────────────────────────────────────┐
│  Editor (UnityEditor)  →  -executeMethod / BuildPipeline    │
│  Player (runtime)      →  Mono / IL2CPP                     │
│  CI: one process per buildTarget; -activeBuildProfile       │
└─────────────────────────────────────────────────────────────┘

Version pin: Unity Editor version in ProjectSettings/ProjectVersion.txt — CI must use the same Hub editor (e.g. 6000.0.xf1). Command-line reference.


When to use / when not to

Use when: automated builds, asset validation, playmode tests, Addressables content builds, batch import settings.

Do not use when: Unreal-specific Nanite workflows; server-side Unity without licensing review for headless batch (Unity licensing applies to build machines).


Operational Capabilities & Agent Directives

  1. Editor isolation: #if UNITY_EDITOR, Assets/**/Editor/, Editor asmdefs — never reference UnityEditor in runtime.
  2. CI entry points: public static void X() only; EditorApplication.Exit(code) on failure; no modal dialogs in batchmode.
  3. One target per invocation: pass -buildTarget StandaloneWindows64 or -activeBuildProfile "Assets/.../Windows.asset" — multi-target requires separate processes.
  4. Deterministic builds: pin graphics tiers, disable random Application.targetFrameRate side effects in build scripts; use BuildOptions.StrictMode where available; log Application.unityVersion.
  5. Color/URP: document active RP asset; linear color space + URP asset must match CI player settings or lighting differs from Editor.
  6. Logs: always -logFile path; parse for Scripts have compiler errors before blaming runtime.

Production C#: CI build + exit code

Assets/Editor/CiBuild.cs:

#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

public static class CiBuild
{
    public static void BuildWindowsCI()
    {
        var ok = BuildInternal(BuildTarget.StandaloneWindows64, "Builds/Win/Game.exe");
        EditorApplication.Exit(ok ? 0 : 1);
    }

    static bool BuildInternal(BuildTarget target, string location)
    {
        Directory.CreateDirectory(Path.GetDirectoryName(location)!);
        var report = BuildPipeline.BuildPlayer(new BuildPlayerOptions
        {
            scenes = EditorBuildSettingsScene.GetActiveSceneList(EditorBuildSettings.scenes),
            locationPathName = location,
            target = target,
            options = BuildOptions.CompressWithLz4HC
        });
        Debug.Log($"Build {report.summary.result} size={report.summary.totalSize}");
        return report.summary.result == BuildResult.Succeeded;
    }
}
#endif
& "C:\Program Files\Unity\Hub\Editor\6000.0.42f1\Editor\Unity.exe" `
  -batchmode -nographics -quit `
  -projectPath "$PWD" `
  -buildTarget StandaloneWindows64 `
  -executeMethod CiBuild.BuildWindowsCI `
  -logFile "$PWD\Logs\ci-build.log"

Addressables / content pipeline (batch)

Use AddressableAssetSettings.BuildPlayerContent() from an Editor static method after -executeMethod imports complete. Fail CI if catalog hash changes without intentional bump (store catalog.json hash artifact).


Failure taxonomy

Symptom Cause Fix
Hang after open Modal dialog / missing -quit -batchmode -quit; guard DisplayDialog
Wrong platform shaders Missing -buildTarget / profile Set -activeBuildProfile
UnityEditor in player Editor script in runtime asmdef Editor folder + asmdef platforms
Non-deterministic lighting Different RP or color space Serialize Quality/Graphics settings
License / activation Batch on fresh VM Unity license activation docs for CI

Reddit/forum pattern: parallel Unity instances on same project path fail — one lock per -projectPath.


Agent Operational Directive

MANDATORY: Static -executeMethod in Editor assembly; explicit exit codes; -logFile; pin Editor version; separate CI job per build target.

正文来自原始仓库, 版权归作者所有,许可:MIT。 权利人认为有不妥,可申请下架。

原始仓库

作者的项目地址:https://github.com/alivirgo/Major-AI-Skills

标签

unitygame-developmentc-sharpperformance-optimizationbest-practices

同类 Skill