Sample: Saved Games
Redimentary saved games are implemented in PlayUR by allowing you to store simple Key-Value-Pairs for primitives using Unity's built-in PlayerPrefs
system. PlayUR overrides the PlayerPrefs
class, storing anything that previously would have been saved locally to PlayUR servers instead. These values will be loaded again for any repeat visits (even across different experiments and builds).
You can view the values on the Saved Games tab on the PlayUR Dashboard (once you enable the feature on the Game settings page).
This sample allows you to save some text entered in a text box. On load, the text box will contain the last saved message in the box.
using System.Collections;
using PlayUR;
using TMPro;
public class SavedGamesSample : PlayURBehaviour
{
/// <summary>
/// The input text box that stores loaded text, and is used for setting text to save
/// </summary>
public TMP_InputField input;
/// <summary>
/// The PlayerPrefs key that stores the saved value for this sample
/// </summary>
public string saveKey;
/// <summary>
/// The value to load if the saveKey is not found
/// </summary>
public string defaultValue;
public override void OnReady()
{
input.text = PlayerPrefs.GetString(saveKey, defaultValue);
}
public void Save()
{
PlayerPrefs.SetString(saveKey, input.text);
//note: no real need to call save on PlayerPrefs class, as it perioidcally saves (every 5 seconds)
//PlayerPrefs.Save();
}
}
Unity Sample Requirements
You can import this sample from the Unity Package Manager. For the code to run, you will need a game set up in Unity (see Getting Started). Additionally, you need to enable the Saved Games feature on the Game settings page.