您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
80 行
2.4 KiB
80 行
2.4 KiB
#if !UNITY_EDITOR && (UNITY_PS4)
|
|
#define PLATFORM_EXCLUDES_QUIT_MENU
|
|
#endif
|
|
#if !UNITY_EDITOR
|
|
using UnityEngine;
|
|
#endif
|
|
using System.Collections.Generic;
|
|
using Unity.Entities;
|
|
using Unity.MegaCity.UI;
|
|
using Unity.NetCode;
|
|
using Unity.NetCode.Extensions;
|
|
|
|
namespace Unity.MegaCity.Gameplay
|
|
{
|
|
/// <summary>
|
|
/// System that handles quitting the game
|
|
/// </summary>
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
|
|
public partial struct QuitSystem : ISystem
|
|
{
|
|
public static bool WantsToQuit;
|
|
private static bool ShouldDisconnectPlayers;
|
|
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
if (ShouldDisconnectPlayers)
|
|
{
|
|
Leave();
|
|
ShouldDisconnectPlayers = false;
|
|
if (SystemAPI.TryGetSingletonEntity<NetworkStreamConnection>(out var singletonEntity))
|
|
{
|
|
MultiverseRoomAPI.Instance.ClientIsInGame = false;
|
|
UnityEngine.Debug.Log($"[{state.WorldUnmanaged.Name}] User has requested to disconnect from the server.");
|
|
var requestDisconnect = new NetworkStreamRequestDisconnect { Reason = NetworkStreamDisconnectReason.ConnectionClose };
|
|
state.EntityManager.AddComponentData(singletonEntity, requestDisconnect);
|
|
}
|
|
}
|
|
|
|
#if !PLATFORM_EXCLUDES_QUIT_MENU
|
|
if (WantsToQuit)
|
|
{
|
|
Leave();
|
|
WantsToQuit = false;
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#elif !UNITY_SERVER
|
|
Application.Quit();
|
|
#endif
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private async void Leave()
|
|
{
|
|
await MultiverseRoomAPI.Instance.LeaveRoom();
|
|
}
|
|
|
|
public static void DisconnectAllPlayers()
|
|
{
|
|
if (!ShouldDisconnectPlayers)
|
|
ShouldDisconnectPlayers = true;
|
|
}
|
|
|
|
public static bool IsPlayerConnected()
|
|
{
|
|
return GetAllClientConnected().Count > 0;
|
|
}
|
|
|
|
private static List<World> GetAllClientConnected()
|
|
{
|
|
var clientWorlds = new List<World>();
|
|
foreach (var world in World.All)
|
|
{
|
|
if (world.IsClient() || world.IsThinClient())
|
|
clientWorlds.Add(world);
|
|
}
|
|
return clientWorlds;
|
|
}
|
|
}
|
|
}
|