Init
This commit is contained in:
@@ -0,0 +1,437 @@
|
||||
using System.Collections.Generic;
|
||||
using Blocks.Common;
|
||||
using Unity.Properties;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
[UxmlElement]
|
||||
public partial class SessionInfoElement : VisualElement
|
||||
{
|
||||
|
||||
const string k_SessionInfoHeaderName = "SessionInfoHeader";
|
||||
const string k_SessionInfoHeaderText = "SESSION INFO";
|
||||
|
||||
const string k_SessionDataHeaderName = "SessionData";
|
||||
const string k_SessionDataHeaderText = "Session Data";
|
||||
|
||||
const string k_ListIsEmptyLabel = "List is empty";
|
||||
const string k_KeyLabelPropertyName = "PropertyKeyLabel";
|
||||
const string k_VisibilityLabelPropertyName = "PropertyVisibilityLabel";
|
||||
const string k_ValueLabelPropertyName = "PropertyValueLabel";
|
||||
|
||||
const string k_SessionPropertiesHeaderName = "SessionProperties";
|
||||
const string k_SessionPropertiesHeaderText = "Session Properties";
|
||||
|
||||
const string k_PlayerDataHeaderName = "PlayerData";
|
||||
const string k_PlayerDataHeaderText = "Player Data";
|
||||
|
||||
const string k_PlayerHeaderName = "PlayerHeader";
|
||||
const string k_PlayerIdLabelName = "PlayerIdLabel";
|
||||
const string k_PlayerJoinedLabelName = "PlayerJoinedLabel";
|
||||
const string k_PlayerLastUpdatedLabelName = "PlayerLastUpdatedLabel";
|
||||
const string k_PlayerPropertiesListName = "PlayerPropertiesList";
|
||||
|
||||
const string k_PlayerPropertiesHeaderText = "Player Properties";
|
||||
|
||||
const string k_PlayerPropertiesIdKeyName = "PlayerPropertiesIDKey";
|
||||
const string k_PlayerPropertiesIdValueName = "PlayerPropertiesIDValue";
|
||||
const string k_PlayerPropertiesIdVisibilityName = "PlayerPropertiesIDVisibility";
|
||||
|
||||
[CreateProperty, UxmlAttribute]
|
||||
public string SessionType
|
||||
{
|
||||
get => m_SessionType;
|
||||
set
|
||||
{
|
||||
if (m_SessionType == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_SessionType = value;
|
||||
if (panel != null)
|
||||
{
|
||||
UpdateBindings();
|
||||
}
|
||||
}
|
||||
}
|
||||
string m_SessionType;
|
||||
|
||||
SessionInfoViewModel m_ViewModel;
|
||||
|
||||
readonly List<DataBinding> m_Bindings = new();
|
||||
|
||||
public SessionInfoElement()
|
||||
{
|
||||
AddToClassList(BlocksTheme.FullWidth);
|
||||
|
||||
var sessionInfoHeader = new Label
|
||||
{
|
||||
name = k_SessionInfoHeaderName,
|
||||
text = k_SessionInfoHeaderText
|
||||
};
|
||||
sessionInfoHeader.AddToClassList(BlocksTheme.Header);
|
||||
sessionInfoHeader.AddToClassList(BlocksTheme.HeaderSpaceBottom);
|
||||
Add(sessionInfoHeader);
|
||||
|
||||
var stateLabel = new Label();
|
||||
stateLabel.AddToClassList(BlocksTheme.Header);
|
||||
stateLabel.AddToClassList(BlocksTheme.HeaderXSmall);
|
||||
stateLabel.AddToClassList(BlocksTheme.SpaceBottom);
|
||||
var stateBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.SessionState)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
stateLabel.SetBinding(new BindingId(nameof(stateLabel.text)), stateBinding);
|
||||
Add(stateLabel);
|
||||
m_Bindings.Add(stateBinding);
|
||||
|
||||
var scrollViewParent = new ScrollView();
|
||||
scrollViewParent.AddToClassList(BlocksTheme.ScrollView);
|
||||
var sessionContainerBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.SessionInfoDisplayStyle)),
|
||||
bindingMode = BindingMode.ToTarget,
|
||||
};
|
||||
scrollViewParent.SetBinding(new BindingId("style.display"), sessionContainerBinding);
|
||||
Add(scrollViewParent);
|
||||
m_Bindings.Add(sessionContainerBinding);
|
||||
|
||||
var sessionDataHeader = new Label
|
||||
{
|
||||
name = k_SessionDataHeaderName,
|
||||
text = k_SessionDataHeaderText
|
||||
};
|
||||
sessionDataHeader.AddToClassList(BlocksTheme.Header);
|
||||
sessionDataHeader.AddToClassList(BlocksTheme.HeaderSmall);
|
||||
sessionDataHeader.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
scrollViewParent.Add(sessionDataHeader);
|
||||
|
||||
var sessionIdLabel = new Label();
|
||||
sessionIdLabel.AddToClassList(BlocksTheme.Label);
|
||||
var sessionIdBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.SessionId)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
sessionIdLabel.SetBinding(new BindingId(nameof(sessionIdLabel.text)), sessionIdBinding);
|
||||
scrollViewParent.Add(sessionIdLabel);
|
||||
m_Bindings.Add(sessionIdBinding);
|
||||
|
||||
var sessionNameLabel = new Label();
|
||||
sessionNameLabel.AddToClassList(BlocksTheme.Label);
|
||||
var sessionNameBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.Name)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
sessionNameLabel.SetBinding(new BindingId(nameof(sessionNameLabel.text)), sessionNameBinding);
|
||||
scrollViewParent.Add(sessionNameLabel);
|
||||
m_Bindings.Add(sessionNameBinding);
|
||||
|
||||
var maxPlayersLabel = new Label();
|
||||
maxPlayersLabel.AddToClassList(BlocksTheme.Label);
|
||||
var maxPlayersBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.MaxPlayers)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
maxPlayersLabel.SetBinding(new BindingId(nameof(maxPlayersLabel.text)), maxPlayersBinding);
|
||||
scrollViewParent.Add(maxPlayersLabel);
|
||||
m_Bindings.Add(maxPlayersBinding);
|
||||
|
||||
var isPrivateLabel = new Label();
|
||||
isPrivateLabel.AddToClassList(BlocksTheme.Label);
|
||||
var isPrivateBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.IsPrivate)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
isPrivateLabel.SetBinding(new BindingId(nameof(isPrivateLabel.text)), isPrivateBinding);
|
||||
scrollViewParent.Add(isPrivateLabel);
|
||||
m_Bindings.Add(isPrivateBinding);
|
||||
|
||||
var isLockedLabel = new Label();
|
||||
isLockedLabel.AddToClassList(BlocksTheme.Label);
|
||||
var isLockedBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.IsLocked)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
isLockedLabel.SetBinding(new BindingId(nameof(isLockedLabel.text)), isLockedBinding);
|
||||
scrollViewParent.Add(isLockedLabel);
|
||||
m_Bindings.Add(isLockedBinding);
|
||||
|
||||
var sessionCodeLabel = new Label();
|
||||
sessionCodeLabel.AddToClassList(BlocksTheme.Label);
|
||||
var sessionCodeBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.SessionCode)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
sessionCodeLabel.SetBinding(new BindingId(nameof(sessionCodeLabel.text)), sessionCodeBinding);
|
||||
scrollViewParent.Add(sessionCodeLabel);
|
||||
m_Bindings.Add(sessionCodeBinding);
|
||||
|
||||
var hostIDLabel = new Label();
|
||||
hostIDLabel.AddToClassList(BlocksTheme.Label);
|
||||
var hostIDBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.HostID)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
hostIDLabel.SetBinding(new BindingId(nameof(hostIDLabel.text)), hostIDBinding);
|
||||
scrollViewParent.Add(hostIDLabel);
|
||||
m_Bindings.Add(hostIDBinding);
|
||||
|
||||
var isHostLabel = new Label();
|
||||
isHostLabel.AddToClassList(BlocksTheme.Label);
|
||||
isHostLabel.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
var isHostBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.IsHost)),
|
||||
bindingMode = BindingMode.ToTarget
|
||||
};
|
||||
isHostLabel.SetBinding(new BindingId(nameof(isHostLabel.text)), isHostBinding);
|
||||
scrollViewParent.Add(isHostLabel);
|
||||
m_Bindings.Add(isHostBinding);
|
||||
|
||||
var propertiesListView = new ListView
|
||||
{
|
||||
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight,
|
||||
makeNoneElement = () =>
|
||||
{
|
||||
var label = new Label(k_ListIsEmptyLabel);
|
||||
label.AddToClassList(BlocksTheme.Label);
|
||||
return label;
|
||||
},
|
||||
makeHeader = () =>
|
||||
{
|
||||
var sessionPropertiesHeader = new Label
|
||||
{
|
||||
name = k_SessionPropertiesHeaderName,
|
||||
text = k_SessionPropertiesHeaderText
|
||||
};
|
||||
sessionPropertiesHeader.AddToClassList(BlocksTheme.Header);
|
||||
sessionPropertiesHeader.AddToClassList(BlocksTheme.HeaderXSmall);
|
||||
sessionPropertiesHeader.AddToClassList(BlocksTheme.HeaderXSmall);
|
||||
return sessionPropertiesHeader;
|
||||
},
|
||||
makeItem = () =>
|
||||
{
|
||||
var container = new VisualElement();
|
||||
container.AddToClassList(BlocksTheme.Label);
|
||||
|
||||
var keyLabel = new Label
|
||||
{
|
||||
name = k_KeyLabelPropertyName
|
||||
};
|
||||
keyLabel.AddToClassList(BlocksTheme.Label);
|
||||
container.Add(keyLabel);
|
||||
|
||||
var visibilityLabel = new Label
|
||||
{
|
||||
name = k_VisibilityLabelPropertyName
|
||||
};
|
||||
visibilityLabel.AddToClassList(BlocksTheme.Label);
|
||||
container.Add(visibilityLabel);
|
||||
|
||||
var valueLabel = new Label
|
||||
{
|
||||
name = k_ValueLabelPropertyName
|
||||
};
|
||||
valueLabel.AddToClassList(BlocksTheme.Label);
|
||||
container.Add(valueLabel);
|
||||
return container;
|
||||
},
|
||||
bindItem = (element, index) =>
|
||||
{
|
||||
var sessionProperty = m_ViewModel.SessionProperties[index];
|
||||
|
||||
element.Q<Label>(k_KeyLabelPropertyName).text = $"Key: {sessionProperty.Key}";
|
||||
element.Q<Label>(k_VisibilityLabelPropertyName).text = $"Visibility: {sessionProperty.Visibility}";
|
||||
element.Q<Label>(k_ValueLabelPropertyName).text = $"Value: {sessionProperty.Value}";
|
||||
}
|
||||
};
|
||||
propertiesListView.AddToClassList(BlocksTheme.Label);
|
||||
propertiesListView.AddToClassList(BlocksTheme.SpaceBottom);
|
||||
var propertiesBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.SessionProperties)),
|
||||
bindingMode = BindingMode.ToTarget,
|
||||
};
|
||||
propertiesListView.SetBinding(new BindingId(nameof(ListView.itemsSource)), propertiesBinding);
|
||||
scrollViewParent.Add(propertiesListView);
|
||||
m_Bindings.Add(propertiesBinding);
|
||||
|
||||
var playerStatsListView = new ListView
|
||||
{
|
||||
selectionType = SelectionType.None,
|
||||
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight,
|
||||
makeNoneElement = () =>
|
||||
{
|
||||
var label = new Label(k_ListIsEmptyLabel);
|
||||
label.AddToClassList(BlocksTheme.Label);
|
||||
return label;
|
||||
},
|
||||
makeHeader = () =>
|
||||
{
|
||||
var playerDataHeader = new Label
|
||||
{
|
||||
name = k_PlayerDataHeaderName,
|
||||
text = k_PlayerDataHeaderText
|
||||
};
|
||||
playerDataHeader.AddToClassList(BlocksTheme.Header);
|
||||
playerDataHeader.AddToClassList(BlocksTheme.HeaderSmall);
|
||||
playerDataHeader.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
return playerDataHeader;
|
||||
},
|
||||
makeItem = () =>
|
||||
{
|
||||
var container = new VisualElement();
|
||||
container.AddToClassList(BlocksTheme.SpaceBottom);
|
||||
|
||||
var playerHeaderLabel = new Label
|
||||
{
|
||||
name = k_PlayerHeaderName
|
||||
};
|
||||
playerHeaderLabel.AddToClassList(BlocksTheme.Header);
|
||||
playerHeaderLabel.AddToClassList(BlocksTheme.HeaderXSmall);
|
||||
playerHeaderLabel.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
container.Add(playerHeaderLabel);
|
||||
|
||||
var playerIdLabel = new Label
|
||||
{
|
||||
name = k_PlayerIdLabelName
|
||||
};
|
||||
playerIdLabel.AddToClassList(BlocksTheme.Label);
|
||||
container.Add(playerIdLabel);
|
||||
|
||||
var playerJoinedLabel = new Label
|
||||
{
|
||||
name = k_PlayerJoinedLabelName
|
||||
};
|
||||
playerJoinedLabel.AddToClassList(BlocksTheme.Label);
|
||||
container.Add(playerJoinedLabel);
|
||||
|
||||
var playerLastUpdatedLabel = new Label
|
||||
{
|
||||
name = k_PlayerLastUpdatedLabelName
|
||||
};
|
||||
playerLastUpdatedLabel.AddToClassList(BlocksTheme.Label);
|
||||
playerLastUpdatedLabel.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
container.Add(playerLastUpdatedLabel);
|
||||
|
||||
var playerPropertiesList = new ListView
|
||||
{
|
||||
name = k_PlayerPropertiesListName,
|
||||
headerTitle = k_PlayerPropertiesHeaderText,
|
||||
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight,
|
||||
makeNoneElement = () =>
|
||||
{
|
||||
var label = new Label(k_ListIsEmptyLabel);
|
||||
label.AddToClassList(BlocksTheme.Label);
|
||||
return label;
|
||||
},
|
||||
makeHeader = () =>
|
||||
{
|
||||
var playerPropertiesHeader = new Label
|
||||
{
|
||||
text = k_PlayerPropertiesHeaderText
|
||||
};
|
||||
playerPropertiesHeader.AddToClassList(BlocksTheme.Header);
|
||||
playerPropertiesHeader.AddToClassList(BlocksTheme.HeaderXSmall);
|
||||
playerPropertiesHeader.AddToClassList(BlocksTheme.SpaceBottomSmall);
|
||||
return playerPropertiesHeader;
|
||||
}
|
||||
};
|
||||
container.Add(playerPropertiesList);
|
||||
|
||||
return container;
|
||||
},
|
||||
bindItem = (element, index) =>
|
||||
{
|
||||
var player = m_ViewModel.PlayerStats[index];
|
||||
element.Q<Label>(k_PlayerHeaderName).text = $"Player {index + 1}";
|
||||
element.Q<Label>(k_PlayerIdLabelName).text = $"ID: {player.Id}";
|
||||
element.Q<Label>(k_PlayerJoinedLabelName).text = $"Joined: {player.Joined}";
|
||||
element.Q<Label>(k_PlayerLastUpdatedLabelName).text = $"Last Updated: {player.LastUpdated}";
|
||||
|
||||
var playerPropertiesList = element.Q<ListView>(k_PlayerPropertiesListName);
|
||||
if (playerPropertiesList == null)
|
||||
return;
|
||||
|
||||
playerPropertiesList.itemsSource = player.PlayerPropertiesList;
|
||||
playerPropertiesList.makeItem = () =>
|
||||
{
|
||||
var propertyContainer = new VisualElement();
|
||||
|
||||
var propertyKeyLabel = new Label
|
||||
{
|
||||
name = k_PlayerPropertiesIdKeyName
|
||||
};
|
||||
propertyKeyLabel.AddToClassList(BlocksTheme.Label);
|
||||
propertyContainer.Add(propertyKeyLabel);
|
||||
|
||||
var propertyValueLabel = new Label
|
||||
{
|
||||
name = k_PlayerPropertiesIdValueName
|
||||
};
|
||||
propertyValueLabel.AddToClassList(BlocksTheme.Label);
|
||||
propertyContainer.Add(propertyValueLabel);
|
||||
|
||||
var propertyVisibilityLabel = new Label
|
||||
{
|
||||
name = k_PlayerPropertiesIdVisibilityName
|
||||
};
|
||||
propertyVisibilityLabel.AddToClassList(BlocksTheme.Label);
|
||||
propertyContainer.Add(propertyVisibilityLabel);
|
||||
return propertyContainer;
|
||||
};
|
||||
playerPropertiesList.bindItem = (propertyElement, propertyIndex) =>
|
||||
{
|
||||
var sessionProperty = player.PlayerPropertiesList[propertyIndex];
|
||||
propertyElement.Q<Label>(k_PlayerPropertiesIdKeyName).text = $"Key: {sessionProperty.Key}";
|
||||
propertyElement.Q<Label>(k_PlayerPropertiesIdValueName).text = $"Value: {sessionProperty.Value}";
|
||||
propertyElement.Q<Label>(k_PlayerPropertiesIdVisibilityName).text = $"Visibility: {sessionProperty.Visibility}";
|
||||
};
|
||||
}
|
||||
};
|
||||
playerStatsListView.AddToClassList(BlocksTheme.ListViewNoHover);
|
||||
var playerStatsBinding = new DataBinding
|
||||
{
|
||||
dataSourcePath = new PropertyPath(nameof(SessionInfoViewModel.PlayerStats)),
|
||||
bindingMode = BindingMode.ToTarget,
|
||||
};
|
||||
playerStatsListView.SetBinding(new BindingId(nameof(ListView.itemsSource)), playerStatsBinding);
|
||||
scrollViewParent.Add(playerStatsListView);
|
||||
m_Bindings.Add(playerStatsBinding);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
|
||||
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
|
||||
}
|
||||
|
||||
void UpdateBindings()
|
||||
{
|
||||
CleanupBindings();
|
||||
|
||||
m_ViewModel = new SessionInfoViewModel(m_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b4ddfd6af5d6c24e8bfea2f6d96389e
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/SessionInfo/SessionInfoElement.cs
|
||||
uploadId: 814574
|
||||
@@ -0,0 +1,349 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Properties;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Services.Multiplayer;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Blocks.Sessions.Common
|
||||
{
|
||||
public class SessionProperty
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Visibility { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
|
||||
public class Players
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Joined { get; set; }
|
||||
public string LastUpdated { get; set; }
|
||||
public List<PlayerProperties> PlayerPropertiesList { get; set; }
|
||||
}
|
||||
|
||||
public class PlayerProperties
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Visibility { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
|
||||
public class SessionInfoViewModel : IDisposable, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
|
||||
{
|
||||
SessionObserver m_SessionObserver;
|
||||
ISession m_Session;
|
||||
long m_UpdateVersion;
|
||||
|
||||
[CreateProperty]
|
||||
public string SessionState
|
||||
{
|
||||
get => m_SessionState;
|
||||
set
|
||||
{
|
||||
if (m_SessionState == value)
|
||||
return;
|
||||
m_SessionState = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessionState = "No Session";
|
||||
|
||||
[CreateProperty]
|
||||
public DisplayStyle SessionInfoDisplayStyle
|
||||
{
|
||||
get => m_SessionInfoDisplayStyle;
|
||||
set
|
||||
{
|
||||
if (m_SessionInfoDisplayStyle == value)
|
||||
return;
|
||||
m_SessionInfoDisplayStyle = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
DisplayStyle m_SessionInfoDisplayStyle = DisplayStyle.None;
|
||||
|
||||
[CreateProperty]
|
||||
public string SessionId
|
||||
{
|
||||
get => m_SessionId;
|
||||
set
|
||||
{
|
||||
if (m_SessionId == value)
|
||||
return;
|
||||
m_SessionId = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessionId = "Unknown Session ID";
|
||||
|
||||
[CreateProperty]
|
||||
public string Name
|
||||
{
|
||||
get => m_SessionName;
|
||||
set
|
||||
{
|
||||
if (m_SessionName == value)
|
||||
return;
|
||||
m_SessionName = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessionName = "Unknown Session Name";
|
||||
|
||||
[CreateProperty]
|
||||
public string MaxPlayers
|
||||
{
|
||||
get => m_MaxPlayers;
|
||||
set
|
||||
{
|
||||
if (m_MaxPlayers == value)
|
||||
return;
|
||||
m_MaxPlayers = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_MaxPlayers = "Max Players: 0";
|
||||
|
||||
[CreateProperty]
|
||||
public string IsPrivate
|
||||
{
|
||||
get => m_IsPrivate;
|
||||
set
|
||||
{
|
||||
if (m_IsPrivate == value)
|
||||
return;
|
||||
m_IsPrivate = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_IsPrivate = "true";
|
||||
|
||||
[CreateProperty]
|
||||
public string IsLocked
|
||||
{
|
||||
get => m_IsLocked;
|
||||
set
|
||||
{
|
||||
if (m_IsLocked == value)
|
||||
return;
|
||||
m_IsLocked = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_IsLocked = "false";
|
||||
|
||||
[CreateProperty]
|
||||
public string SessionCode
|
||||
{
|
||||
get => m_SessionCode;
|
||||
set
|
||||
{
|
||||
if (m_SessionCode == value)
|
||||
return;
|
||||
m_SessionCode = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_SessionCode = "Unknown Session Code";
|
||||
|
||||
[CreateProperty]
|
||||
public string HostID
|
||||
{
|
||||
get => m_HostId;
|
||||
set
|
||||
{
|
||||
if (m_HostId == value)
|
||||
return;
|
||||
m_HostId = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_HostId = "Unknown Host ID";
|
||||
|
||||
[CreateProperty]
|
||||
public string IsHost
|
||||
{
|
||||
get => m_IsHost;
|
||||
set
|
||||
{
|
||||
if (m_IsHost == value)
|
||||
return;
|
||||
m_IsHost = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
string m_IsHost = "Unknown";
|
||||
|
||||
[CreateProperty]
|
||||
public List<SessionProperty> SessionProperties
|
||||
{
|
||||
get => m_SessionProperties;
|
||||
set
|
||||
{
|
||||
if (m_SessionProperties == value)
|
||||
return;
|
||||
m_SessionProperties = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
List<SessionProperty> m_SessionProperties;
|
||||
|
||||
[CreateProperty]
|
||||
public List<Players> PlayerStats
|
||||
{
|
||||
get => m_PlayerStats;
|
||||
set
|
||||
{
|
||||
if (m_PlayerStats == value)
|
||||
return;
|
||||
m_PlayerStats = value;
|
||||
Notify();
|
||||
}
|
||||
}
|
||||
|
||||
List<Players> m_PlayerStats;
|
||||
|
||||
public SessionInfoViewModel(string sessionType)
|
||||
{
|
||||
SessionProperties = new List<SessionProperty>();
|
||||
|
||||
m_SessionObserver = new SessionObserver(sessionType);
|
||||
m_SessionObserver.AddingSessionStarted += SessionInfoReceptionStarted;
|
||||
m_SessionObserver.SessionAdded += OnSessionAdded;
|
||||
m_SessionObserver.AddingSessionFailed += OnAddingSessionFailed;
|
||||
if (m_SessionObserver.Session != null)
|
||||
{
|
||||
OnSessionAdded(m_SessionObserver.Session);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionInfoReceptionStarted(AddingSessionOptions sessionOptions)
|
||||
{
|
||||
SessionInfoDisplayStyle = DisplayStyle.None;
|
||||
SessionState = "Awaiting Session information.";
|
||||
|
||||
++m_UpdateVersion;
|
||||
}
|
||||
|
||||
void OnSessionAdded(ISession newSession)
|
||||
{
|
||||
m_Session = newSession;
|
||||
m_Session.Changed += OnSessionChanged;
|
||||
m_Session.RemovedFromSession += OnSessionRemoved;
|
||||
m_Session.Deleted += OnSessionRemoved;
|
||||
|
||||
OnSessionChanged();
|
||||
}
|
||||
|
||||
void OnSessionChanged()
|
||||
{
|
||||
SessionInfoDisplayStyle = DisplayStyle.Flex;
|
||||
SessionState = $"Session State: {m_Session.State}";
|
||||
SessionId = $"Session ID: {m_Session.Id}";
|
||||
Name = $"Session Name: {m_Session.Name}";
|
||||
MaxPlayers = $"MaxPlayers: {m_Session.MaxPlayers}";
|
||||
IsPrivate = $"IsPrivate: {m_Session.IsPrivate}";
|
||||
IsLocked = $"IsLocked: {m_Session.IsLocked}";
|
||||
SessionCode = $"Session Code: {m_Session.Code}";
|
||||
HostID = $"Host ID: {m_Session.Host}";
|
||||
IsHost = $"Is Host: {m_Session.IsHost}";
|
||||
|
||||
var sessionPropertiesList = new List<SessionProperty>();
|
||||
foreach (var property in m_Session.Properties)
|
||||
{
|
||||
sessionPropertiesList.Add(new SessionProperty { Key = property.Key, Visibility = property.Value.Visibility.ToString(), Value = property.Value.Value });
|
||||
}
|
||||
|
||||
SessionProperties = sessionPropertiesList;
|
||||
|
||||
var playersList = new List<Players>();
|
||||
foreach (var player in m_Session.Players)
|
||||
{
|
||||
var playerPropertiesList = new List<PlayerProperties>();
|
||||
var playerProperties = player.Properties;
|
||||
foreach (var property in playerProperties)
|
||||
{
|
||||
playerPropertiesList.Add(new PlayerProperties { Key = property.Key, Visibility = property.Value.Visibility.ToString(), Value = property.Value.Value });
|
||||
}
|
||||
|
||||
playersList.Add(new Players { Id = player.Id, Joined = player.Joined.ToString(), LastUpdated = player.LastUpdated.ToString(), PlayerPropertiesList = playerPropertiesList });
|
||||
}
|
||||
|
||||
PlayerStats = playersList;
|
||||
|
||||
++m_UpdateVersion;
|
||||
}
|
||||
|
||||
void OnAddingSessionFailed(AddingSessionOptions addingSessionOptions, Exception exception)
|
||||
{
|
||||
SessionInfoDisplayStyle = DisplayStyle.None;
|
||||
SessionState = exception.Message;
|
||||
|
||||
++m_UpdateVersion;
|
||||
}
|
||||
|
||||
void OnSessionRemoved()
|
||||
{
|
||||
CleanupSession();
|
||||
|
||||
SessionInfoDisplayStyle = DisplayStyle.None;
|
||||
SessionState = "Session State: No Session";
|
||||
|
||||
++m_UpdateVersion;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_SessionObserver != null)
|
||||
{
|
||||
m_SessionObserver.AddingSessionStarted -= SessionInfoReceptionStarted;
|
||||
m_SessionObserver.SessionAdded -= OnSessionAdded;
|
||||
m_SessionObserver.AddingSessionFailed -= OnAddingSessionFailed;
|
||||
|
||||
m_SessionObserver.Dispose();
|
||||
m_SessionObserver = null;
|
||||
}
|
||||
|
||||
if (m_Session != null)
|
||||
{
|
||||
CleanupSession();
|
||||
}
|
||||
}
|
||||
|
||||
void CleanupSession()
|
||||
{
|
||||
m_Session.Changed -= OnSessionChanged;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25c64e96e7394114a2da0fc5ce47bedf
|
||||
timeCreated: 1761688334
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 341930
|
||||
packageName: Unity Building Block - Multiplayer Session
|
||||
packageVersion: 1.0
|
||||
assetPath: Assets/Blocks/CommonSession/Runtime/SessionInfo/SessionInfoViewModel.cs
|
||||
uploadId: 814574
|
||||
Reference in New Issue
Block a user