Init
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using Blocks.Common;
|
||||
using Unity.Properties;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
[UxmlElement]
|
||||
public partial class PlayerCountLabel : Label
|
||||
{
|
||||
[CreateProperty, UxmlAttribute]
|
||||
public string SessionType
|
||||
{
|
||||
get => m_SessionType;
|
||||
set
|
||||
{
|
||||
if (m_SessionType == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_SessionType = value;
|
||||
if (panel != null)
|
||||
{
|
||||
UpdateBindings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessionType;
|
||||
|
||||
DataBinding m_DataBinding;
|
||||
|
||||
public PlayerCountLabel()
|
||||
{
|
||||
AddToClassList(BlocksTheme.Label);
|
||||
m_DataBinding = new DataBinding()
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(PlayerCountViewModel.DisplayText)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
SetBinding(new BindingId(nameof(text)), m_DataBinding);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
|
||||
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
|
||||
}
|
||||
|
||||
void UpdateBindings()
|
||||
{
|
||||
CleanupBindings();
|
||||
m_DataBinding.dataSource = new PlayerCountViewModel(m_SessionType);
|
||||
}
|
||||
|
||||
void CleanupBindings()
|
||||
{
|
||||
if (m_DataBinding.dataSource is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
m_DataBinding.dataSource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ba500f520b97b346b2521ef36ef885d
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/PlayerCount/PlayerCountLabel.cs
|
||||
uploadId: 814574
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Properties;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
public class PlayerCountViewModel : INotifyBindablePropertyChanged, IDataSourceViewHashProvider, IDisposable
|
||||
{
|
||||
const string k_DefaultDisplayText = "- / - Players";
|
||||
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
/// <summary>
|
||||
/// This property is bound to <see cref="PlayerCountLabel.text"/> so that the label displays the number of
|
||||
/// players in the session.
|
||||
/// It is a property using [CreateProperty] attribute to allow for data binding in UIToolkit
|
||||
/// <summary>
|
||||
[CreateProperty]
|
||||
public string DisplayText
|
||||
{
|
||||
get => m_DisplayText;
|
||||
set
|
||||
{
|
||||
if (m_DisplayText == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_DisplayText = value;
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
string m_DisplayText = k_DefaultDisplayText;
|
||||
|
||||
public PlayerCountViewModel(string sessionType)
|
||||
{
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
|
||||
if (m_SessionObserver.Session != null)
|
||||
{
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
}
|
||||
|
||||
void OnSessionAdded(ISession newSession)
|
||||
{
|
||||
m_Session = newSession;
|
||||
m_Session.Changed += OnSessionChanged;
|
||||
m_Session.RemovedFromSession += OnSessionRemoved;
|
||||
m_Session.Deleted += OnSessionRemoved;
|
||||
OnSessionChanged();
|
||||
}
|
||||
|
||||
void OnSessionRemoved()
|
||||
{
|
||||
DisplayText = k_DefaultDisplayText;
|
||||
CleanUpSession();
|
||||
}
|
||||
|
||||
void CleanUpSession()
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnSessionRemoved;
|
||||
m_Session.Deleted -= OnSessionRemoved;
|
||||
m_Session.Changed -= OnSessionChanged;
|
||||
m_Session = null;
|
||||
}
|
||||
|
||||
void OnSessionChanged()
|
||||
{
|
||||
DisplayText = $"{m_Session.Players.Count} / {m_Session.MaxPlayers} Players";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_SessionObserver != null)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5092878c82fa2624cae291fe02975c67
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/PlayerCount/PlayerCountViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user