Init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using Blocks.Common;
|
||||
using Unity.Properties;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
[UxmlElement]
|
||||
public partial class SessionStatusLabel : Label
|
||||
{
|
||||
[UxmlAttribute, CreateProperty]
|
||||
public string SessionType
|
||||
{
|
||||
get => m_SessiontType;
|
||||
set
|
||||
{
|
||||
if (m_SessiontType == value)
|
||||
return;
|
||||
|
||||
m_SessiontType = value;
|
||||
if (panel != null)
|
||||
UpdateBindings();
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessiontType;
|
||||
|
||||
DataBinding m_Binding;
|
||||
|
||||
public SessionStatusLabel()
|
||||
{
|
||||
AddToClassList(BlocksTheme.Label);
|
||||
|
||||
m_Binding = new DataBinding()
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionStatusViewModel.DisplayText)),
|
||||
bindingMode = BindingMode.ToTarget,
|
||||
};
|
||||
|
||||
SetBinding(new BindingId(nameof(text)), m_Binding);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(evt => UpdateBindings());
|
||||
RegisterCallback<DetachFromPanelEvent>(evt => RemoveBindings());
|
||||
}
|
||||
|
||||
void RemoveBindings()
|
||||
{
|
||||
if (m_Binding.dataSource is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
m_Binding.dataSource = null;
|
||||
}
|
||||
|
||||
void UpdateBindings()
|
||||
{
|
||||
RemoveBindings();
|
||||
m_Binding.dataSource = new SessionStatusViewModel(SessionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4da76dc5904c6224f90646080a5911cd
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/SessionStatus/SessionStatusLabel.cs
|
||||
uploadId: 814574
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Properties;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
public class SessionStatusViewModel : INotifyBindablePropertyChanged, IDataSourceViewHashProvider, IDisposable
|
||||
{
|
||||
const string k_NoSessionText = "Not in a session";
|
||||
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
[CreateProperty]
|
||||
public string DisplayText
|
||||
{
|
||||
get => m_DisplayText;
|
||||
set
|
||||
{
|
||||
if (m_DisplayText == value)
|
||||
return;
|
||||
|
||||
m_DisplayText = value;
|
||||
m_UpdateVersion++;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_DisplayText = k_NoSessionText;
|
||||
|
||||
public SessionStatusViewModel(string sessionType)
|
||||
{
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
m_SessionObserver.AddingSessionStarted += OnAddingSessionStarted;
|
||||
m_SessionObserver.AddingSessionFailed += OnAddingSessionFailed;
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
if (m_SessionObserver.Session != null)
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
|
||||
void OnAddingSessionFailed(AddingSessionOptions sessionOptions, SessionException exception)
|
||||
{
|
||||
DisplayText = "Session operation failed: " + exception.Message;
|
||||
}
|
||||
|
||||
void OnAddingSessionStarted(AddingSessionOptions sessionOptions)
|
||||
{
|
||||
DisplayText = "Awaiting Session information...";
|
||||
}
|
||||
|
||||
void OnSessionAdded(ISession session)
|
||||
{
|
||||
m_Session = session;
|
||||
m_Session.RemovedFromSession += OnRemovedFromSession;
|
||||
m_Session.Deleted += OnRemovedFromSession;
|
||||
DisplayText = "In session: " + m_Session.Name;
|
||||
}
|
||||
|
||||
void OnRemovedFromSession()
|
||||
{
|
||||
CleanupSession();
|
||||
DisplayText = k_NoSessionText;
|
||||
}
|
||||
|
||||
void CleanupSession()
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnRemovedFromSession;
|
||||
m_Session.Deleted -= OnRemovedFromSession;
|
||||
m_Session = null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_SessionObserver?.Dispose();
|
||||
m_SessionObserver = null;
|
||||
|
||||
if (m_Session != null)
|
||||
CleanupSession();
|
||||
}
|
||||
|
||||
/// <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: 12b466ea4a0f660488391a3c0f5cdfe1
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/SessionStatus/SessionStatusViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user