Exemplo básico de como usar a API no Unity:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class GameAuthManager : MonoBehaviour
{
public string apiBaseUrl = "https://neuroworkplay.com//api";
private string authToken;
public void Login(string email, string password)
{
StartCoroutine(LoginCoroutine(email, password));
}
private IEnumerator LoginCoroutine(string email, string password)
{
var loginData = new {
email = email,
password = password
};
string json = JsonUtility.ToJson(loginData);
using (UnityWebRequest request = UnityWebRequest.Post(apiBaseUrl + "/login", json, "application/json"))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var response = JsonUtility.FromJson<LoginResponse>(request.downloadHandler.text);
if (response.success)
{
authToken = response.data.token;
Debug.Log("Login realizado com sucesso!");
}
}
}
}
public void CheckGameAccess(int gameId)
{
StartCoroutine(CheckAccessCoroutine(gameId));
}
private IEnumerator CheckAccessCoroutine(int gameId)
{
var accessData = new { game_id = gameId };
string json = JsonUtility.ToJson(accessData);
using (UnityWebRequest request = UnityWebRequest.Post(apiBaseUrl + "/check-access", json, "application/json"))
{
request.SetRequestHeader("Authorization", "Bearer " + authToken);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var response = JsonUtility.FromJson<AccessResponse>(request.downloadHandler.text);
if (response.data.has_access)
{
Debug.Log("Usuário tem acesso ao game!");
// Liberar acesso ao conteúdo do game
}
else
{
Debug.Log("Usuário não tem acesso ao game!");
// Mostrar tela de bloqueio
}
}
}
}
}