Unity: Parties #
This tutorial uses Unity 2021.3 with Pragma Engine 0.4.0 to demonstrate integrating Pragma Engine party functionality with a third-party game engine. This guide assumes you are proficient with Unity.
After creating a Party Plugin in Pragma Engine, you can build the corresponding frontend in Unity with help from the Pragma Party API.
In this section, we’ll expand the PragmaManager.cs
file we created in the Create Unity Project Files Unity tutorial to implement party functionality. By the end of the tutorial, our game client will be able to create, join, and leave parties, as well as make game mode and character selections.
There are many ways to apply the Pragma logic to Unity UI elements. This tutorial provides a quick way to link UI elements such as buttons and input fields with the Pragma SDK and backend.
To test the functionality in this topic, you’ll need to build your Unity project and launch at least two executables.
Update SDK #
Because there were changes to the ext protos, re-run the update script command from your project’s root directory:
update-pragma-sdk.sh
Create the party #
Goal #
Allow a player to create a party from a Unity project.
Upon party creation, parties are automatically assigned a unique party ID and invite code. In our example, we have the CreateParty()
function return the party ID and invite code for easy access.
Steps #
In
PragmaManager.cs
, add aCreateParty()
method:public void CreateParty() { Player.PartyApi.CreateParty(new ExtCreateRequest(), new ExtPlayerJoinRequest(), result => { if (result.IsFailure) { Debug.Log("Pragma -- Create party failed"); } else { Debug.Log("Pragma -- Party created"); Debug.Log("Pragma -- Party ID: " + Player.PartyApi.GetPartyCache().GetParty().Id); Debug.Log("Pragma -- Party invite code: " + Player.PartyApi.GetPartyCache().GetParty().InviteCode); } } ); }
Our
CreateParty()
method calls the PartyApi’sCreateParty()
SDK method, which handles creating a party on the backend. Our example does not accept any extra data for party creation, but you can addExtCreateRequest
andExtPlayerJoinRequest
parameters to allow for party customization during the time of creation.Our function prints out the party’s invite code, which is set on the party and can be distributed to allow others to join the party (see Join party with invite code).
In the Unity editor, create a UI button called
Create Party
and hook it up to the PragmaManagerCreateParty()
method when clicked.
Test #
To test this functionality in the Unity editor, log in using test01
and click the Create Party
button.
If successful, you’ll see “Pragma – Party created”, as well as the party ID and invite code, in the Unity console.
Join party with invite code #
Goal #
Allow a player to join a party using the party’s public invite code.
Upon party creation, parties are automatically assigned a unique invite code. To access this value, you can have the CreateParty()
function return the party invite code and store it locally, or access the code with a call to Player.PartyApi.GetPartyCache().GetParty().InviteCode
.
Steps #
In
PragmaManager.cs
, above theAwake()
method, declare anInputInviteCode
input field. This is where users will enter the invite code for the party they want to join. We’ll connect this to a Unity UI element in a later step.public InputField InputInviteCode;
In
PragmaManager.cs
, define theJoinPartyWithInviteCode()
method using the Pragma SDKJoinPartyWithInviteCode()
method and text from theInputInviteCode
field:public void JoinPartyWithInviteCode() { string partyInviteCode = InputInviteCode.text; ExtPlayerJoinRequest playerJoinRequest = new ExtPlayerJoinRequest (); Player.PartyApi.JoinPartyWithInviteCode(playerJoinRequest, partyInviteCode, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Party joined with invite code: " + partyInviteCode); } else { Debug.Log("Pragma -- Could not join party"); } }); }
In yor Unity editor:
a. Create an input field where players can enter the invite code for the party they want to join, and connect it to the Pragma Manager
InputInviteCode
:b. Create a Join by invite code button and connect it to the Pragma Manager
JoinPartyWithInviteCode()
when clicked:
Send party invite #
Goal #
Allow a player to send a party invite to another player using the invitee’s Pragma ID.
Steps #
In
PragmaManager.cs
, above theAwake()
method, declare aPartyInviteeIdInput
input field. This is where users will enter the invitee’s Pragma ID. We’ll connect this to a Unity UI element in a later step.public InputField PartyInviteeIdInput;
In
PragmaManager.cs
, define theSendPartyInvite()
method using the Pragma SDKSendPartyInvite()
method and thePartyInviteeIdInput
input:public void SendPartyInvite() { PragmaId inviteePragmaId = new PragmaId(PartyInviteeIdInput.text); Player.PartyApi.SendPartyInvite(inviteePragmaId, result => { if (result.IsSuccessful) { Debug.Log("Invite sent"); } else { Debug.Log("Could not send invite"); } }); }
In yor Unity editor:
a. Create an input field where players can enter an invitee’s Pragma ID, and connect it with the Pragma Manager
PartyInviteeIdInput
.b. Create a Send party invite button and connect it to the Pragma Manager
SendPartyInvite()
method when clicked.
Accept or decline a party invite #
Goal #
Allow players to accept or decline a received party invite.
Party invites are identified by the inviter’s PragmaId. Invitees respond to party invites by providing the inviter’s Pragma Id to the AcceptPartyInvite()
and DeclinePartyInvite()
SDK methods.
Steps #
In
PragmaManager.cs
, above theAwake()
method, declare aPartyInviteId
input field. This is where the player will enter the invite ID they want to respond to. We’ll connect this to a Unity UI element in a later step.public InputField PartyInviteId;
In
PragmaManager.cs
, define anAcceptPartyInvite()
andDeclinePartyInvite()
method using the Pragma SDKAcceptPartyInvite()
andDeclinePartyInvite()
methods with thePartyInviteId
input:public void AcceptPartyInvite() { PragmaId pragmaInviteId = new PragmaId(partyInviteId.text); ExtPlayerJoinRequest playerJoinRequest = new ExtPlayerJoinRequest (); Player.PartyApi.AcceptPartyInvite(pragmaInviteId, playerJoinRequest, result => { if (result.IsSuccessful) { Debug.Log("Invite accepted"); } else { Debug.Log("Could not be accepted"); } }); } public void DeclinePartyInvite() { PragmaId pragmaInviteId = new PragmaId(partyInviteId.text); Player.PartyApi.DeclinePartyInvite(pragmaInviteId, result => { if (result.IsSuccessful) { Debug.Log("Invite declined"); } else { Debug.Log("Could not be declined"); } }); }
In yor Unity editor:
a. Create an input field called
PartyInviteId
and link it to the Pragma ManagerPartyInviteId
.b. Create an Accept invite button and connect it to the Pragma Manager
AcceptInvite()
method when clicked.c. Create a Decline invite button and connect it to the Pragma Manager
DeclineInvite()
method when clicked.
Make game mode selections #
Goal #
Allow party leaders to select the mode for their game. Game mode options are based on the values defined in the Set Up Parties with Pragma Engine tutorial.
Steps #
In
PragmaManager.cs
, define aSetGameModeCasual()
andSetGameModeRanked()
method:public void SetGameModeCasual() { ExtUpdatePartyRequest updatePartyRequest = new ExtUpdatePartyRequest { NewGameMode = GameMode.Casual }; Player.PartyApi.UpdateParty(updatePartyRequest, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Game mode set" + updatePartyRequest); } else { Debug.Log("Pragma -- Could not set game mode"); } }); } public void SetGameModeRanked() { ExtUpdatePartyRequest updatePartyRequest = new ExtUpdatePartyRequest { NewGameMode = GameMode.Ranked }; Player.PartyApi.UpdateParty(updatePartyRequest, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Game mode set" + updatePartyRequest); } else { Debug.Log("Pragma -- Could not set game mode"); } }); }
Method details
Our
SetGameMode()
methods update theExtUpdatePartyRequest
proto with a new game mode selection based on the button clicked. We then use theUpdateParty()
SDK method with the updatedExtUpdatePartyRequest
to update the party.In your Unity editor, create buttons for each game mode option (CASUAL or RANKED), and link them to the appropriate Pragma Manager method.
Make character selections #
Goal #
Allow players to select a character. Character options are based on the values defined in the Set Up Parties with Pragma Engine tutorial.
Steps #
In
PragmaManager.cs
, define aSetCharacterKnight()
andSetCharacterMage()
andSetCharacterRouge()
method:public void SetCharacterKnight() { ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest { NewCharacter = Character.Knight }; Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest); } else { Debug.Log("Pragma -- Could not set character"); } }); } public void SetCharacterMage() { ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest { NewCharacter = Character.Mage }; Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest); } else { Debug.Log("Pragma -- Could not set character"); } }); } public void SetCharacterRogue() { ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest { NewCharacter = Character.Rogue }; Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result => { if (result.IsSuccessful) { Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest); } else { Debug.Log("Pragma -- Could not set character"); } }); }
Method details
Our
SetCharacter()
methods update theExtUpdatePartyPlayerRequest
proto with a new character selection based on the button clicked. We then use theUpdatePartyPlayer()
SDK method with the updatedExtUpdatePartyPlayerRequest
to update the party player.In your Unity editor, create buttons for each character options (KNIGHT, MAGE, or ROGUE), and link them to the appropriate Pragma Manager method.
Leave party #
Goal #
Allow players to leave a party.
Steps #
In
PragmaManager.cs
, define aLeaveParty()
that uses theLeaveParty()
SDK method:public void LeaveParty() { Player.PartyApi.LeaveParty(response => { if (response.IsSuccessful) { Debug.Log("Pragma -- Left party"); } else { Debug.Log("Pragma -- Could not leave party "); } }); }
In the Unity editor, create a Leave party button and hook it up to the PragmaManager
LeaveParty()
method when clicked.