This commit is contained in:
Даниил Заикин
2026-06-17 20:44:53 +03:00
parent 9d153773c2
commit b133e7c656
1880 changed files with 244545 additions and 0 deletions
@@ -0,0 +1,73 @@
using System;
using Blocks.Common;
using Unity.Properties;
using UnityEngine.UIElements;
namespace Blocks.Sessions.Common
{
[UxmlElement]
public partial class LeaveSessionButton : Button
{
const string k_LeaveSessionButtonText = "Leave Session";
[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;
LeaveSessionViewModel m_ViewModel;
public LeaveSessionButton()
{
text = k_LeaveSessionButtonText;
AddToClassList(BlocksTheme.Button);
m_DataBinding = new DataBinding() { dataSourcePath = new PropertyPath(nameof(LeaveSessionViewModel.CanLeaveSession)), bindingMode = BindingMode.ToTarget };
SetBinding(new BindingId(nameof(enabledSelf)), m_DataBinding);
clicked += LeaveSession;
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
}
void LeaveSession()
{
_ = m_ViewModel.LeaveSession(m_SessionType);
}
void UpdateBindings()
{
CleanupBindings();
m_ViewModel = new LeaveSessionViewModel(m_SessionType);
m_DataBinding.dataSource = m_ViewModel;
}
void CleanupBindings()
{
if (m_DataBinding.dataSource is IDisposable disposable)
{
disposable.Dispose();
}
m_ViewModel = null;
m_DataBinding.dataSource = null;
}
}
}
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 26c91da98adaf109abcae03e989988aa
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/CommonSession/Runtime/LeaveSession/LeaveSessionButton.cs
uploadId: 814574
@@ -0,0 +1,102 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Unity.Properties;
using Unity.Services.Multiplayer;
using UnityEngine.UIElements;
namespace Blocks.Sessions.Common
{
class LeaveSessionViewModel : IDisposable, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
{
SessionObserver m_SessionObserver;
ISession m_Session;
[CreateProperty]
public bool CanLeaveSession
{
get => m_CanLeaveSession;
private set
{
if (m_CanLeaveSession == value)
{
return;
}
m_CanLeaveSession = value;
Notify();
}
}
bool m_CanLeaveSession;
public LeaveSessionViewModel(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.RemovedFromSession += OnSessionRemoved;
m_Session.Deleted += OnSessionRemoved;
CanLeaveSession = true;
}
void OnSessionRemoved()
{
CanLeaveSession = false;
CleanupSession();
}
public async Task LeaveSession(string sessionType)
{
var leaveTask = MultiplayerService.Instance?.Sessions[sessionType]?.LeaveAsync();
if (leaveTask != null)
await leaveTask;
}
void CleanupSession()
{
m_Session.RemovedFromSession -= OnSessionRemoved;
m_Session.Deleted -= OnSessionRemoved;
m_Session = null;
}
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_CanLeaveSession boolean is toggled when changes occur.
/// </summary>
public long GetViewHashCode() => m_CanLeaveSession ? 1 : 0;
/// <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: 9b15923ac8de40429b36be39da11e1b6
timeCreated: 1748894877
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/CommonSession/Runtime/LeaveSession/LeaveSessionViewModel.cs
uploadId: 814574