diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/UI/IngameUI.prefab b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/UI/IngameUI.prefab index 07f1da6df..aa7738785 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/UI/IngameUI.prefab +++ b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/UI/IngameUI.prefab @@ -227,6 +227,7 @@ Transform: - {fileID: 2256034313796960807} - {fileID: 7559383771208540613} - {fileID: 8481058004063830698} + - {fileID: 6866794867799222920} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &5231826839060252271 @@ -378,3 +379,50 @@ MonoBehaviour: m_PanelMaxSize: 1 m_DisplayYOffset: 1.8 m_Camera: {fileID: 0} +--- !u!1 &8792977535300992051 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6866794867799222920} + - component: {fileID: 2710521362083790645} + m_Layer: 0 + m_Name: TextChatUI + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &6866794867799222920 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8792977535300992051} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 9.600002, y: -5.4000006, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 8617889547341104717} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2710521362083790645 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8792977535300992051} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 893a7d85794ab094798b16ae014c5d52, type: 3} + m_Name: + m_EditorClassIdentifier: + m_UIDocument: {fileID: 436460152536684898} + m_Asset: {fileID: 9197481963319205126, guid: e47914746c33245118570e272f51690a, type: 3} + m_Messages: [] diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox.meta b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox.meta new file mode 100644 index 000000000..1adf7ea1e --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a547e3b96c1db794f83e00146d14d6d2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab new file mode 100644 index 000000000..9995db9a0 --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab @@ -0,0 +1,48 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4141237635811601714 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7448202836938688200} + - component: {fileID: 8855226785007094250} + m_Layer: 0 + m_Name: VivoxManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7448202836938688200 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4141237635811601714} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &8855226785007094250 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4141237635811601714} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dde97419ba27cd44bae2dcde9387507b, type: 3} + m_Name: + m_EditorClassIdentifier: + m_ParticipantPrefab: {fileID: 0} + m_ParticipantListParent: {fileID: 0} diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab.meta b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab.meta new file mode 100644 index 000000000..17b92c905 --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Prefabs/Vivox/VivoxManager.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 53316d28950de8e4494bc48f320e09f6 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scenes/Bootstrap.unity b/Basic/DistributedAuthoritySocialHub/Assets/Scenes/Bootstrap.unity index b1bafbd5c..9c8800c15 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scenes/Bootstrap.unity +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scenes/Bootstrap.unity @@ -42,7 +42,8 @@ RenderSettings: --- !u!157 &3 LightmapSettings: m_ObjectHideFlags: 0 - serializedVersion: 12 + serializedVersion: 13 + m_BakeOnSceneLoad: 0 m_GISettings: serializedVersion: 2 m_BounceScale: 1 @@ -147,8 +148,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b5134217b751de44fb27f189c7ef29e2, type: 3} m_Name: m_EditorClassIdentifier: - m_AutoAuthenticateOnStart: 1 - m_InitiateVivoxOnAuthentication: 1 --- !u!4 &340021862 Transform: m_ObjectHideFlags: 0 @@ -223,6 +222,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 593a2fe42fa9d37498c96f9a383b6521, type: 3} m_Name: m_EditorClassIdentifier: + NetworkManagerExpanded: 0 NetworkConfig: ProtocolVersion: 0 NetworkTransport: {fileID: 900326968} @@ -246,8 +246,8 @@ MonoBehaviour: SpawnTimeout: 10 EnableNetworkLogs: 1 NetworkTopology: 1 - UseCMBService: 0 - AutoSpawnPlayerPrefabClientSide: 1 + UseCMBService: 1 + AutoSpawnPlayerPrefabClientSide: 0 NetworkMessageMetrics: 1 NetworkProfilingMetrics: 1 OldPrefabList: [] @@ -268,9 +268,67 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1001 &1475976338 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + serializedVersion: 3 + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 4141237635811601714, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_Name + value: VivoxManager + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_RemovedGameObjects: [] + m_AddedGameObjects: [] + m_AddedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 340021862} - {fileID: 900326970} + - {fileID: 1475976338} diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scenes/HubScene_TownMarket.unity b/Basic/DistributedAuthoritySocialHub/Assets/Scenes/HubScene_TownMarket.unity index 89e842ce0..dbfa62ded 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scenes/HubScene_TownMarket.unity +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scenes/HubScene_TownMarket.unity @@ -465,6 +465,63 @@ MonoBehaviour: m_LightCookieSize: {x: 1, y: 1} m_LightCookieOffset: {x: 0, y: 0} m_SoftShadowQuality: 0 +--- !u!1001 &387878127 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + serializedVersion: 3 + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 4141237635811601714, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_Name + value: VivoxManager + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7448202836938688200, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_RemovedGameObjects: [] + m_AddedGameObjects: [] + m_AddedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 53316d28950de8e4494bc48f320e09f6, type: 3} --- !u!1001 &406782382 PrefabInstance: m_ObjectHideFlags: 0 @@ -291126,3 +291183,4 @@ SceneRoots: - {fileID: 735923504} - {fileID: 8089728398451373915} - {fileID: 2053309536} + - {fileID: 387878127} diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/GameplayEventHandler.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/GameplayEventHandler.cs index d7cdf335f..56ca5cf6d 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/GameplayEventHandler.cs +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/GameplayEventHandler.cs @@ -13,8 +13,12 @@ static class GameplayEventHandler internal static event Action OnStartButtonPressed; internal static event Action OnReturnToMainMenuButtonPressed; internal static event Action OnQuitGameButtonPressed; - internal static event Action OnConnectToSessionCompleted; + internal static event Action OnConnectToSessionCompleted; internal static event Action OnExitedSession; + internal static event Action OnTextMessageReceived; + internal static event Action OnSendTextMessage; + internal static event Action OnBlockPlayerControls; + internal static event Action OnChatIsReady; internal static void NetworkObjectDespawned(NetworkObject networkObject) { @@ -41,9 +45,9 @@ internal static void QuitGamePressed() OnQuitGameButtonPressed?.Invoke(); } - internal static void ConnectToSessionComplete(Task task) + internal static void ConnectToSessionComplete(Task task, string sessionName) { - OnConnectToSessionCompleted?.Invoke(task); + OnConnectToSessionCompleted?.Invoke(task, sessionName); } internal static void ExitedSession() @@ -60,5 +64,25 @@ internal static void LoadInGameScene() { SceneManager.LoadScene("HubScene_TownMarket"); } + + internal static void ProcessTextMessageReceived(string senderName, string message, bool fromSelf) + { + OnTextMessageReceived?.Invoke(senderName, message, fromSelf); + } + + internal static void SendTextMessage(string message) + { + OnSendTextMessage?.Invoke(message); + } + + public static void BlockPlayerControls(bool disable) + { + OnBlockPlayerControls?.Invoke(disable); + } + + public static void SetTextChatReady(bool enabled) + { + OnChatIsReady?.Invoke(enabled); + } } } diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/MainMenuHandler.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/MainMenuHandler.cs index 5b7efda12..38f4c091f 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/MainMenuHandler.cs +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/GameManagement/MainMenuHandler.cs @@ -15,7 +15,7 @@ void OnDestroy() GameplayEventHandler.OnConnectToSessionCompleted -= OnConnectToSessionCompleted; } - void OnConnectToSessionCompleted(Task task) + void OnConnectToSessionCompleted(Task task, string sessionName) { if (task.IsCompletedSuccessfully) { diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Player/AvatarTransform.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Player/AvatarTransform.cs index 6df387d3a..728409068 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Player/AvatarTransform.cs +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Player/AvatarTransform.cs @@ -1,5 +1,6 @@ using System; using Unity.Collections; +using Unity.Multiplayer.Samples.SocialHub.GameManagement; using Unity.Multiplayer.Samples.SocialHub.Gameplay; using UnityEngine; using Unity.Multiplayer.Samples.SocialHub.Input; @@ -71,6 +72,8 @@ public override void OnNetworkSpawn() Debug.LogError("CameraControl not found on the Main Camera or Main Camera is missing."); } + GameplayEventHandler.OnBlockPlayerControls += OnBlockPlayerControls; + base.OnNetworkSpawn(); } @@ -91,6 +94,7 @@ public override void OnNetworkDespawn() cameraControl.SetTransform(null); } + GameplayEventHandler.OnBlockPlayerControls -= OnBlockPlayerControls; m_TopUIController?.RemovePlayer(gameObject); } @@ -122,6 +126,11 @@ void OnPlayerNameChanged(FixedString32Bytes previousValue, FixedString32Bytes ne m_TopUIController.AddPlayer(gameObject, newValue.Value); } + void OnBlockPlayerControls(bool blockInput) + { + m_PlayerInput.enabled = !blockInput; + } + public void NetworkUpdate(NetworkUpdateStage updateStage) { switch (updateStage) diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/ServicesHelper.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/ServicesHelper.cs index 5da7c9dc9..6238c3667 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/ServicesHelper.cs +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/ServicesHelper.cs @@ -5,16 +5,12 @@ using Unity.Services.Authentication; using Unity.Services.Core; using Unity.Services.Multiplayer; -using Unity.Services.Vivox; using UnityEngine; namespace Unity.Multiplayer.Samples.SocialHub.Services { class ServicesHelper : MonoBehaviour { - [SerializeField] - bool m_InitiateVivoxOnAuthentication; - static bool s_InitialLoad; Task m_SessionTask; @@ -40,37 +36,71 @@ async void Start() NetworkManager.Singleton.OnClientStopped += OnClientStopped; GameplayEventHandler.OnStartButtonPressed += OnStartButtonPressed; - GameplayEventHandler.OnReturnToMainMenuButtonPressed += OnReturnToMainMenuButtonPressed; + GameplayEventHandler.OnReturnToMainMenuButtonPressed += LeaveSession; GameplayEventHandler.OnQuitGameButtonPressed += OnQuitGameButtonPressed; + + await VivoxManager.Instance.Initialize(); } - void OnClientStopped(bool obj) + async void OnStartButtonPressed(string playerName, string sessionName) { - LeaveSession(); + var connectTask = ConnectToSession(playerName, sessionName); + await connectTask; + GameplayEventHandler.ConnectToSessionComplete(connectTask, sessionName); } - void OnDestroy() + async Task ConnectToSession(string playerName, string sessionName) { - if (NetworkManager.Singleton) + if (AuthenticationService.Instance == null) { - NetworkManager.Singleton.OnClientStopped -= OnClientStopped; + return; } - GameplayEventHandler.OnStartButtonPressed -= OnStartButtonPressed; - GameplayEventHandler.OnReturnToMainMenuButtonPressed -= OnReturnToMainMenuButtonPressed; - GameplayEventHandler.OnQuitGameButtonPressed -= OnQuitGameButtonPressed; + if (!AuthenticationService.Instance.IsSignedIn) + { + await SignIn(); + } + + await AuthenticationService.Instance.UpdatePlayerNameAsync(playerName); + Debug.Log($"Signed in anonymously after. Name: {name}. ID: {AuthenticationService.Instance.PlayerName}"); + + if (string.IsNullOrEmpty(sessionName)) + { + Debug.LogError("Session name is empty. Cannot connect."); + return; + } + + await ConnectThroughLiveService(sessionName); } - async void OnStartButtonPressed(string playerName, string sessionName) + async Task SignIn() { - var connectTask = ConnectToSession(playerName, sessionName); - await connectTask; - GameplayEventHandler.ConnectToSessionComplete(connectTask); + try + { + AuthenticationService.Instance.SignInFailed += SignInFailed; + AuthenticationService.Instance.SwitchProfile(GetRandomString(5)); + await AuthenticationService.Instance.SignInAnonymouslyAsync(); + Debug.Log($"Signed in anonymously before. Name: {name}. ID: {AuthenticationService.Instance.PlayerName}"); + } + catch (Exception e) + { + Debug.LogException(e); + throw; + } } - void OnReturnToMainMenuButtonPressed() + async Task ConnectThroughLiveService(string sessionName) { - LeaveSession(); + // Join Session + var options = new SessionOptions() + { + Name = sessionName, + MaxPlayers = 64, + }.WithDistributedAuthorityNetwork(); + + m_CurrentSession = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionName, options); + m_CurrentSession.RemovedFromSession += RemovedFromSession; + m_CurrentSession.StateChanged += CurrentSessionOnStateChanged; } void OnQuitGameButtonPressed() @@ -103,129 +133,61 @@ async void LeaveSession() void SignInFailed(RequestFailedException obj) { - Debug.LogWarning($"{nameof(SignedIn)} obj.ErrorCode {obj.ErrorCode}"); + Debug.LogWarning($"Sign in via Authentication failed: obj.ErrorCode {obj.ErrorCode}"); } - void SignedIn() - { - if (m_InitiateVivoxOnAuthentication) - { - LogInToVivox(); - } - } - - async void LogInToVivox() + void RemovedFromSession() { - await VivoxService.Instance.InitializeAsync(); - - var options = new LoginOptions - { - DisplayName = AuthenticationService.Instance.Profile, - EnableTTS = true - }; - VivoxService.Instance.LoggedIn += LoggedInToVivox; - await VivoxService.Instance.LoginAsync(options); + ExitedSession(); } - static string GetRandomString(int length) + void CurrentSessionOnStateChanged(SessionState sessionState) { - var r = new System.Random(); - var result = new char[length]; - - for (int i = 0; i < length; i++) + if (sessionState != SessionState.Connected) { - result[i] = (char)r.Next('a', 'z' + 1); + ExitedSession(); } - - return new string(result); } - void LoggedInToVivox() - { - Debug.Log(nameof(LoggedInToVivox)); - } - - async Task SignIn() + void ExitedSession() { - try - { - AuthenticationService.Instance.SignInFailed += SignInFailed; - AuthenticationService.Instance.SignedIn += SignedIn; - AuthenticationService.Instance.SwitchProfile(GetRandomString(5)); - await AuthenticationService.Instance.SignInAnonymouslyAsync(); - } - catch (Exception e) + if (m_CurrentSession != null) { - Debug.LogException(e); - throw; + m_CurrentSession.RemovedFromSession -= RemovedFromSession; + m_CurrentSession.StateChanged -= CurrentSessionOnStateChanged; + m_CurrentSession = null; + GameplayEventHandler.ExitedSession(); } } - async Task ConnectToSession(string playerName, string sessionName) + void OnClientStopped(bool obj) { - if (AuthenticationService.Instance == null) - { - return; - } - - if (!AuthenticationService.Instance.IsSignedIn) - { - await SignIn(); - } - - await AuthenticationService.Instance.UpdatePlayerNameAsync(playerName); - - if (string.IsNullOrEmpty(sessionName)) - { - Debug.LogError("Session name is empty. Cannot connect."); - return; - } - - await ConnectThroughLiveService(sessionName); + LeaveSession(); } - async Task ConnectThroughLiveService(string sessionName) + void OnDestroy() { - try - { - var options = new SessionOptions() - { - Name = sessionName, - MaxPlayers = 64, - }.WithDistributedAuthorityNetwork(); - - m_CurrentSession = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionName, options); - m_CurrentSession.RemovedFromSession += RemovedFromSession; - m_CurrentSession.StateChanged += CurrentSessionOnStateChanged; - } - catch (Exception e) + if (NetworkManager.Singleton) { - Debug.LogException(e); + NetworkManager.Singleton.OnClientStopped -= OnClientStopped; } - } - void RemovedFromSession() - { - ExitedSession(); + GameplayEventHandler.OnStartButtonPressed -= OnStartButtonPressed; + GameplayEventHandler.OnReturnToMainMenuButtonPressed -= LeaveSession; + GameplayEventHandler.OnQuitGameButtonPressed -= OnQuitGameButtonPressed; } - void CurrentSessionOnStateChanged(SessionState sessionState) + static string GetRandomString(int length) { - if (sessionState != SessionState.Connected) - { - ExitedSession(); - } - } + var r = new System.Random(); + var result = new char[length]; - void ExitedSession() - { - if (m_CurrentSession != null) + for (var i = 0; i < length; i++) { - m_CurrentSession.RemovedFromSession -= RemovedFromSession; - m_CurrentSession.StateChanged -= CurrentSessionOnStateChanged; - m_CurrentSession = null; - GameplayEventHandler.ExitedSession(); + result[i] = (char)r.Next('a', 'z' + 1); } + + return new string(result); } } } diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/Unity.Multiplayer.Samples.SocialHub.Services.asmdef b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/Unity.Multiplayer.Samples.SocialHub.Services.asmdef index 75876aea7..2adb0fab0 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/Unity.Multiplayer.Samples.SocialHub.Services.asmdef +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/Unity.Multiplayer.Samples.SocialHub.Services.asmdef @@ -7,7 +7,8 @@ "GUID:37e17ffe38d86ae48bc3207e83ffef88", "GUID:6087a74f6015aae4daed9a2577a7596c", "GUID:1491147abca9d7d4bb7105af628b223e", - "GUID:707bb6c7c4ef140eeac239b4f8251af8" + "GUID:707bb6c7c4ef140eeac239b4f8251af8", + "GUID:75469ad4d38634e559750d17036d5f7c" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs new file mode 100644 index 000000000..40fbbfc0f --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs @@ -0,0 +1,96 @@ +using System; +using System.Threading.Tasks; +using Unity.Multiplayer.Samples.SocialHub.GameManagement; +using Unity.Services.Authentication; +using UnityEngine; +using Unity.Services.Vivox; + +namespace Unity.Multiplayer.Samples.SocialHub.Services +{ + class VivoxManager : MonoBehaviour + { + internal static VivoxManager Instance { get; set; } + string ChannelName { get; set; } + + void Awake() + { + if (Instance == null) + { + Instance = this; + DontDestroyOnLoad(gameObject); + } + else + { + Destroy(gameObject); + } + } + + internal async Task Initialize() + { + await VivoxService.Instance.InitializeAsync(); + GameplayEventHandler.OnConnectToSessionCompleted += LoginVivox; + VivoxService.Instance.LoggedIn += OnLoggedInVivox; + VivoxService.Instance.ChannelJoined += OnChannelJoined; + GameplayEventHandler.OnExitedSession += LogoutVivox; + } + + async void LoginVivox(Task t, string sessionName) + { + ChannelName = sessionName; + await VivoxService.Instance.InitializeAsync(); + var loginOptions = new LoginOptions() + { + DisplayName = AuthenticationService.Instance.PlayerName, + PlayerId = AuthenticationService.Instance.PlayerId + }; + await VivoxService.Instance.LoginAsync(loginOptions); + } + + async void OnLoggedInVivox() + { + await JoinChannel(ChannelName); + } + + async Task JoinChannel(string channelName) + { + var channelOptions = new ChannelOptions(); + + await VivoxService.Instance.JoinGroupChannelAsync(channelName, ChatCapability.TextAndAudio, channelOptions); + GameplayEventHandler.OnSendTextMessage -= SendVivoxMessage; + GameplayEventHandler.OnSendTextMessage += SendVivoxMessage; + VivoxService.Instance.ChannelMessageReceived -= OnMessageReceived; + VivoxService.Instance.ChannelMessageReceived += OnMessageReceived; + } + + void OnChannelJoined(string channelName) + { + GameplayEventHandler.SetTextChatReady(true); + } + + async void LogoutVivox() + { + GameplayEventHandler.SetTextChatReady(false); + await VivoxService.Instance.LogoutAsync(); + } + + async void SendVivoxMessage(string message) + { + await VivoxService.Instance.SendChannelTextMessageAsync(VivoxManager.Instance.ChannelName, message); + } + + void OnMessageReceived(VivoxMessage vivoxMessage) + { + var senderName = vivoxMessage.SenderDisplayName.Split("#")[0]; + GameplayEventHandler.ProcessTextMessageReceived(senderName, vivoxMessage.MessageText, vivoxMessage.FromSelf); + } + + void OnDestroy() + { + GameplayEventHandler.OnSendTextMessage -= SendVivoxMessage; + VivoxService.Instance.ChannelMessageReceived -= OnMessageReceived; + GameplayEventHandler.OnExitedSession -= LogoutVivox; + GameplayEventHandler.OnConnectToSessionCompleted -= LoginVivox; + VivoxService.Instance.LoggedIn -= OnLoggedInVivox; + } + } +} diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs.meta b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs.meta new file mode 100644 index 000000000..be0ec1b8c --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/Services/VivoxManager.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: dde97419ba27cd44bae2dcde9387507b \ No newline at end of file diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/HomeScreenView.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/HomeScreenView.cs index 3da9aa1f1..d504019a3 100644 --- a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/HomeScreenView.cs +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/HomeScreenView.cs @@ -78,9 +78,9 @@ void HandleQuitButtonPressed() GameplayEventHandler.QuitGamePressed(); } - void OnConnectToSessionCompleted(Task obj) + void OnConnectToSessionCompleted(Task task, string sessionName ) { - if (!obj.IsCompletedSuccessfully) + if (!task.IsCompletedSuccessfully) { m_StartButton.enabledSelf = true; } diff --git a/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/IngameUI/TextChatManager.cs b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/IngameUI/TextChatManager.cs new file mode 100644 index 000000000..99301ec78 --- /dev/null +++ b/Basic/DistributedAuthoritySocialHub/Assets/Scripts/UI/IngameUI/TextChatManager.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using Unity.Properties; +using UnityEngine; +using UnityEngine.UIElements; +using static Unity.Multiplayer.Samples.SocialHub.GameManagement.GameplayEventHandler; + +namespace Unity.Multiplayer.Samples.SocialHub.UI +{ + class TextChatManager : MonoBehaviour + { + [SerializeField] + UIDocument m_UIDocument; + + [SerializeField] + VisualTreeAsset m_Asset; + + // Serializable for Bindings. + [SerializeField, HideInInspector] + List m_Messages = new(); + + ListView m_MessageView; + TextField m_MessageInputField; + Button m_SendButton; + VisualElement m_Root; + VisualElement m_TextChatView; + + bool m_IsChatActive; + + void Start() + { + + m_Root = m_UIDocument.rootVisualElement.Q("textchat-container"); + m_Asset.CloneTree(m_Root); + + m_Root.Q