欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群
public class Controller : MonoBehaviour {
int timeLimit = 1;//限制时间
DateTime runTime;//启动时间
DateTime currentTime = DateTime.Now;//当前时间
string path = "";
void Start()
{
path = Application.persistentDataPath+ "/RunTime";
if (!File.Exists(path))
{
//获取当前启动时间
Debug.Log("第一次启动时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//存入启动时间
File.WriteAllBytes(path, Encoding.UTF8.GetBytes(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
}
//读取第一次启动时间
runTime = Convert.ToDateTime(File.ReadAllLines(path)[0]);
Debug.Log("第一次启动时间:" + runTime.ToString("yyyy - MM - dd HH: mm:ss"));
TimeSpan ts1 = new TimeSpan(runTime.Ticks);
TimeSpan ts2 = new TimeSpan(currentTime.Ticks);
TimeSpan tsSub = ts1.Subtract(ts2).Duration();
Debug.Log("天/" + tsSub.Days + "-时/" + tsSub.Hours + "-分/" + tsSub.Minutes);
if (tsSub.Days >= timeLimit)
{
//天数
Debug.Log("退出");
}
if (tsSub.Hours >= timeLimit)
{
//小时数
Debug.Log("退出");
}
if (tsSub.Minutes >= timeLimit)
{
//分钟数
Debug.Log("退出");
}
}
}
欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群