Virtual Pet Game Template
  • VIRTUAL PET GAME TEMPLATE
  • Getting Started
    • ๐ŸŽฏ Project Setup Guide
    • ๐Ÿงฉ Home Scene Overview
    • ๐Ÿ—บ๏ธ UI Main Menu
    • ๐Ÿ—บ๏ธ LoadingManager
    • ๐Ÿ—บ๏ธ GameInstance
    • ๐Ÿ—บ๏ธ ToolsManager
    • ๐Ÿ—บ๏ธ AudioManager
    • ๐Ÿ—บ๏ธ LanguageManager
    • ๐Ÿ—บ๏ธ MonetizationManager
    • ๐Ÿ—บ๏ธ PlayerSave
  • Modifying the project
    • ๐Ÿพ How to Add a New Pet
    • ๐Ÿ“œHow Add a New Pet Rules
    • ๐Ÿ’ฐ How to Add a New Currency
    • ๐ŸŽ How to Add a New Food Item
    • ๐Ÿงข Adding a New Accessory
    • ๐ŸŽฎ How to Add a New Minigame
  • Modifying Minigames
    • ๐ŸŒค๏ธ Minigame "Jump Sky"
    • ๐Ÿ” Minigame "Food Flicker"
    • ๐Ÿฆ Minigame โ€œFlappyโ€
    • ๐Ÿ’Ž Minigame โ€œGem Hunterโ€
Powered by GitBook
On this page
  • ๐Ÿ’พ What It Does
  • ๐Ÿง  How It Works
  • ๐Ÿ”ง Built-In Keys
  • ๐Ÿ“ฅ Saving Data
  • ๐Ÿ“ค Reading Data
  • ๐Ÿ“ฆ Item System (Dictionary-based)
  • ๐ŸŽฎ Scores, Stats & Coins
  • ๐ŸŽจ Clothes, Food & Color
  • โฑ Offline Stat Decay
  • ๐Ÿงช Switching Save Backend
  • โœ… Highlights
  1. Getting Started

๐Ÿ—บ๏ธ PlayerSave

PlayerSave โ€“ Persistent Save System

The PlayerSave class is a static high-level API for saving and retrieving all persistent data in the game, such as nickname, level, items, coins, stats, color, selected clothes, and more.

It automatically handles encryption, serialization (for complex types), and saving behind the scenes through the configured ILocalSaveProvider. No need to manually call PlayerPrefs.Save().


๐Ÿ’พ What It Does

  • Stores and retrieves player data using SetString, SetInt, SetFloat (encrypted under the hood)

  • Automatically saves after every change

  • Supports:

    • ๐Ÿง Nickname, pet ID, level, EXP

    • ๐Ÿ“ฆ Item quantities (dictionary-based)

    • ๐ŸŽฎ High Scores

    • ๐Ÿ›’ Selected clothes and food

    • ๐Ÿงผ Stat values (like hygiene, hunger)

    • ๐Ÿ’ฐ Multiple coin types (by ID)

    • ๐ŸŽจ Pet color customization

    • โฑ Offline stat decay timestamp

  • Raises events like OnCurrencyChanged, OnStatsChanged, OnExpChanged


๐Ÿง  How It Works

  • Uses an ILocalSaveProvider (default: PlayerPrefsSaveProvider)

  • Each call to SetInt, SetString, etc.:

    • Encrypts and stores the value

    • Automatically triggers provider.Save()

  • Uses JSON serialization for complex data (e.g., inventory)

  • Data is saved securely in PlayerPrefs, but can be swapped to a cloud or custom provider


๐Ÿ”ง Built-In Keys

Category
Key Format
Example

Nickname

PLAYER_NICK

"FluffyCat"

Pet ID

PLAYER_PET

0, 1, 2...

Level / Exp

PLAYER_LVL, PLAYER_EXP

5, 1500

Stat

STAT_{statName}

STAT_Hygiene

Coins

COIN_{coinId}

COIN_Gold

Scores

SCORE_{mode}

SCORE_FlappyPet

Items

ITEMS_JSON

JSON-encoded inventory

Food

SELECTED_FOOD

"apple_01"

Clothes

SELECTED_CLOTHES_{slot}

"hat_02"

Pet Color

PET_COLOR

#FFCC99FF

Last Update

PET_LAST_STAT_UPDATE

UTC Ticks (as string)


๐Ÿ“ฅ Saving Data

You donโ€™t need to call PlayerPrefs.Save() โ€” just use the functions below:

PlayerSave.SetString("customKey", "someValue");
PlayerSave.SetInt("someNumber", 5);
PlayerSave.SetFloat("someFloat", 0.75f);

This will encrypt, store, and save automatically.


๐Ÿ“ค Reading Data

You can retrieve saved values using the matching Get functions:

string name = PlayerSave.GetString("PLAYER_NICK");
int hygiene = PlayerSave.GetStat("Hygiene");
int coins = PlayerSave.GetCoin("Gold");

๐Ÿ“ฆ Item System (Dictionary-based)

Items are stored in a JSON dictionary behind the scenes:

// Add items
PlayerSave.AddItems(new Dictionary<string, int> {
  { "food_apple", 3 },
  { "hat_red", 1 }
});

// Check quantity
int hats = PlayerSave.GetItemCount("hat_red");

// Remove items
PlayerSave.RemoveItems(new Dictionary<string, int> {
  { "food_apple", 1 }
});

๐ŸŽฎ Scores, Stats & Coins

PlayerSave.SetBestScore("FlappyPet", 1500);
int score = PlayerSave.GetBestScore("FlappyPet");

PlayerSave.SetStat("Hygiene", 20, 100);
int hygiene = PlayerSave.GetStat("Hygiene");

PlayerSave.AddCoin("Gold", 10);
PlayerSave.RemoveCoin("Gold", 5);
int gold = PlayerSave.GetCoin("Gold");

๐ŸŽจ Clothes, Food & Color

PlayerSave.SetSelectedClothes("head", "hat_red");
PlayerSave.SetSelectedFood("food_apple");

Color petColor = PlayerSave.GetPetColor();
PlayerSave.SetPetColor(Color.cyan);

โฑ Offline Stat Decay

PlayerSave.SaveLastTimestamp(DateTime.UtcNow);
string lastUpdate = PlayerSave.GetLastTimestamp();

Used to calculate how much time has passed offline for stat decay (hunger, hygiene, etc.)


๐Ÿงช Switching Save Backend

If needed, you can replace the save provider with your own implementation:

PlayerSave.SetProvider(new MyCustomSaveProvider());

Your class must implement ILocalSaveProvider.


โœ… Highlights

  • ๐Ÿ” Encrypted & auto-saving system

  • ๐Ÿ”ง Uses standard APIs: SetString, GetInt, etc.

  • ๐Ÿงฑ Structured: supports stats, coins, scores, food, clothes

  • ๐Ÿ”„ Ready for cloud migration (via provider interface)

Previous๐Ÿ—บ๏ธ MonetizationManagerNext๐Ÿพ How to Add a New Pet

Last updated 28 days ago