Init
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
using System.Collections.Generic;
|
||||
using Blocks.Common;
|
||||
using Blocks.Sessions.Common;
|
||||
using Unity.Properties;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions
|
||||
{
|
||||
[UxmlElement]
|
||||
public partial class JoinSessionByCode : VisualElement
|
||||
{
|
||||
const string k_SessionCodeTextFieldPlaceholder = "Enter Session Code";
|
||||
const string k_JoinButtonText = "JOIN";
|
||||
|
||||
[UxmlAttribute, CreateProperty]
|
||||
SessionSettings SessionSettings
|
||||
{
|
||||
get => m_SessionSettings;
|
||||
set
|
||||
{
|
||||
if (m_SessionSettings == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_SessionSettings = value;
|
||||
if (panel != null)
|
||||
{
|
||||
UpdateBindings();
|
||||
}
|
||||
}
|
||||
}
|
||||
SessionSettings m_SessionSettings;
|
||||
|
||||
JoinSessionByCodeViewModel m_ViewModel;
|
||||
|
||||
readonly List<DataBinding> m_Bindings = new();
|
||||
|
||||
public JoinSessionByCode()
|
||||
{
|
||||
AddToClassList(BlocksTheme.ContainerHorizontal);
|
||||
|
||||
var sessionCodeTextField = new TextField
|
||||
{
|
||||
textEdition =
|
||||
{
|
||||
placeholder = k_SessionCodeTextFieldPlaceholder,
|
||||
hidePlaceholderOnFocus = true
|
||||
}
|
||||
};
|
||||
sessionCodeTextField.AddToClassList(BlocksTheme.TextField);
|
||||
sessionCodeTextField.AddToClassList(BlocksTheme.SpaceRight);
|
||||
var sessionCodeBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(m_ViewModel.SessionCode)),
|
||||
bindingMode = BindingMode.ToSource
|
||||
};
|
||||
sessionCodeTextField.SetBinding("value", sessionCodeBinding);
|
||||
Add(sessionCodeTextField);
|
||||
m_Bindings.Add(sessionCodeBinding);
|
||||
|
||||
var createSessionButton = new Button
|
||||
{
|
||||
text = k_JoinButtonText
|
||||
};
|
||||
createSessionButton.AddToClassList(BlocksTheme.Button);
|
||||
var createSessionBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(m_ViewModel.CanJoinSession)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
createSessionButton.SetBinding(new BindingId(nameof(enabledSelf)), createSessionBinding);
|
||||
createSessionButton.clicked += JoinSession;
|
||||
Add(createSessionButton);
|
||||
m_Bindings.Add(createSessionBinding);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
|
||||
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
|
||||
}
|
||||
|
||||
void JoinSession()
|
||||
{
|
||||
if (!m_ViewModel.AreMultiplayerServicesInitialized())
|
||||
{
|
||||
Debug.LogError("Multiplayer Services are not initialized. You can initialize them with default settings by adding a Servicesinitialization and PlayerAuthentication components in your scene.");
|
||||
return;
|
||||
}
|
||||
|
||||
_ = m_ViewModel.JoinSessionByCodeAsync(m_SessionSettings.ToJoinSessionOptions());
|
||||
}
|
||||
|
||||
void UpdateBindings()
|
||||
{
|
||||
CleanupBindings();
|
||||
|
||||
m_ViewModel = new JoinSessionByCodeViewModel(m_SessionSettings?.sessionType);
|
||||
foreach (var binding in m_Bindings)
|
||||
{
|
||||
binding.dataSource = m_ViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
void CleanupBindings()
|
||||
{
|
||||
m_ViewModel?.Dispose();
|
||||
m_ViewModel = null;
|
||||
|
||||
foreach (var binding in m_Bindings)
|
||||
{
|
||||
binding.dataSource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdf5e790676d88e42b60d23a1c94ec3d
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/MultiplayerSession/Runtime/JoinSessionByCode/JoinSessionByCodeElement.cs
|
||||
uploadId: 814574
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Unity.Properties;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions
|
||||
{
|
||||
public class JoinSessionByCodeViewModel : IDisposable, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
|
||||
{
|
||||
const string k_ValidSessionCodeCharacters = "6789BCDFGHJKLMNPQRTWbcdfghjklmnpqrtw";
|
||||
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
[CreateProperty]
|
||||
public bool CanJoinSession
|
||||
{
|
||||
get => m_CanJoinSession;
|
||||
private set
|
||||
{
|
||||
var canJoin = value;
|
||||
if(canJoin && m_Session != null)
|
||||
canJoin = false;
|
||||
|
||||
if (m_CanJoinSession == canJoin)
|
||||
return;
|
||||
|
||||
m_CanJoinSession = canJoin;
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
bool m_CanJoinSession;
|
||||
|
||||
[CreateProperty]
|
||||
public string SessionCode
|
||||
{
|
||||
get => m_SessionCode;
|
||||
private set
|
||||
{
|
||||
if (m_SessionCode == value)
|
||||
return;
|
||||
|
||||
m_SessionCode = value;
|
||||
CanJoinSession = CheckIsSessionCodeFormatValid(m_SessionCode);
|
||||
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
string m_SessionCode;
|
||||
|
||||
public JoinSessionByCodeViewModel(string sessionType)
|
||||
{
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
|
||||
if (m_SessionObserver.Session != null)
|
||||
{
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
}
|
||||
|
||||
static bool CheckIsSessionCodeFormatValid(string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str) || str.Length is < 6 or > 8)
|
||||
return false;
|
||||
|
||||
foreach (var c in str)
|
||||
{
|
||||
if (!k_ValidSessionCodeCharacters.Contains(c))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnSessionAdded(ISession session)
|
||||
{
|
||||
m_Session = session;
|
||||
m_Session.RemovedFromSession += OnSessionRemoved;
|
||||
m_Session.Deleted += OnSessionRemoved;
|
||||
CanJoinSession = false;
|
||||
}
|
||||
|
||||
void OnSessionRemoved()
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnSessionRemoved;
|
||||
m_Session.Deleted -= OnSessionRemoved;
|
||||
m_Session = null;
|
||||
CanJoinSession = CheckIsSessionCodeFormatValid(SessionCode);
|
||||
}
|
||||
|
||||
public bool AreMultiplayerServicesInitialized()
|
||||
{
|
||||
return MultiplayerService.Instance != null;
|
||||
}
|
||||
|
||||
public async Task<ISession> JoinSessionByCodeAsync(JoinSessionOptions joinSessionOptions)
|
||||
{
|
||||
return await MultiplayerService.Instance.JoinSessionByCodeAsync(SessionCode, joinSessionOptions);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_SessionObserver != null)
|
||||
{
|
||||
m_SessionObserver.SessionAdded -= OnSessionAdded;
|
||||
m_SessionObserver.Dispose();
|
||||
m_SessionObserver = null;
|
||||
}
|
||||
if (m_Session != null)
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnSessionRemoved;
|
||||
m_Session.Deleted -= OnSessionRemoved;
|
||||
m_Session = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is used by UIToolkit to determine if any data bound to the UI has changed.
|
||||
/// Instead of hashing the data, an m_UpdateVersion counter is incremented when changes occur.
|
||||
/// </summary>
|
||||
public long GetViewHashCode() => m_UpdateVersion;
|
||||
|
||||
/// <summary>
|
||||
/// Suggested implementation of INotifyBindablePropertyChanged from UIToolkit.
|
||||
/// </summary>
|
||||
public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;
|
||||
void Notify([CallerMemberName] string property = null) =>
|
||||
propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(property));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5fdbae86b41f456438497221fc9e2b7a
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/MultiplayerSession/Runtime/JoinSessionByCode/JoinSessionByCodeViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user