04 · 免服云端双轨存档
无需自建数据库,将单机游戏升级为跨设备云漫游游戏。
1. 全局生命周期管理(必须配置)
- 在第一个加载场景中创建一个新的空
GameObject(命名为SaveManager)。 - 点击 Add Component →
TelegramCloudSaveManager。 - 勾选 Sync On Start:
- 游戏启动第一帧自动核对云端与本地时间戳
- 切换到后台时自动将脏数据刷新至云端
零代码体验
勾选 Sync On Start 后,云端存读档无需编写任何额外代码。
2. 零代码 UI 联动
数据展示
在金币/等级 Text 组件上挂载 TelegramCloudDataText:
| 字段 | 示例 |
|---|---|
| Data Key | GoldCoins |
| Format | 金币:{value} |
数值变化时文本自动刷新。
数据增量 / 修改
在关卡完成或奖励 Button 上挂载 TelegramCloudDataButton:
| 字段 | 值 |
|---|---|
| Action Type | AddInt |
| Data Key | GoldCoins |
| Value | 100 |
| Auto Save To Cloud | ✅ 勾选 |
3. 现有 C# 代码一秒替换
在现有 C# 脚本中执行全局搜索替换:
csharp
// 替换前
PlayerPrefs.SetInt("GoldCoins", value);
PlayerPrefs.GetInt("GoldCoins");
// 替换后
TelegramPlayerPrefs.SetInt("GoldCoins", value);
TelegramPlayerPrefs.GetInt("GoldCoins");本地数据读写瞬间升级为内存缓存 + Telegram 免费云持久化 + 跨设备漫游。
演示参考
完整演示实现请参见 Packages/com.fasttma.core/Samples/04_CloudSave。