unity-so-prefab-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity SO-Prefab Manager

Unity SO-Prefab管理器

This skill enforces a strictly modular "Bridge" pattern between Data and Logic.
本技能在数据与逻辑之间强制实施严格模块化的‘桥接’模式。

Core Workflow: The Bridge Pattern

核心工作流:桥接模式

To ensure that multiple instances of the same unit (e.g., 4 Robots) can have independent runtime values (Health, Energy) while sharing the same Template (RobotSO), follow these steps:
为确保同一单位的多个实例(例如4个机器人)在共享同一模板(RobotSO)的同时,能拥有独立的运行时数值(生命值、能量值),请遵循以下步骤:

1. Structure the ScriptableObject (The Template)

1. 构建ScriptableObject(模板)

The SO should contain Static/Read-Only data that defines the "Ideal" version of the object.
  • Example:
    maxHealth
    ,
    baseSpeed
    ,
    displayName
    .
SO应包含静态/只读数据,用于定义对象的‘理想’版本。
  • 示例:
    maxHealth
    ,
    baseSpeed
    ,
    displayName
    .

2. Structure the MonoBehaviour (The Instance)

2. 构建MonoBehaviour(实例)

The script on the Prefab should contain Runtime/Mutable data and a reference to the SO.
  • Example:
    currentHealth
    ,
    activeBuffs
    .
Prefab上的脚本应包含运行时/可变数据以及对SO的引用。
  • 示例:
    currentHealth
    ,
    activeBuffs
    .

3. Initialize the Bridge

3. 初始化桥接

Use
Awake()
to copy values from the SO to the local instance variables.
csharp
[Header("Data Reference")]
[SerializeField] private RobotSO data;

[Header("Runtime State")]
private float currentHealth;

private void Awake() {
    if (data == null) {
        Debug.LogError($"[System] Fail: Missing SO data on {gameObject.name}");
        return;
    }
    // Initialize independent state
    currentHealth = data.MaxHealth;
    
    Debug.Log($"[{data.name}] Success: Initialized instance | Health: {currentHealth}");
}
使用
Awake()
方法将SO中的值复制到本地实例变量中。
csharp
[Header("Data Reference")]
[SerializeField] private RobotSO data;

[Header("Runtime State")]
private float currentHealth;

private void Awake() {
    if (data == null) {
        Debug.LogError($"[System] Fail: Missing SO data on {gameObject.name}");
        return;
    }
    // Initialize independent state
    currentHealth = data.MaxHealth;
    
    Debug.Log($"[{data.name}] Success: Initialized instance | Health: {currentHealth}");
}

Advanced Usage

进阶用法

1. File Creation: API-First vs. YAML Power-Mode

1. 文件创建:API优先模式 vs YAML高效模式

Standard (1-10 Files) - Use Unity API

标准模式(1-10个文件)- 使用Unity API

For most tasks, use
mcp_unityMCP_manage_scriptable_object
. This is the healthiest method as it ensures GUID integrity and immediate
.meta
file availability.
  • Workflow:
    Call manage_scriptable_object
    ->
    Apply Patches
    .
对于大多数任务,使用
mcp_unityMCP_manage_scriptable_object
。这是最稳妥的方法,因为它能确保GUID的完整性,且能立即生成
.meta
文件。
  • 工作流:
    调用manage_scriptable_object
    ->
    应用补丁
    .

Performance (10+ Files) - Use YAML Generation

高效模式(10个以上文件)- 使用YAML生成

For massive data dumps (e.g., importing 50 items), bypass the API latency by writing files directly.
  • Workflow:
    1. Create a YAML template based on an existing
      .asset
      file.
    2. Write
      .asset
      files directly to
      Assets/Data/
      via
      write_to_file
      .
    3. Call
      mcp_unityMCP_refresh_unity(scope="assets")
      to force Unity to generate
      .meta
      files.
对于大规模数据导入(例如,导入50个物品),可通过直接写入文件来绕过API延迟。
  • 工作流:
    1. 基于现有
      .asset
      文件创建YAML模板。
    2. 通过
      write_to_file
      .asset
      文件直接写入
      Assets/Data/
      目录。
    3. 调用
      mcp_unityMCP_refresh_unity(scope="assets")
      强制Unity生成
      .meta
      文件。

2. Surgical YAML (Deep Debugging)

2. 精准YAML操作(深度调试)

Use direct file reading/parsing for "Deep Audits" that the API can't quickly handle:
  • GUID Verification: Search for a specific
    m_Script: {fileID: ..., guid: ...}
    in prefabs to find which scripts are actually attached.
  • Reference Repair: Use
    grep_search
    and
    replace_file_content
    project-wide to swap broken GUIDs without opening the Editor for every file.
对于API无法快速处理的‘深度审计’操作,可使用直接文件读取/解析:
  • GUID验证: 在prefab中搜索特定的
    m_Script: {fileID: ..., guid: ...}
    ,以确定实际挂载的脚本。
  • 引用修复: 在项目范围内使用
    grep_search
    replace_file_content
    来替换损坏的GUID,无需逐个打开编辑器操作文件。

Reference Material

参考资料

  • See references/bridge-pattern.md for detailed implementation details and troubleshooting.
  • See references/naming-conventions.md for the project-standard folder structure.
  • See references/yaml-templates.md for standard Unity YAML boilerplate.
  • 如需详细的实现细节和故障排除方法,请查看references/bridge-pattern.md
  • 如需项目标准文件夹结构,请查看references/naming-conventions.md
  • 如需标准Unity YAML模板,请查看references/yaml-templates.md