unity-so-prefab-manager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity 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 to copy values from the SO to the local instance variables.
Awake()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}");
}使用方法将SO中的值复制到本地实例变量中。
Awake()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 . This is the healthiest method as it ensures GUID integrity and immediate file availability.
mcp_unityMCP_manage_scriptable_object.meta- Workflow: ->
Call manage_scriptable_object.Apply Patches
对于大多数任务,使用。这是最稳妥的方法,因为它能确保GUID的完整性,且能立即生成文件。
mcp_unityMCP_manage_scriptable_object.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:
- Create a YAML template based on an existing file.
.asset - Write files directly to
.assetviaAssets/Data/.write_to_file - Call to force Unity to generate
mcp_unityMCP_refresh_unity(scope="assets")files..meta
- Create a YAML template based on an existing
对于大规模数据导入(例如,导入50个物品),可通过直接写入文件来绕过API延迟。
- 工作流:
- 基于现有文件创建YAML模板。
.asset - 通过将
write_to_file文件直接写入.asset目录。Assets/Data/ - 调用强制Unity生成
mcp_unityMCP_refresh_unity(scope="assets")文件。.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 in prefabs to find which scripts are actually attached.
m_Script: {fileID: ..., guid: ...} - Reference Repair: Use and
grep_searchproject-wide to swap broken GUIDs without opening the Editor for every file.replace_file_content
对于API无法快速处理的‘深度审计’操作,可使用直接文件读取/解析:
- GUID验证: 在prefab中搜索特定的,以确定实际挂载的脚本。
m_Script: {fileID: ..., guid: ...} - 引用修复: 在项目范围内使用和
grep_search来替换损坏的GUID,无需逐个打开编辑器操作文件。replace_file_content
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。