-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGroupPresenceAndInviteHandlerMotif.cs
118 lines (102 loc) · 3.92 KB
/
GroupPresenceAndInviteHandlerMotif.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) Meta Platforms, Inc. and affiliates.
#if FUSION2
using Fusion;
using UnityEngine;
using UnityEngine.UI;
using Oculus.Platform;
using Meta.XR.MultiplayerBlocks.Fusion;
using MRMotifs.SharedAssets;
namespace MRMotifs.SharedActivities.QuestPlatform
{
/// <summary>
/// The GroupPresenceAndInviteHandlerMotif class is responsible for managing group presence
/// and launching the invite panel using the Oculus Platform SDK. It allows users to set
/// their presence in a joinable state and invite friends to join them in a multiplayer session.
/// </summary>
public class GroupPresenceAndInviteHandlerMotif : MonoBehaviour
{
[Tooltip("Decide if you would like to use the Group Presence features for your experience, such as invites.")]
[SerializeField]
private bool setupGroupPresence = true;
[Header("Destination and Session Info")]
[Tooltip("Destination API Name, which can be found on the Developer Dashboard under Engagement > Destinations.")]
[SerializeField]
private string destinationApiName;
[Tooltip("Lobby Session ID.")]
[SerializeField]
private string lobbySessionId;
[Tooltip("Match Session ID.")]
[SerializeField]
private string matchSessionId;
private Button m_inviteFriendsButton;
private void Awake()
{
if (!setupGroupPresence) return;
FusionBBEvents.OnConnectedToServer += SetGroupPresence;
SetupFriendsInvite();
}
private void OnDestroy()
{
if (!setupGroupPresence) return;
FusionBBEvents.OnConnectedToServer -= SetGroupPresence;
m_inviteFriendsButton.onClick.RemoveListener(OpenInvitePanel);
}
private void SetupFriendsInvite()
{
m_inviteFriendsButton = FindAnyObjectByType<MenuPanel>().FriendsInviteButton;
m_inviteFriendsButton.onClick.AddListener(OpenInvitePanel);
}
private void SetGroupPresence(NetworkRunner obj)
{
SetGroupPresence();
}
private void OpenInvitePanel()
{
LaunchInvitePanel();
}
/// <summary>
/// Sets the group presence for the current user with the provided destination, lobby session ID,
/// and match session ID. This makes the user's session joinable, allowing other users to join the game.
/// </summary>
public void SetGroupPresence()
{
var options = new GroupPresenceOptions();
options.SetDestinationApiName(destinationApiName);
options.SetLobbySessionId(lobbySessionId);
options.SetMatchSessionId(matchSessionId);
options.SetIsJoinable(true);
GroupPresence.Set(options).OnComplete(
message =>
{
if (message.IsError)
{
Debug.LogError("Error setting group presence: " + message.GetError().Message);
}
else
{
Debug.Log("Group presence successfully set for the Chess scene!");
}
});
}
/// <summary>
/// Launches the invite panel, allowing the user to invite friends to join their current session.
/// </summary>
public void LaunchInvitePanel()
{
var options = new InviteOptions();
GroupPresence.LaunchInvitePanel(options).OnComplete(
message =>
{
if (message.IsError)
{
Debug.LogError("Error launching invite panel: " + message.GetError().Message);
}
else
{
Debug.Log("Invite panel successfully launched.");
}
});
}
}
}
#endif