Agent SkillsBesty0728/Unity-Skills › unity-patterns

unity-patterns

GitHub

为Unity设计模式选择提供建议,涵盖ScriptableObject、事件系统、状态机等。指导用户判断何种模式适合特定问题或用于构建解耦系统,并在不同方案间进行权衡决策。

SkillsForUnity/unity-skills~/skills/patterns/SKILL.md Besty0728/Unity-Skills

Trigger Scenarios

询问使用何种设计模式 判断是否该用状态机 在事件/状态机/对象池方案间抉择 构建解耦系统

Install

npx skills add Besty0728/Unity-Skills --skill unity-patterns -g -y
More Options

Non-standard path

npx skills add https://github.com/Besty0728/Unity-Skills/tree/main/SkillsForUnity/unity-skills~/skills/patterns -g -y

Use without installing

npx skills use Besty0728/Unity-Skills@unity-patterns

指定 Agent (Claude Code)

npx skills add Besty0728/Unity-Skills --skill unity-patterns -a claude-code -g -y

安装 repo 全部 skill

npx skills add Besty0728/Unity-Skills --all -g -y

预览 repo 内 skill

npx skills add Besty0728/Unity-Skills --list

SKILL.md

Frontmatter
{
    "name": "unity-patterns",
    "description": "Advises on choosing Unity design patterns — ScriptableObject, event systems, state machines, object pooling, observer, and more. Use when deciding which pattern fits a problem, structuring decoupled systems, or choosing between event\/state-machine\/pool approaches, even if the user just asks \"用什么模式\" or \"该用状态机吗\". 为选择 Unity 设计模式提供建议(ScriptableObject、事件系统、状态机、对象池、观察者等);当用户要判断哪种模式适合某问题、构建解耦系统、或在事件\/状态机\/对象池方案间抉择时使用。"
}

Unity Pattern Selector

Use this skill to decide whether a pattern is justified. Do not recommend every pattern at once.

Guardrails

Mode: Documentation only — no REST skills to gate; load freely under any operating mode (Approval / Auto / Bypass).

  • Recommend at most 1-3 patterns, and explain why simpler options are not enough.

Pattern Guide

  • ScriptableObject

    • Use for authored config, shared static data, event channels, and reusable data assets.
    • Avoid as the default home for per-run mutable gameplay state.
  • C# events / delegates

    • Use for one-to-many notifications with clear ownership and unsubscribe points.
    • Avoid for imperative flows that need ordering, return values, or complex debugging.
  • Global event bus / observer hub

    • Use sparingly and only when many systems truly need broad decoupled notifications.
    • Avoid as the default answer to coupling. It often hides ownership and makes debugging harder.
  • Interfaces

    • Use when multiple implementations or clearer dependency boundaries are needed.
    • Avoid adding interfaces around every class without a real seam.
  • State machine

    • Use for actors with mutually exclusive states and explicit transitions.
    • Avoid when a few booleans or a small command flow is enough.
  • Object pool

    • Use for frequent spawn/despawn of bullets, VFX, enemies, UI items.
    • Avoid for rare objects or when lifetime is simple.
  • Service layer

    • Use for a small number of cross-scene systems with explicit bootstrap and interfaces.
    • Avoid turning everything into hidden singletons or service locators.
  • Generics / custom attributes

    • Use when they remove repeated boilerplate with clear type safety or editor metadata value.
    • Avoid when they make gameplay code harder to read than duplicated simple code.

Decision Lab: Same Goal, Multiple Implementations

The Pattern Guide above answers "which pattern?". For most non-trivial design decisions, the better exercise is: pick the 2-3 plausible implementations, put them side by side, and compare on switch cost, query cost, code complexity, and failure modes. Below is a worked example; use it as the shape for other decisions rather than as the answer.

Worked example: "Pause AI on 100 enemies for a cutscene"

Implementation Switch cost (enter/exit cutscene) Per-frame cost while paused Code complexity Failure modes
Field flag: each EnemyAI.Update starts with if (_paused) return;; manager sets the flag O(N) writes, no allocations N branch tests + dispatch overhead per frame Lowest — one bool, one guard clause Silent bugs when a dev adds a new Update and forgets the guard
enabled = false: manager disables the MonoBehaviour on every enemy O(N) writes, no allocations Zero — Unity skips disabled Behaviours in its message list Low, but state spread across components OnDisable / OnEnable side effects (coroutines stop, listeners unsubscribe) may fire unexpectedly
Subset list: manager keeps _active / _frozen lists; no per-enemy Update — manager ticks only _active O(N) list move on switch Proportional to _active count only — perfect early-cull Highest — one source of truth for ownership, requires managed ticking Adding a new Update on enemies reintroduces per-frame cost; the list is easy to desync if other code respawns enemies directly

How to use

  • Pick the smallest scale where the trade-off matters. At N=5 all three are indistinguishable; at N=5000 only the subset list stays flat.
  • State what gets worse under each option, not only what improves. A table with only upsides is a sign you haven't thought about it yet.
  • The ECS lesson is that the same three shapes (value change, structural change, enableable) show up everywhere — the labels transfer even when the framework does not. Source: Dots101/Entities101/Assets/HelloCube/13. StateChange/SetStateSystem.cs:42-80 — one system dispatches to three implementations behind config.Mode, each with measurably different cost in the profiler.

When to skip this exercise

If one option is obviously bounded (N ≤ 10, state changes once per session, or the code runs once at startup), a single paragraph "we picked X because it's simplest" is enough. Decision Lab is for choices that will outlive the current task.

Output Format

  • Recommended pattern(s)
  • Why they fit this case
  • Why not the simpler alternative
  • Minimal implementation boundary
  • Known tradeoffs

Version History

  • ec9f870 Current 2026-07-05 14:40

Same Skill Collection

SkillsForUnity/unity-skills~/skills/adr/SKILL.md
SkillsForUnity/unity-skills~/skills/animator/SKILL.md
SkillsForUnity/unity-skills~/skills/architecture/SKILL.md
SkillsForUnity/unity-skills~/skills/asmdef/SKILL.md
SkillsForUnity/unity-skills~/skills/asset/SKILL.md
SkillsForUnity/unity-skills~/skills/async/SKILL.md
SkillsForUnity/unity-skills~/skills/batch/SKILL.md
SkillsForUnity/unity-skills~/skills/blueprints/SKILL.md
SkillsForUnity/unity-skills~/skills/bookmark/SKILL.md
SkillsForUnity/unity-skills~/skills/camera/SKILL.md
SkillsForUnity/unity-skills~/skills/cinemachine/SKILL.md
SkillsForUnity/unity-skills~/skills/cleaner/SKILL.md
SkillsForUnity/unity-skills~/skills/component/SKILL.md
SkillsForUnity/unity-skills~/skills/console/SKILL.md
SkillsForUnity/unity-skills~/skills/debug/SKILL.md
SkillsForUnity/unity-skills~/skills/decal/SKILL.md
SkillsForUnity/unity-skills~/skills/dotween/SKILL.md
SkillsForUnity/unity-skills~/skills/editor/SKILL.md
SkillsForUnity/unity-skills~/skills/event/SKILL.md
SkillsForUnity/unity-skills~/skills/gameobject/SKILL.md
SkillsForUnity/unity-skills~/skills/graphics/SKILL.md
SkillsForUnity/unity-skills~/skills/history/SKILL.md
SkillsForUnity/unity-skills~/skills/importer/SKILL.md
SkillsForUnity/unity-skills~/skills/inspector/SKILL.md
SkillsForUnity/unity-skills~/skills/light/SKILL.md
SkillsForUnity/unity-skills~/skills/material/SKILL.md
SkillsForUnity/unity-skills~/skills/navmesh/SKILL.md
SkillsForUnity/unity-skills~/skills/netcode-design/SKILL.md
SkillsForUnity/unity-skills~/skills/netcode/SKILL.md
SkillsForUnity/unity-skills~/skills/optimization/SKILL.md
SkillsForUnity/unity-skills~/skills/package/SKILL.md
SkillsForUnity/unity-skills~/skills/perception/SKILL.md
SkillsForUnity/unity-skills~/skills/performance/SKILL.md
SkillsForUnity/unity-skills~/skills/physics/SKILL.md
SkillsForUnity/unity-skills~/skills/postprocess/SKILL.md
SkillsForUnity/unity-skills~/skills/prefab/SKILL.md
SkillsForUnity/unity-skills~/skills/probuilder/SKILL.md
SkillsForUnity/unity-skills~/skills/profiler/SKILL.md
SkillsForUnity/unity-skills~/skills/project-scout/SKILL.md
SkillsForUnity/unity-skills~/skills/project/SKILL.md
SkillsForUnity/unity-skills~/skills/sample/SKILL.md
SkillsForUnity/unity-skills~/skills/scene-contracts/SKILL.md
SkillsForUnity/unity-skills~/skills/scene/SKILL.md
SkillsForUnity/unity-skills~/skills/script-roles/SKILL.md
SkillsForUnity/unity-skills~/skills/script/SKILL.md
SkillsForUnity/unity-skills~/skills/scriptableobject/SKILL.md
SkillsForUnity/unity-skills~/skills/scriptdesign/SKILL.md
SkillsForUnity/unity-skills~/skills/shader/SKILL.md
SkillsForUnity/unity-skills~/skills/shadergraph/SKILL.md
SkillsForUnity/unity-skills~/skills/SKILL.md
SkillsForUnity/unity-skills~/skills/smart/SKILL.md
SkillsForUnity/unity-skills~/skills/terrain/SKILL.md
SkillsForUnity/unity-skills~/skills/test/SKILL.md
SkillsForUnity/unity-skills~/skills/testability/SKILL.md
SkillsForUnity/unity-skills~/skills/timeline/SKILL.md
SkillsForUnity/unity-skills~/skills/ui/SKILL.md
SkillsForUnity/unity-skills~/skills/uitoolkit/SKILL.md
SkillsForUnity/unity-skills~/skills/urp/SKILL.md
SkillsForUnity/unity-skills~/skills/validation/SKILL.md
SkillsForUnity/unity-skills~/skills/volume/SKILL.md
SkillsForUnity/unity-skills~/skills/workflow/SKILL.md
SkillsForUnity/unity-skills~/skills/xr/SKILL.md
SkillsForUnity/unity-skills~/skills/yooasset-design/SKILL.md
SkillsForUnity/unity-skills~/skills/yooasset/SKILL.md
SkillsForUnity/unity-skills~/SKILL.md
SkillsForUnity/unity-skills~/skills/addressables-design/SKILL.md
SkillsForUnity/unity-skills~/skills/dotween-design/SKILL.md
SkillsForUnity/unity-skills~/skills/shadergraph-design/SKILL.md
SkillsForUnity/unity-skills~/skills/unitask-design/SKILL.md
SkillsForUnity/unity-skills~/skills/yaml-editing/SKILL.md

Metadata

Files
0
Version
ec9f870
Hash
7250bbd7
Indexed
2026-07-05 14:40

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-07-08 23:19
浙ICP备14020137号-1 $Carte des visiteurs$