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,92 @@
using System;
using Blocks.Common;
using Blocks.Sessions.Common;
using Unity.Properties;
using UnityEngine;
using UnityEngine.UIElements;
namespace Blocks.Sessions
{
[UxmlElement]
public partial class QuickJoinButton : Button
{
const string k_QuickJoinButtonText = "QUICK JOIN";
[UxmlAttribute, CreateProperty]
public SessionSettings SessionSettings
{
get => m_SessionSettings;
set
{
if (m_SessionSettings == value)
{
return;
}
m_SessionSettings = value;
if (panel != null)
{
UpdateBindings();
}
}
}
SessionSettings m_SessionSettings;
[UxmlAttribute]
public QuickJoinSettings QuickJoinSettings;
DataBinding m_DataBinding;
QuickJoinViewModel m_ViewModel;
public QuickJoinButton()
{
text = k_QuickJoinButtonText;
AddToClassList(BlocksTheme.Button);
m_DataBinding = new DataBinding()
{
dataSourcePath = new PropertyPath(nameof(QuickJoinViewModel.CanClickButton)),
bindingMode = BindingMode.ToTarget
};
SetBinding("enabledSelf", m_DataBinding);
clicked += OnQuickJoinButtonClicked;
RegisterCallback<AttachToPanelEvent>(_ => UpdateBindings());
RegisterCallback<DetachFromPanelEvent>(_ => CleanupBindings());
}
void OnQuickJoinButtonClicked()
{
if (!SessionSettings)
{
Debug.LogError("SessionSettings is null, it needs to be assigned in the uxml.");
return;
}
if (!m_ViewModel.AreMultiplayerServicesInitialized())
{
Debug.LogError("Multiplayer Services are not initialized. You can initialize them with default settings by adding a ServicesInitialization and PlayerAuthentication components in your scene.");
return;
}
_ = m_ViewModel.MatchmakeSessionAsync(QuickJoinSettings.ToQuickJoinOptions(), SessionSettings.ToSessionOptions());
}
void UpdateBindings()
{
CleanupBindings();
m_ViewModel = new QuickJoinViewModel(SessionSettings?.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: f2fe78a7d34bd1b4d84380b9aeacc6a7
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/MultiplayerSession/Runtime/QuickJoin/QuickJoinButton.cs
uploadId: 814574
@@ -0,0 +1,24 @@
using Unity.Services.Multiplayer;
using UnityEngine;
namespace Blocks.Sessions
{
[CreateAssetMenu(fileName = nameof(QuickJoinSettings), menuName = "Services/Blocks/Session/" + nameof(QuickJoinSettings))]
public class QuickJoinSettings : ScriptableObject
{
[Header("QuickJoinSettings")]
[Tooltip("The timeout in seconds for the quick join to stop trying to join and either fail or create its own session is createSession is true.")]
public float timeout = 5f;
[Tooltip("If true, the quick join will create a session if it cannot find one to join withing the given timeout time frame.")]
public bool createSession = true;
public QuickJoinOptions ToQuickJoinOptions()
{
return new QuickJoinOptions
{
Timeout = System.TimeSpan.FromSeconds(timeout),
CreateSession = createSession
};
}
}
}
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d453e3c39a7322043bc13370b81b57f1
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/MultiplayerSession/Runtime/QuickJoin/QuickJoinSettings.cs
uploadId: 814574
@@ -0,0 +1,125 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Unity.Properties;
using Unity.Services.Multiplayer;
using UnityEngine.UIElements;
namespace Blocks.Sessions
{
public class QuickJoinViewModel : INotifyBindablePropertyChanged, IDataSourceViewHashProvider, IDisposable
{
SessionObserver m_SessionObserver;
ISession m_Session;
long m_UpdateVersion;
/// <summary>
/// This property is bound to <see cref="QuickJoinButton.enabledSelf"/> so that the button is enabled or disabled
/// based on whether a session can be joined. <br/>
/// It is a property using [CreateProperty] attribute to allow for data binding in UIToolkit
/// and using <see cref="Notify"/> to optimize UIToolkit redraws.
/// </summary>
[CreateProperty]
public bool CanClickButton
{
get => m_CanClickButton;
set
{
if (m_CanClickButton == value)
{
return;
}
m_CanClickButton = value;
++m_UpdateVersion;
Notify();
}
}
bool m_CanClickButton = true;
public QuickJoinViewModel(string sessionType)
{
m_SessionObserver = new SessionObserver(sessionType);
m_SessionObserver.AddingSessionStarted += OnAddingSessionStarted;
m_SessionObserver.SessionAdded += OnSessionAdded;
m_SessionObserver.AddingSessionFailed += OnAddingSessionFailed;
if (m_SessionObserver.Session != null)
{
OnSessionAdded(m_SessionObserver.Session);
}
}
void OnAddingSessionFailed(AddingSessionOptions session, Exception exception)
{
CanClickButton = true;
}
void OnAddingSessionStarted(AddingSessionOptions sessionOptions)
{
CanClickButton = false;
}
void OnSessionAdded(ISession newSession)
{
m_Session = newSession;
m_Session.RemovedFromSession += OnSessionRemoved;
m_Session.Deleted += OnSessionRemoved;
CanClickButton = false;
}
void OnSessionRemoved()
{
CanClickButton = true;
CleanupSession();
}
void CleanupSession()
{
m_Session.RemovedFromSession -= OnSessionRemoved;
m_Session.Deleted -= OnSessionRemoved;
m_Session = null;
}
public bool AreMultiplayerServicesInitialized()
{
return MultiplayerService.Instance != null;
}
public async Task<ISession> MatchmakeSessionAsync(QuickJoinOptions quickJoinOptions, SessionOptions sessionOptions)
{
return await MultiplayerService.Instance.MatchmakeSessionAsync(quickJoinOptions, sessionOptions);
}
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: 0486ed66e217eda44805ac20119a31f1
AssetOrigin:
serializedVersion: 1
productId: 341930
packageName: Unity Building Block - Multiplayer Session
packageVersion: 1.0
assetPath: Assets/Blocks/MultiplayerSession/Runtime/QuickJoin/QuickJoinViewModel.cs
uploadId: 814574