# 序列化警告：ScriptableObject 'PlayerData' 版本不匹配。预期版本 1，找到版本 2。数据可能丢失。

- **ID:** `unity/scriptableobject-serialization-version-mismatch`
- **领域:** unity
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

ScriptableObject 资源使用较新版本的类定义（例如添加了字段）序列化，但当前代码期望旧版本，导致反序列化不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2021.3.0f1 | active | — | — |
| Unity 2022.3.0f1 | active | — | — |
| Unity 2023.2.0f1 | active | — | — |
| Unity 2024.1.0b1 | active | — | — |

## 解决方案

1. ```
   Implement custom serialization with versioning: Add a version field to the ScriptableObject and use OnBeforeSerialize/OnAfterDeserialize to handle migration. Example:
public int version = 1;
public void OnAfterDeserialize() {
    if (version < 2) {
        // migrate old data
        version = 2;
    }
}
   ```
2. ```
   Use a custom Editor script to update all ScriptableObject assets to the latest version: Iterate through assets and call a migration method that sets default values for new fields.
   ```

## 无效尝试

- **Delete and recreate the ScriptableObject asset from scratch** — This loses all existing data and may not be feasible if data is critical; also does not fix the root mismatch. (90% 失败率)
- **Revert the script to the old version without the new fields** — This prevents using new features and may break other parts of the code that depend on the new fields. (75% 失败率)
