Read Roaming Databases data

Use the DatabaseConnection.Get() or DatabaseConnection.GetString() APIs to read data.

The following example continues the mouse coordinate example from the Save Roadming Databases data example to demonstrate how to add code to read the data back from the database. It logs the value to the console whenever the user presses the right mouse button.

private void Update()
    {
        const string keyMousePosition = "mousePosition";

        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos = Input.mousePosition;
            string value = JsonUtility.ToJson(mousePos);

            // Write value to database
            db.PutString(keyMousePosition, value);
            Debug.Log($"Saved {mousePos.ToString()} to database");
        }
        else if (Input.GetMouseButtonDown(1))
        {
            // Read value from database
            string value = db.GetString(keyMousePosition);

            // If not found, the value will be null
            if (value == null)
            {
                Debug.Log($"Key {keyMousePosition} not found.");
                return;
            }

            Vector3 mousePos = JsonUtility.FromJson<Vector3>(value);
            Debug.Log($"Read {mousePos.ToString()} from database");
        }
    }