Init
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using Blocks.Common;
|
||||
using Unity.Properties;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
[UxmlElement]
|
||||
public partial class PlayerListViewElement : ListView
|
||||
{
|
||||
private const string k_PlayerListIsEmptyLabel = "No player has joined";
|
||||
|
||||
private string m_SessionType;
|
||||
private DataBinding m_DataBinding;
|
||||
private PlayerListViewModel m_ViewModel;
|
||||
|
||||
[CreateProperty, UxmlAttribute]
|
||||
public string SessionType
|
||||
{
|
||||
get => m_SessionType;
|
||||
set
|
||||
{
|
||||
if (m_SessionType == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_SessionType = value;
|
||||
if (panel != null)
|
||||
{
|
||||
UpdateBindingSources();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerListViewElement()
|
||||
{
|
||||
selectionType = SelectionType.None;
|
||||
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight;
|
||||
// required for making each element inheriting an indexed array item for the data source path
|
||||
// if not set, you need to specify the datasource path manually in the bindItem callback for each item
|
||||
bindingSourceSelectionMode = BindingSourceSelectionMode.AutoAssign;
|
||||
|
||||
AddToClassList(BlocksTheme.ListViewNoHover);
|
||||
AddToClassList(BlocksTheme.ScrollView);
|
||||
|
||||
makeNoneElement = MakeNoneElement;
|
||||
|
||||
makeItem = MakeDefaultItem;
|
||||
|
||||
BindItemSource();
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(OnAttachToPanelEvent);
|
||||
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanelEvent);
|
||||
}
|
||||
|
||||
|
||||
private void OnDetachFromPanelEvent(DetachFromPanelEvent panelEvent)
|
||||
{
|
||||
CleanupBindings();
|
||||
}
|
||||
|
||||
private void OnAttachToPanelEvent(AttachToPanelEvent panelEvent)
|
||||
{
|
||||
UpdateBindingSources();
|
||||
}
|
||||
|
||||
private void UpdateBindingSources()
|
||||
{
|
||||
CleanupBindings();
|
||||
|
||||
m_ViewModel = new PlayerListViewModel(m_SessionType);
|
||||
m_DataBinding.dataSource = m_ViewModel;
|
||||
}
|
||||
|
||||
private void CleanupBindings()
|
||||
{
|
||||
m_ViewModel?.Dispose();
|
||||
m_ViewModel = null;
|
||||
m_DataBinding.dataSource = null;
|
||||
}
|
||||
|
||||
private static VisualElement MakeNoneElement()
|
||||
{
|
||||
var label = new Label(k_PlayerListIsEmptyLabel);
|
||||
label.AddToClassList(BlocksTheme.Label);
|
||||
return label;
|
||||
}
|
||||
|
||||
private void BindItemSource()
|
||||
{
|
||||
m_DataBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(PlayerListViewModel.Players)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
SetBinding(new BindingId(nameof(itemsSource)), m_DataBinding);
|
||||
}
|
||||
|
||||
private static VisualElement MakeDefaultItem()
|
||||
{
|
||||
var playerNameLabel = new Label { name = nameof(PlayerNameLabel) };
|
||||
playerNameLabel.AddToClassList(BlocksTheme.Label);
|
||||
|
||||
var dataBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = PropertyPath.FromName(nameof(PlayerViewModel.Name)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
|
||||
playerNameLabel.SetBinding(new BindingId(nameof(Label.text)), dataBinding);
|
||||
|
||||
return playerNameLabel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ebf8a475674a6344a8602c7b78150111
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/PlayerList/PlayerListViewElement.cs
|
||||
uploadId: 814574
|
||||
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Properties;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
public class PlayerListViewModel : INotifyBindablePropertyChanged, IDataSourceViewHashProvider, IDisposable
|
||||
{
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
List<PlayerViewModel> m_Players;
|
||||
|
||||
[CreateProperty]
|
||||
public List<PlayerViewModel> Players
|
||||
{
|
||||
get => m_Players;
|
||||
set
|
||||
{
|
||||
if (m_Players == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_Players = value;
|
||||
++m_UpdateVersion;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerListViewModel(string sessionType)
|
||||
{
|
||||
Players = new List<PlayerViewModel>();
|
||||
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
|
||||
if (m_SessionObserver.Session != null)
|
||||
{
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePlayerList()
|
||||
{
|
||||
if (m_Session == null)
|
||||
return;
|
||||
|
||||
var updatedPlayerList = new List<PlayerViewModel>();
|
||||
foreach (var player in m_Session.Players)
|
||||
{
|
||||
updatedPlayerList.Add(new PlayerViewModel(player, m_Session));
|
||||
}
|
||||
|
||||
// properly dispose the player view models first
|
||||
DisposeAndClearPlayerList();
|
||||
Players.AddRange(updatedPlayerList);
|
||||
|
||||
++m_UpdateVersion;
|
||||
|
||||
Notify(nameof(Players));
|
||||
}
|
||||
|
||||
private void DisposeAndClearPlayerList()
|
||||
{
|
||||
foreach (var player in Players)
|
||||
{
|
||||
player.Dispose();
|
||||
}
|
||||
|
||||
Players.Clear();
|
||||
}
|
||||
|
||||
void OnSessionAdded(ISession newSession)
|
||||
{
|
||||
m_Session = newSession;
|
||||
m_Session.RemovedFromSession += OnSessionRemoved;
|
||||
m_Session.Deleted += OnSessionRemoved;
|
||||
|
||||
m_Session.PlayerJoined += OnPlayerCountChanged;
|
||||
m_Session.PlayerHasLeft += OnPlayerCountChanged;
|
||||
m_Session.PlayerPropertiesChanged += OnPlayerPropertiesChanged;
|
||||
|
||||
UpdatePlayerList();
|
||||
}
|
||||
|
||||
void OnPlayerCountChanged(string playerId)
|
||||
{
|
||||
UpdatePlayerList();
|
||||
}
|
||||
|
||||
void OnPlayerPropertiesChanged()
|
||||
{
|
||||
UpdatePlayerList();
|
||||
}
|
||||
|
||||
void OnSessionRemoved()
|
||||
{
|
||||
DisposeAndClearPlayerList();
|
||||
CleanupSession();
|
||||
}
|
||||
|
||||
void CleanupSession()
|
||||
{
|
||||
m_Session.RemovedFromSession -= OnSessionRemoved;
|
||||
m_Session.Deleted -= OnSessionRemoved;
|
||||
m_Session.PlayerHasLeft -= OnPlayerCountChanged;
|
||||
m_Session.PlayerJoined -= OnPlayerCountChanged;
|
||||
m_Session.PlayerPropertiesChanged -= OnPlayerPropertiesChanged;
|
||||
m_Session = null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_SessionObserver != null)
|
||||
{
|
||||
m_SessionObserver.SessionAdded -= OnSessionAdded;
|
||||
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: 261a2bc9eb4f2eb48a133715c1244987
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/PlayerList/PlayerListViewModel.cs
|
||||
uploadId: 814574
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Properties;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a DataBinding compatible representation of a player in a session.
|
||||
/// </summary>
|
||||
public class PlayerViewModel :
|
||||
/* IReadOnlyPlayer, */
|
||||
INotifyBindablePropertyChanged, IDataSourceViewHashProvider, IDisposable
|
||||
{
|
||||
private IReadOnlyPlayer _player;
|
||||
private ISession _session;
|
||||
|
||||
private long _updateVersion;
|
||||
|
||||
/// <summary>
|
||||
/// The unique identifier for the player. If not provided for a create or join request, it will be set to the ID of the caller.
|
||||
/// </summary>
|
||||
[CreateProperty]
|
||||
public string Id => _player?.Id;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the player retrieved via the Authentication service.
|
||||
/// </summary>
|
||||
[CreateProperty]
|
||||
public string Name => _player?.GetPlayerName();
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the given player is the host of the current Session.
|
||||
/// </summary>
|
||||
[CreateProperty]
|
||||
public bool IsHost => _session?.Host == Id;
|
||||
|
||||
/// <summary>
|
||||
/// The allocation id
|
||||
/// </summary>
|
||||
public string AllocationId => _player?.AllocationId;
|
||||
|
||||
/// <summary>
|
||||
/// Custom game-specific properties that apply to an individual player (e.g. `role` or `skill`).
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, PlayerProperty> Properties => _player?.Properties;
|
||||
|
||||
/// <summary>
|
||||
/// The time at which the member joined the Session.
|
||||
/// </summary>
|
||||
public DateTime Joined => _player?.Joined ?? DateTime.UnixEpoch;
|
||||
|
||||
/// <summary>
|
||||
/// The last time the metadata for this member was updated.
|
||||
/// </summary>
|
||||
public DateTime LastUpdated => _player?.LastUpdated ?? DateTime.UnixEpoch;
|
||||
|
||||
/// <summary>
|
||||
/// The Session the player is part of.
|
||||
/// </summary>
|
||||
public ISession Session => _session;
|
||||
|
||||
public PlayerViewModel(IReadOnlyPlayer player, ISession session)
|
||||
{
|
||||
_player = player;
|
||||
_session = session;
|
||||
|
||||
_session.Changed += OnSessionChanged;
|
||||
_session.SessionHostChanged += OnSessionHostChanged;
|
||||
_session.PlayerPropertiesChanged += OnSessionPlayerPropertiesChanged;
|
||||
}
|
||||
|
||||
private void OnSessionHostChanged(string obj)
|
||||
{
|
||||
_updateVersion++;
|
||||
Notify(nameof(IsHost));
|
||||
}
|
||||
|
||||
private void OnSessionPlayerPropertiesChanged()
|
||||
{
|
||||
_updateVersion++;
|
||||
if (!string.IsNullOrEmpty(Name))
|
||||
{
|
||||
Notify(nameof(Name));
|
||||
}
|
||||
|
||||
Notify(nameof(Properties));
|
||||
}
|
||||
|
||||
private void OnSessionChanged()
|
||||
{
|
||||
_updateVersion++;
|
||||
Notify(nameof(Session));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_session.Changed -= OnSessionChanged;
|
||||
_session.SessionHostChanged -= OnSessionHostChanged;
|
||||
_session.PlayerPropertiesChanged -= OnSessionPlayerPropertiesChanged;
|
||||
|
||||
_player = null;
|
||||
_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() => _updateVersion;
|
||||
|
||||
/// <summary>
|
||||
/// Suggested implementation of INotifyBindablePropertyChanged from UIToolkit.
|
||||
/// </summary>
|
||||
public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;
|
||||
|
||||
private void Notify([CallerMemberName] string property = null)
|
||||
{
|
||||
propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(property));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75d4b34acea8451a8a3d375147ba9213
|
||||
timeCreated: 1761247019
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/PlayerList/PlayerViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user