ログインしてるユーザーを保持する
ローカルストレージにログインユーザーの情報を保持する
- PlayerData.tsを編集する
PlayerData.ts
import { sys } from 'cc'; export class PlayerData { public static username: string = ""; public static highScore: number = 0; public static level: number = 1; private static readonly SAVE_KEY = 'MY_GAME_PLAYER_DATA'; public static save() { const data = { username: this.username, highScore: this.highScore, level: this.level }; sys.localStorage.setItem(this.SAVE_KEY, JSON.stringify(data)); console.log("PlayerData saved to storage."); } public static load() { const json = sys.localStorage.getItem(this.SAVE_KEY); if (json) { const data = JSON.parse(json); this.setPlayerData(data); } } public static setPlayerData(data: any) { this.username = data.username || "Guest"; this.highScore = data.highScore || 0; this.level = data.level || 1; console.log(`PlayerDataにセット: ${this.username} (Best: ${this.highScore})`); } } - MenuManager.tsでPlayerDataを読み込む処理を追記する
MenuManager.ts
onLoad() { PlayerData.load(); }
まとめ
これで一度ログインした後は更新しても、ログインしたユーザー情報が保持されたままになりました。