278 lines
8.8 KiB
C#
278 lines
8.8 KiB
C#
|
|
using System;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 跨平台时间设置测试器
|
|||
|
|
/// 用于测试和验证时间设置功能
|
|||
|
|
/// </summary>
|
|||
|
|
public class TimeSettingsTest : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("时间设置测试")]
|
|||
|
|
[Tooltip("测试用的目标时间")]
|
|||
|
|
public DateTime TestTargetTime = DateTime.Now.AddMinutes(1);
|
|||
|
|
|
|||
|
|
[Tooltip("是否在启动时显示当前时间信息")]
|
|||
|
|
public bool ShowTimeInfoOnStart = true;
|
|||
|
|
|
|||
|
|
[Tooltip("是否启用详细日志")]
|
|||
|
|
public bool VerboseLogging = true;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
if (ShowTimeInfoOnStart)
|
|||
|
|
{
|
|||
|
|
DisplayCurrentTimeInfo();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 显示当前时间信息
|
|||
|
|
/// </summary>
|
|||
|
|
[ContextMenu("显示时间信息")]
|
|||
|
|
public void DisplayCurrentTimeInfo()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
DateTime systemTime = WindowsSystemController.GetSystemTime();
|
|||
|
|
DateTime unityTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
Debug.Log("=== 时间信息 ===");
|
|||
|
|
Debug.Log($"系统时间: {systemTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
Debug.Log($"Unity时间: {unityTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
Debug.Log($"时间差异: {Math.Abs((systemTime - unityTime).TotalSeconds):F2} 秒");
|
|||
|
|
Debug.Log($"当前平台: {Application.platform}");
|
|||
|
|
|
|||
|
|
#if !UNITY_EDITOR
|
|||
|
|
// 检查权限信息
|
|||
|
|
bool hasPermission = TimeSettingsPermissionManager.HasTimeSettingsPermission();
|
|||
|
|
string limitations = TimeSettingsPermissionManager.GetTimeSettingsLimitations();
|
|||
|
|
|
|||
|
|
Debug.Log($"时间设置权限: {(hasPermission ? "有权限" : "无权限")}");
|
|||
|
|
Debug.Log($"权限限制: {limitations}");
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"显示时间信息失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 测试设置系统时间
|
|||
|
|
/// </summary>
|
|||
|
|
[ContextMenu("测试时间设置")]
|
|||
|
|
public void TestTimeSet()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
DateTime targetTime = TestTargetTime;
|
|||
|
|
|
|||
|
|
if (VerboseLogging)
|
|||
|
|
{
|
|||
|
|
Debug.Log("=== 时间设置测试开始 ===");
|
|||
|
|
Debug.Log($"目标时间: {targetTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 记录设置前的时间
|
|||
|
|
DateTime beforeTime = WindowsSystemController.GetSystemTime();
|
|||
|
|
|
|||
|
|
// 尝试设置时间
|
|||
|
|
bool success = WindowsSystemController.SetSystemTime(targetTime);
|
|||
|
|
|
|||
|
|
// 记录设置后的时间
|
|||
|
|
System.Threading.Thread.Sleep(1000); // 等待1秒
|
|||
|
|
DateTime afterTime = WindowsSystemController.GetSystemTime();
|
|||
|
|
|
|||
|
|
if (VerboseLogging)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"设置前时间: {beforeTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
Debug.Log($"设置后时间: {afterTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
Debug.Log($"设置结果: {(success ? "成功" : "失败")}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (success)
|
|||
|
|
{
|
|||
|
|
// 验证设置是否生效
|
|||
|
|
TimeSpan difference = afterTime - targetTime;
|
|||
|
|
if (Math.Abs(difference.TotalSeconds) <= 30) // 允许30秒误差
|
|||
|
|
{
|
|||
|
|
Debug.Log("✓ 时间设置测试成功!");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"⚠ 时间设置部分成功,但存在 {difference.TotalSeconds:F1} 秒误差");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning("✗ 时间设置测试失败");
|
|||
|
|
|
|||
|
|
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
|
|||
|
|
Debug.Log("Windows提示: 请以管理员身份运行应用");
|
|||
|
|
#elif UNITY_ANDROID && !UNITY_EDITOR
|
|||
|
|
Debug.Log("Android提示: 请在系统设置中手动修改时间");
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (VerboseLogging)
|
|||
|
|
{
|
|||
|
|
Debug.Log("=== 时间设置测试结束 ===");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"时间设置测试失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 测试时间权限
|
|||
|
|
/// </summary>
|
|||
|
|
[ContextMenu("测试时间权限")]
|
|||
|
|
public void TestTimePermissions()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Debug.Log("=== 时间权限测试 ===");
|
|||
|
|
|
|||
|
|
#if !UNITY_EDITOR
|
|||
|
|
bool hasPermission = TimeSettingsPermissionManager.HasTimeSettingsPermission();
|
|||
|
|
Debug.Log($"当前权限状态: {(hasPermission ? "✓ 有权限" : "✗ 无权限")}");
|
|||
|
|
|
|||
|
|
string limitations = TimeSettingsPermissionManager.GetTimeSettingsLimitations();
|
|||
|
|
Debug.Log($"权限限制说明: {limitations}");
|
|||
|
|
|
|||
|
|
string process = TimeSettingsPermissionManager.GetRecommendedTimeSetupProcess();
|
|||
|
|
Debug.Log($"推荐设置流程: {process}");
|
|||
|
|
|
|||
|
|
if (!hasPermission)
|
|||
|
|
{
|
|||
|
|
Debug.Log("尝试请求权限或引导用户...");
|
|||
|
|
bool requestResult = TimeSettingsPermissionManager.RequestTimeSettingsPermission();
|
|||
|
|
Debug.Log($"权限请求结果: {(requestResult ? "成功" : "需要用户操作")}");
|
|||
|
|
}
|
|||
|
|
#else
|
|||
|
|
Debug.Log("编辑器模式: 跳过权限检查");
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"时间权限测试失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置当前时间+指定分钟数
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="minutesToAdd">要添加的分钟数</param>
|
|||
|
|
[ContextMenu("设置时间+1分钟")]
|
|||
|
|
public void SetTimeForward1Minute()
|
|||
|
|
{
|
|||
|
|
SetTimeForward(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[ContextMenu("设置时间+5分钟")]
|
|||
|
|
public void SetTimeForward5Minutes()
|
|||
|
|
{
|
|||
|
|
SetTimeForward(5);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetTimeForward(int minutes)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
DateTime currentTime = WindowsSystemController.GetSystemTime();
|
|||
|
|
DateTime newTime = currentTime.AddMinutes(minutes);
|
|||
|
|
|
|||
|
|
Debug.Log($"将时间从 {currentTime:HH:mm:ss} 设置为 {newTime:HH:mm:ss} (+{minutes}分钟)");
|
|||
|
|
|
|||
|
|
bool success = WindowsSystemController.SetSystemTime(newTime);
|
|||
|
|
|
|||
|
|
if (success)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"✓ 成功将时间向前调整 {minutes} 分钟");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"✗ 时间调整失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"时间调整失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 重置到准确的网络时间(模拟)
|
|||
|
|
/// </summary>
|
|||
|
|
[ContextMenu("重置到网络时间")]
|
|||
|
|
public void ResetToNetworkTime()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 在实际应用中,这里应该从NTP服务器获取准确时间
|
|||
|
|
// 目前使用本地系统时间作为"网络时间"
|
|||
|
|
DateTime networkTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
Debug.Log($"重置时间到网络时间: {networkTime:yyyy-MM-dd HH:mm:ss}");
|
|||
|
|
|
|||
|
|
bool success = WindowsSystemController.SetSystemTime(networkTime);
|
|||
|
|
|
|||
|
|
if (success)
|
|||
|
|
{
|
|||
|
|
Debug.Log("✓ 成功重置到网络时间");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning("✗ 重置到网络时间失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"重置网络时间失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 运行完整的时间功能测试套件
|
|||
|
|
/// </summary>
|
|||
|
|
[ContextMenu("运行完整测试")]
|
|||
|
|
public void RunFullTest()
|
|||
|
|
{
|
|||
|
|
Debug.Log("🧪 开始完整时间功能测试...");
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 1. 显示当前信息
|
|||
|
|
DisplayCurrentTimeInfo();
|
|||
|
|
|
|||
|
|
System.Threading.Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
// 2. 测试权限
|
|||
|
|
TestTimePermissions();
|
|||
|
|
|
|||
|
|
System.Threading.Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
// 3. 测试时间设置
|
|||
|
|
TestTimeSet();
|
|||
|
|
|
|||
|
|
System.Threading.Thread.Sleep(2000);
|
|||
|
|
|
|||
|
|
// 4. 验证结果
|
|||
|
|
DisplayCurrentTimeInfo();
|
|||
|
|
|
|||
|
|
Debug.Log("🎉 完整时间功能测试完成!");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"完整测试失败: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
// 可以在这里添加实时时间监控逻辑
|
|||
|
|
// 例如:每秒检查一次时间变化
|
|||
|
|
}
|
|||
|
|
}
|