using HYPLAY.Runtime;


Login

Guide: https://docs.hyplay.com/docs/auth-url-construction

Open the HYPLAY login popup.

Async

This is the preferred method.

using HYPLAY.Runtime;
public class LoginButton : MonoBehaviour
{
  public async void LoginButtonPressed ()
  {
    Debug.Log ("Login button pressed");
    await HyplayBridge.LoginAsync();
    Debug.Log ("User logged in");
  }
}

Callback

using HYPLAY.Runtime;
public class LoginButton : MonoBehaviour
{
  public void LoginButtonPressed ()
  {
    Debug.Log ("Login button pressed");
    HyplayBridge.Login(LoginCallback);
  }
  
  private void LoginCallback ()
  {
    Debug.Log ("User logged in");
  }
		
  // or
  public void LoginButtonPressedInlineCallback ()
  {
    Debug.Log ("Login button pressed");
    HyplayBridge.Login(() => {
      Debug.Log("User logged in!");
    });
  }
}

Logout

API Doc: https://docs.hyplay.com/reference/endsession

Players should be able to log out and end their session.

Async

using HYPLAY.Runtime;
public class LogoutButton : MonoBehaviour
{
  public async void LogoutButtonPressed ()
  {
    Debug.Log ("Logout button pressed");
    await HyplayBridge.LogoutAsync();
    Debug.Log ("User logged out");
  }
}

Callback

using HYPLAY.Runtime;
public class LogoutButton : MonoBehaviour
{
  public void LogoutButtonPressed ()
  {
    Debug.Log ("Logout button pressed");
    HyplayBridge.Logout(LogoutCallback);
  }
  
  private void LogoutCallback ()
  {
    Debug.Log ("User logged in");
  }
		
  // or
  public void LogoutButtonPressedInlineCallback ()
  {
    Debug.Log ("Logout button pressed");
    HyplayBridge.Logout(() => {
      Debug.Log("User logged out!");
    });
  }
}

Login and Get User

API Doc: https://docs.hyplay.com/reference/getcurrentuser

Open the HYPLAY login popup, and return a HyplayUser object after the user has completed login.

Async

This is the preferred method.

using HYPLAY.Runtime;
public class LoginButton : MonoBehaviour
{
  public async void LoginButtonPressed ()
  {
    Debug.Log ("Login button pressed");
    var res = await HyplayBridge.LoginAndGetUserAsync();
    if (res.Success)
    {
      Debug.Log ($"User {res.Data.Username} logged in");
    } else 
    {
      Debug.LogError (res.Error);
    }				
  }
}

Callback

using HYPLAY.Runtime;
public class LoginButton : MonoBehaviour
{
  public void LoginButtonPressed ()
  {
    Debug.Log ("Login button pressed");
    HyplayBridge.LoginAndGetUser(LoginCallback);
  }
  
  private void LoginCallback (HyplayResponse<HyplayUser> res)
  {
    if (res.Success)
    {
      Debug.Log ($"User {res.Data.Username} logged in");
    } else 
    {
      Debug.LogError (res.Error);
    }
  }
  // or
  public void LoginButtonPressedInlineCallback ()
  {
    Debug.Log ("Login button pressed");
    HyplayBridge.LoginAndGetUser(res => {
      if (res.Success)
      {
        Debug.Log ($"User {res.Data.Username} logged in");
      } else 
      {
        Debug.LogError (res.Error);
      }
    });
  }
}

Get User

API Doc: https://docs.hyplay.com/reference/getcurrentuser

Get the current HyplayUser.

Async

This is the preferred method.

using HYPLAY.Runtime;
public class GetUserButton : MonoBehaviour
{
  public async void GetUserButtonPressed ()
  {
    Debug.Log ("GetUser button pressed");
    var res = await HyplayBridge.GetUserAsync();
    if (res.Success)
    {
      Debug.Log ($"User {res.Data.Username} logged in");
    } else 
    {
      Debug.LogError (res.Error);
    }		
  }
}

Callback

using HYPLAY.Runtime;
public class GetUserButton : MonoBehaviour
{
  public void GetUserButtonPressed ()
  {
    Debug.Log ("GetUser button pressed");
    HyplayBridge.GetUser(GetUserCallback);
  }
  
  private void GetUserCallback (HyplayResponse<HyplayUser> res)
  {
    if (res.Success)
    {
      Debug.Log ($"User {res.Data.Username} logged in");
    } else 
    {
      Debug.LogError (res.Error);
    }
  }
  
  // or
  public void GetUserButtonPressedInlineCallback ()
  {
    Debug.Log ("GetUser button pressed");
    HyplayBridge.GetUser(res => {
      if (res.Success)
      {
        Debug.Log ($"User {res.Data.Username} logged in");
      } else 
      {
        Debug.LogError (res.Error);
      }
    });
  }
}

Get State

Guide: https://docs.hyplay.com/docs/json-database-storage

API Doc: https://docs.hyplay.com/reference/getappstate

Gets the HyplayAppState for a given key

using System;
using HYPLAY.Runtime;

// This is your arbitrary data, it can be anything
[Serializable] 
public class SaveData
{
  public int PlayerLevel = 1; // start the player at level 1
  public int PlayerCoins = 100; // give the player 100 coins to start with
}

public class CloudSave : MonoBehaviour
{
  // this key can be anything
  // the example is a cloud save system so players can take their saved state
  // with them across browsers
  [SerializeField] private string key = "cloud-save"; 
  
  private SaveData _currentSaveData;
 
  public async void Start ()
  {
    // wait until the user has logged in
    await HyplayBridge.WaitForUserLoggedIn();
    
    // the user has now logged in
    // get the state for the current key
    var res = await HyplayBridge.GetState<SaveData>(key);
    if (!res.Success)
    {
      // usually happens because the user is not logged in
      Debug.LogError (res.Error);
    } else 
    {
      // using the Protected State for this example, see the Json Database Storage guide for more info
      _currentSaveData = res.Data.ProtectedState;
      if (_currentSaveData == null)
      {
        // the user has no state
        _currentSaveData = new SaveData();
      } else {
        // we've got a cloud save!
        Debug.Log(_currentSaveData.PlayerLevel);
      }  
    }
  }
}

Set State

Guide: https://docs.hyplay.com/docs/json-database-storage

API Doc: https://docs.hyplay.com/reference/setappstate

Sets the HyplayAppState for a given key

using System;
using HYPLAY.Runtime;

// This is your arbitrary data, it can be anything
[Serializable] 
public class SaveData
{
  public int PlayerLevel = 1; // start the player at level 1
  public int PlayerCoins = 100; // give the player 100 coins to start with
}

// Building on the previous example
public class CloudSave : MonoBehaviour
{
  // this key can be anything
  // the example is a cloud save system so players can take their saved state
  // with them across browsers
  [SerializeField] private string key = "cloud-save";
  
  private SaveData _currentSaveData;
  
  public async void Save ()
  {
    // wait until the user has logged in
    await HyplayBridge.WaitForUserLoggedIn();
	
    // the user has now logged in
    // build the HyplayState object to send to HYPLAY
    // the second parameter here tells HYPLAY to overwrite the state
    var hyplayState = HyplayAppState<SaveData>.WithProtectedState(key, true, _currentSaveData);
    var res = hyplayState.SetState();
    if (!res.Success)
    {
      // usually happens because the user is not logged in
      Debug.LogError (res.Error);
    } else 
    {
      // returns the same object as Get State
      _currentSaveData = res.Data.ProtectedState;
    }
  }
}