-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGroupPresenceAndInviteHandlerMotif.cs
126 lines (110 loc) · 4.1 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
119
120
121
122
123
124
125
126
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if FUSION2
using Fusion;
using UnityEngine;
using UnityEngine.UI;
using Oculus.Platform;
using Meta.XR.MultiplayerBlocks.Fusion;
/// <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 _inviteFriendsButton;
private void Awake()
{
if (!setupGroupPresence) return;
FusionBBEvents.OnConnectedToServer += SetGroupPresence;
SetupFriendsInvite();
}
private void OnDestroy()
{
if (!setupGroupPresence) return;
FusionBBEvents.OnConnectedToServer -= SetGroupPresence;
_inviteFriendsButton.onClick.RemoveListener(OpenInvitePanel);
}
private void SetupFriendsInvite()
{
_inviteFriendsButton = FindObjectOfType<MenuPanel>().FriendsInviteButton;
_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