unity-csharp

A set of practical guidelines for writing Unity game scripts in C#. Unity is a game engine, and these rules cover its update loop, object lifetime, memory cleanup, coroutines, and editable data.

In plain words
What is it for?
Use it when creating or reviewing Unity MonoBehaviour and ScriptableObject scripts. It helps with performance tuning, coroutine problems, object pooling, serialization, and safe cleanup.
Why use it?
It helps avoid frame-rate drops, repeated event handlers, memory leaks, excessive garbage collection, and lifecycle-order bugs. It also clarifies when to use per-frame code, events, or timed routines.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/unity-csharp
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill unity-csharp
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 28 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,931 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00028 $0.02931
Opus 5 $0.00014 $0.01465
Sonnet 5 $0.00006 $0.00586
Haiku 4.5 $0.00003 $0.00293

Measured 2d ago against content hash 4715e549708f, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

unity-csharp scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/unity-csharp/SKILL.md · 198 lines

How it starts

The opening of the file, as written. The whole thing — 198 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Unity C# 最佳实践

何时用

  • 开始编写任何 Unity MonoBehaviour 或 ScriptableObject 脚本前。
  • Review 他人 Unity C# 代码,发现有 Update 里调 GetComponent 或每帧 new 对象时。
  • 接到性能优化任务:帧率抖动、GC Alloc 频繁、Profiler 里 Update 调用栈过深。
  • 设计新的配置数据结构,考虑用 ScriptableObject 还是 public 字段时。
  • 协程逻辑出现卡死、泄漏、或销毁对象后仍在跑的诡异现象时。

核心规则

1. 慎用 Update:每帧逻辑最小化

规则: Update 里只放真正需要每帧响应的逻辑;能用事件、协程或 InvokeRepeating 驱动的,就不放进 Update;任何 GetComponent、Find、tag 比较一律在 Awake/Start 里缓存,不在 Update 里调用。

为什么: AI 和新手最常见的模式是把所有逻辑堆进 Update——"反正每帧都会跑到"。结果是:GetComponent 每帧反射查找,100 个对象就是 100 次反射;倒计时用 timer -= Time.deltaTime 没问题,但 UI 刷新、动画触发、状态机判断全挤在里面,Profiler 一看 Update 占 80% CPU。更隐蔽的问题:新人把 FindObjectOfType<GameManager>() 放进 Update,场景里一有几十个对象就掉帧,排查时根本不知道从哪查。

怎么做:

  • 初始化引用全部放 Awake(自身组件)或 Start(跨对象引用)。
  • 定时逻辑用 InvokeRepeating 或协程 WaitForSeconds,不用 Update 里的计数器模拟低频事件。
  • 状态变化用 C# event / UnityEvent 通知,订阅方在变化时响应,而不是每帧 poll if (state == X)
  • 真正需要每帧的(移动插值、输入读取),保留在 Update,但代码量要极简,复杂运算提取为方法并在注释说明频率必要性。

2. 防 GC 抖动:高频路径零分配

规则: Update、FixedUpdate、协程的热路径里禁止出现 new(含 LINQ、字符串拼接、装箱);高频复用的对象用对象池管理;容器在初始化时预分配容量。

为什么: Unity 使用 Mono / IL2CPP 的 GC,GC 触发时会造成明显的帧率刺尖(spike)。AI 写的代码里最常见的杀手:string.Format($"Score: {score}") 每帧执行一次,enemies.Where(e => e.isAlive).ToList() 每帧生成一个新 List,new Vector3(...) 看起来是值类型不会 GC——但装箱到 object 参数时就会。新手则喜欢在子弹生成时 Instantiate、销毁时 Destroy,百发子弹就是百次 GC 压力。

怎么做:

  • StringBuilderTMP_Text.SetText(format, arg) 替代字符串拼接。
  • LINQ 仅用于编辑器工具或低频初始化代码,运行时热路径改用显式 for 循环。
  • 子弹、特效、UI 元素用 ObjectPool<T>(Unity 2021+ 内置)或自实现的栈式对象池。
  • List<T>(initialCapacity) 预分配,避免频繁扩容。
  • 用 Unity Profiler 的 Memory 视图确认改动前后 GC Alloc 列清零。

3. 协程与生命周期:时序清楚,防泄漏

规则: 明确 Awake → OnEnable → Start → Update → OnDisable → OnDestroy 的时序;协程在对象 SetActive(false)OnDisable 时自动停止,但 OnDestroy 时不会自动停止跑在其他 MonoBehaviour 上的协程;协程引用必须在 OnDisable/OnDestroy 里主动 StopCoroutine

为什么: AI 最典型的错误:在 Awake 里启动协程并访问 Start 才会初始化完毕的跨组件引用,导致 NullReferenceException;或者在 OnDestroy 不清理,导致协程持有对已销毁对象的引用,每帧报 MissingReferenceException,但对象本体早就没了,难以溯源。新手则习惯用 StartCoroutine 后不记返回值,后来想停却只能 StopAllCoroutines,误杀其他协程。

Read the full file on GitHub · 198 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 198 lines · 28 tokens per session scan A 4715e549708f

Subscribe to this mod's changes

unity-csharp is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 28 tokens to every session and 2,931 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

systematic-debugging

Skill "systematic-debugging" from jnMetaCode/superpowers-zh, covering 系统化调试, 概述, 铁律, 何时使用 and 四个阶段.

jnMetaCode/superpowers-zh · 24 tokens