99 lines
2.5 KiB
C#
99 lines
2.5 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.IO;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class TimeData : MonoBehaviour
|
||
{ // 设定目标时间戳(秒级)
|
||
private long targetTimestamp = 1743494340; // 请替换为你的目标时间戳
|
||
|
||
// 文件生成的路径
|
||
private string filePath; // 请根据需求调整文件路径
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
filePath = Application.persistentDataPath + "/jsonData.txt";
|
||
// 你可以选择将目标时间戳设置为某个特定时刻
|
||
// 比如设定一个未来的时间戳:2024年1月1日,UTC时间
|
||
// targetTimestamp = new DateTime(2024, 1, 1).ToUniversalTime().ToUnixTimeSeconds();
|
||
|
||
// 如果目标时间戳已到或超过,立刻创建文件
|
||
if (IsTargetTimeReached())
|
||
{
|
||
CreateTextFile();
|
||
}
|
||
else
|
||
{
|
||
// 启动协程,检查时间戳
|
||
StartCoroutine(CheckTimeAndCreateFile());
|
||
}
|
||
}
|
||
|
||
// 用于检查目标时间戳是否已经到达
|
||
bool IsTargetTimeReached()
|
||
{
|
||
// 获取当前的Unix时间戳
|
||
long currentTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
|
||
return currentTimestamp >= targetTimestamp;
|
||
}
|
||
|
||
// 协程:每秒检查一次时间
|
||
private IEnumerator CheckTimeAndCreateFile()
|
||
{
|
||
while (!IsTargetTimeReached())
|
||
{
|
||
// 每隔一秒检查一次时间戳
|
||
yield return new WaitForSeconds(1);
|
||
}
|
||
// 时间达到后,创建文件
|
||
CreateTextFile();
|
||
}
|
||
|
||
// 创建文本文件并写入内容
|
||
private void CreateTextFile()
|
||
{
|
||
try
|
||
{
|
||
// 如果路径不存在,创建目录
|
||
string directory = Path.GetDirectoryName(filePath);
|
||
if (!Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
// 创建文件并写入内容
|
||
using (StreamWriter writer = new StreamWriter(filePath))
|
||
{
|
||
writer.WriteLine("json");
|
||
writer.WriteLine($"Verson_1.0.1");
|
||
writer.WriteLine($"data");
|
||
}
|
||
|
||
Debug.Log($"文件已生成:{filePath}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"创建文件时出错: {ex.Message}");
|
||
}
|
||
}
|
||
private void Update()
|
||
{
|
||
if (FileExists(filePath))
|
||
{
|
||
Quit();
|
||
}
|
||
}
|
||
|
||
public void Quit()
|
||
{
|
||
Application.Quit();
|
||
}
|
||
// 检查文件是否存在
|
||
bool FileExists(string path)
|
||
{
|
||
return File.Exists(path);
|
||
}
|
||
}
|