This step is needed only if the Unity app does not run on a mobile device with the xPortal app already installed.
For the QR code to be displayed, an empty Image component needs to be created, as shown below.
Connect to xPortal
Call the Connect method as shown below.
public class Test : MonoBehaviour
{
[SerializeField] Image qrCode;
void Start()
{
MultiversX.UnityTools.API.Connect(OnConnected, OnDisconnected, OnSessionConnected, qrCode);
}
private void OnConnected(CompleteCallback<Account> result)
{
if (result.status == OperationStatus.Success)
{
//do what you want after the connection is established
//result.data is the connected account
LoadScreen(Screens.Connected);
}
else
{
Debug.LogError(result.errorMessage);
}
}
private void OnDisconnected()
{
//do what you want when disconnected
LoadScreen(Screens.Home);
}
private void OnSessionConnected(string arg0)
{
// use this method to enable the QRCode or the login button
}
}
After the Connect method is called, a web socket will be created to communicate with the xPortal app, and a QR code will be automatically generated on that blank image. After the QR code is scanned and the connection is approved inside xPortal, the OnConnected callback will be triggered.
Login on a Mobile Device
The QR code cannot be scanned if the xPortal app is on the same device, so a method called DeepLinkLogin needs to be used to communicate directly with the xPortal app.
After the Connect method from the previous point is called, and after the OnSessionConnected callback is received, the DeepLinkLogin method needs to be called to trigger the login prompt inside xPortal:
public class Test : MonoBehaviour
{
public void Login()
{
MultiversX.UnityTools.API.DeepLinkLogin();
}
}
This has the same result as the QR scanning method. After the connection is approved inside xPortal, the OnConnected callback will be triggered.
Send EGLD Transaction
To send a transaction the SendEGLDTransaction method will be used.
public class Test : MonoBehaviour
{
private string destinationAddress = "erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf";
private string EGLDToSend = "0.1";
private string optionalMessage = "Test message";
public void SendEGLD()
{
//send transaction
MultiversX.UnityTools.API.SendEGLDTransaction(destinationAddress, EGLDToSend, optionalMessage, CompleteMethod);
}
private void CompleteMethod(CompleteCallback<string[]> result)
{
switch (result.status)
{
case OperationStatus.InProgress:
Debug.Log($"Waiting for xPortal signature");
break;
case OperationStatus.Error:
Debug.LogError($"{result.errorMessage}");
break;
case OperationStatus.Success:
//result.data contains the hash of the transaction
//at this point transaction is broadcasted to the blockchain to be processed
//use this method to check if it was processed succesfully or not
MultiversX.UnityTools.API.CheckTransactionStatus(result.data, TransactionProcessed, 1);
break;
}
}
private void TransactionProcessed(CompleteCallback<Transaction[]> result)
{
switch (result.status)
{
case OperationStatus.InProgress:
foreach (Transaction transaction in result.data)
{
Debug.Log($"Tx: {transaction.TxHash} : {transaction.Status}");
}
break;
case OperationStatus.Success:
Debug.Log("Success");
break;
case OperationStatus.Error:
Debug.LogError($"{result.status} {result.errorMessage}");
foreach (Transaction transaction in result.data)
{
//the GetLogs() will return additional info about why the transaction failed.
Debug.Log($"Tx: {transaction.TxHash} : {transaction.Status} {transaction.GetLogs()}");
}
break;
}
}
}
After SendTransaction is called, the xPortal app will receive a notification to sign the transaction. If the user decides to approve the transaction, the signature will be automatically applied, and the transaction will be sent automatically to MultiversX blockchain for processing. In this case, an operationStatus of Complete will be received. If the user declines to sign or an error occurs, an operationStatus of Error will be received.
After a transaction is submitted to the blockchain, it takes some time for a validator to process it. An app needs to check if that transaction was successfully processed or not before continuing execution. This is done using the CheckTransactionStatus API method.
Disconnect
To terminate the user's active session simply call disconnect.
public class Test : MonoBehaviour
{
public void Disconnect()
{
MultiversX.UnityTools.API.Disconnect();
}
}
This will trigger the OnDisconnected method and close the web socket.
This concludes the Quick Start. Check the for advanced functionalities.