177 lines
3.9 KiB
C#
177 lines
3.9 KiB
C#
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using Stary.Evo;
|
|
using UnityEngine;
|
|
|
|
public interface IGlobalConfigSystem : ISystem
|
|
{
|
|
public string IP { get; }
|
|
public string GetConnectionId();
|
|
public void SetConnectionId(string connectionId);
|
|
|
|
public string GetConnectionName();
|
|
public void SetConnectionName(string connectionName);
|
|
|
|
public UniTaskVoid StartConnectionTime();
|
|
public string GetConnectionTime();
|
|
|
|
public void StopConnectionTime();
|
|
|
|
public int GetConnectionTimeType();
|
|
public void SetConnectionTimeType(int connectionTimeType);
|
|
|
|
public Texture2D GetConnectionTexture();
|
|
public void SetConnectionTexture(Texture2D connectionTexture);
|
|
|
|
public string GetUserId();
|
|
public void SetUserId(string userId);
|
|
}
|
|
|
|
public class GlobalConfigSystem : AbstractSystem, IGlobalConfigSystem
|
|
{
|
|
/// <summary>
|
|
/// 连接ID
|
|
/// </summary>
|
|
private string _connectionId;
|
|
|
|
/// <summary>
|
|
/// 连接名称
|
|
/// </summary>
|
|
private string _connectionName;
|
|
|
|
/// <summary>
|
|
/// 连接图标
|
|
/// </summary>
|
|
private Texture2D _connectionTexture;
|
|
|
|
/// <summary>
|
|
/// 连接时间
|
|
/// </summary>
|
|
private float _connectionTime;
|
|
|
|
/// <summary>
|
|
/// 连接时间类型
|
|
/// </summary>
|
|
private int _connectionTimeType;
|
|
|
|
private CancellationTokenSource _cts;
|
|
|
|
|
|
/// <summary>
|
|
/// 连接ID
|
|
/// </summary>
|
|
private string _userId;
|
|
|
|
|
|
public override void Dispose()
|
|
{
|
|
}
|
|
|
|
|
|
public string IP => "http://127.0.0.1:8080";
|
|
|
|
public string GetConnectionId()
|
|
{
|
|
if (string.IsNullOrEmpty(_connectionId))
|
|
{
|
|
Debug.LogWarning("GlobalConfigSystem: GetConnectionId not set");
|
|
return "";
|
|
}
|
|
|
|
return _connectionId;
|
|
}
|
|
|
|
public void SetConnectionId(string connectionId)
|
|
{
|
|
_connectionId = connectionId;
|
|
}
|
|
|
|
public string GetConnectionName()
|
|
{
|
|
if (string.IsNullOrEmpty(_connectionName))
|
|
{
|
|
Debug.LogWarning("GlobalConfigSystem: GetConnectionName not set");
|
|
return "";
|
|
}
|
|
|
|
return _connectionName;
|
|
}
|
|
|
|
public void SetConnectionName(string connectionName)
|
|
{
|
|
_connectionName = connectionName;
|
|
}
|
|
|
|
public async UniTaskVoid StartConnectionTime()
|
|
{
|
|
_cts = new CancellationTokenSource();
|
|
_connectionTime = 0;
|
|
while (!_cts.IsCancellationRequested)
|
|
{
|
|
_connectionTime += Time.deltaTime;
|
|
await UniTask.Yield(_cts.Token); // 等一帧,等价于 Update
|
|
}
|
|
}
|
|
|
|
public string GetConnectionTime()
|
|
{
|
|
var totalSeconds = (int)_connectionTime;
|
|
var hours = totalSeconds / 3600;
|
|
var minutes = totalSeconds % 3600 / 60;
|
|
var seconds = totalSeconds % 60;
|
|
return $"{hours:D2}:{minutes:D2}:{seconds:D2}";
|
|
}
|
|
|
|
public void StopConnectionTime()
|
|
{
|
|
_cts?.Cancel();
|
|
_cts?.Dispose();
|
|
_cts = null;
|
|
}
|
|
|
|
public int GetConnectionTimeType()
|
|
{
|
|
return _connectionTimeType;
|
|
}
|
|
|
|
public void SetConnectionTimeType(int connectionTimeType)
|
|
{
|
|
_connectionTimeType = connectionTimeType;
|
|
}
|
|
|
|
public Texture2D GetConnectionTexture()
|
|
{
|
|
if (_connectionTexture == null)
|
|
{
|
|
Debug.LogWarning("GlobalConfigSystem: GetConnectionTexture not set");
|
|
return null;
|
|
}
|
|
|
|
return _connectionTexture;
|
|
}
|
|
|
|
public void SetConnectionTexture(Texture2D connectionTexture)
|
|
{
|
|
_connectionTexture = connectionTexture;
|
|
}
|
|
|
|
public string GetUserId()
|
|
{
|
|
if (string.IsNullOrEmpty(_userId))
|
|
{
|
|
Debug.LogWarning("GlobalConfigSystem: GetUserId not set");
|
|
return "";
|
|
}
|
|
|
|
return _userId;
|
|
}
|
|
|
|
public void SetUserId(string userId)
|
|
{
|
|
_userId = userId;
|
|
}
|
|
|
|
protected override void OnInit()
|
|
{
|
|
}
|
|
} |