Init
This commit is contained in:
+124
@@ -0,0 +1,124 @@
|
||||
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 CreateSessionElement : VisualElement
|
||||
{
|
||||
const string k_EnterSessionNamePlaceholder = "Enter Session Name";
|
||||
const string k_CreateButtonText = "CREATE";
|
||||
|
||||
[CreateProperty, UxmlAttribute]
|
||||
public SessionSettings SessionSettings
|
||||
{
|
||||
get => m_SessionSettings;
|
||||
set
|
||||
{
|
||||
if (m_SessionSettings == value)
|
||||
return;
|
||||
|
||||
m_SessionSettings = value;
|
||||
if (panel != null)
|
||||
UpdateBindings();
|
||||
}
|
||||
}
|
||||
SessionSettings m_SessionSettings;
|
||||
|
||||
CreateSessionViewModel m_ViewModel;
|
||||
|
||||
readonly List<DataBinding> m_Bindings = new();
|
||||
|
||||
public CreateSessionElement()
|
||||
{
|
||||
AddToClassList(BlocksTheme.ContainerHorizontal);
|
||||
|
||||
var enabledBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(m_ViewModel.CanRegisterSession)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
SetBinding(new BindingId(nameof(enabledSelf)), enabledBinding);
|
||||
m_Bindings.Add(enabledBinding);
|
||||
|
||||
var sessionNameTextField = new TextField
|
||||
{
|
||||
textEdition =
|
||||
{
|
||||
placeholder = k_EnterSessionNamePlaceholder,
|
||||
hidePlaceholderOnFocus = true
|
||||
}
|
||||
};
|
||||
sessionNameTextField.AddToClassList(BlocksTheme.TextField);
|
||||
sessionNameTextField.AddToClassList(BlocksTheme.SpaceRight);
|
||||
var sessionNameBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(m_ViewModel.SessionName)),
|
||||
bindingMode = BindingMode.ToSource
|
||||
};
|
||||
sessionNameTextField.SetBinding("value", sessionNameBinding);
|
||||
Add(sessionNameTextField);
|
||||
m_Bindings.Add(sessionNameBinding);
|
||||
|
||||
var createSessionButton = new Button
|
||||
{
|
||||
text = k_CreateButtonText
|
||||
};
|
||||
createSessionButton.AddToClassList(BlocksTheme.Button);
|
||||
var createSessionBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(m_ViewModel.HasSessionName)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
createSessionButton.SetBinding(new BindingId(nameof(enabledSelf)), createSessionBinding);
|
||||
createSessionButton.clicked += CreateSession;
|
||||
Add(createSessionButton);
|
||||
m_Bindings.Add(createSessionBinding);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
|
||||
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
|
||||
}
|
||||
|
||||
void CreateSession()
|
||||
{
|
||||
if (!SessionSettings)
|
||||
{
|
||||
Debug.LogError("SessionSettings is null, it needs to be assigned in the uxml.");
|
||||
return;
|
||||
}
|
||||
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.CreateSessionAsync(SessionSettings.ToSessionOptions());
|
||||
}
|
||||
|
||||
void UpdateBindings()
|
||||
{
|
||||
CleanupBindings();
|
||||
|
||||
m_ViewModel = new CreateSessionViewModel(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: 975cfeeaa0b804d48ae43d4abf031ebf
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/MultiplayerSession/Runtime/CreateSession/CreateSessionElement.cs
|
||||
uploadId: 814574
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
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 CreateSessionViewModel : IDisposable, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
|
||||
{
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
[CreateProperty]
|
||||
public bool CanRegisterSession
|
||||
{
|
||||
get => m_CanRegisterSession;
|
||||
private set
|
||||
{
|
||||
if (m_CanRegisterSession == value)
|
||||
return;
|
||||
|
||||
m_CanRegisterSession = value;
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
bool m_CanRegisterSession = true;
|
||||
|
||||
[CreateProperty]
|
||||
public bool HasSessionName
|
||||
{
|
||||
get => m_HasSessionName;
|
||||
private set
|
||||
{
|
||||
if (m_HasSessionName == value)
|
||||
return;
|
||||
|
||||
m_HasSessionName = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
bool m_HasSessionName;
|
||||
|
||||
[CreateProperty]
|
||||
public string SessionName
|
||||
{
|
||||
get => m_SessionName;
|
||||
private set
|
||||
{
|
||||
if (m_SessionName == value)
|
||||
return;
|
||||
|
||||
m_SessionName = value;
|
||||
|
||||
HasSessionName = m_SessionName != "";
|
||||
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
string m_SessionName;
|
||||
|
||||
public CreateSessionViewModel(string sessionType)
|
||||
{
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
|
||||
m_SessionObserver.AddingSessionStarted += OnAddingSessionStarted;
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
m_SessionObserver.AddingSessionFailed += OnAddingSessionFailed;
|
||||
|
||||
if (m_SessionObserver.Session != null)
|
||||
{
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
}
|
||||
void OnAddingSessionFailed(AddingSessionOptions session, SessionException exception) => CanRegisterSession = true;
|
||||
void OnAddingSessionStarted(AddingSessionOptions session) => CanRegisterSession = false;
|
||||
|
||||
void OnSessionAdded(ISession session)
|
||||
{
|
||||
m_Session = session;
|
||||
m_Session.RemovedFromSession += OnSessionRemoved;
|
||||
m_Session.Deleted += OnSessionRemoved;
|
||||
CanRegisterSession = false;
|
||||
}
|
||||
|
||||
void OnSessionRemoved()
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnSessionRemoved;
|
||||
m_Session.Deleted -= OnSessionRemoved;
|
||||
m_Session = null;
|
||||
CanRegisterSession = true;
|
||||
}
|
||||
|
||||
public bool AreMultiplayerServicesInitialized()
|
||||
{
|
||||
return MultiplayerService.Instance != null;
|
||||
}
|
||||
|
||||
public async Task<IHostSession> CreateSessionAsync(SessionOptions sessionOptions)
|
||||
{
|
||||
sessionOptions.Name = SessionName;
|
||||
return await MultiplayerService.Instance.CreateSessionAsync(sessionOptions);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_SessionObserver != null)
|
||||
{
|
||||
m_SessionObserver.AddingSessionStarted -= OnAddingSessionStarted;
|
||||
m_SessionObserver.SessionAdded -= OnSessionAdded;
|
||||
m_SessionObserver.AddingSessionFailed -= OnAddingSessionFailed;
|
||||
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: 99041548465e15845b967682ac2b66a1
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/MultiplayerSession/Runtime/CreateSession/CreateSessionViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user