Unity3D获取时间戳或北京时间的方法-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Unity3D获取时间戳或北京时间的方法

这篇文章主要讲解了Unity3D获取时间戳或北京时间的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

梅江网站建设公司创新互联,梅江网站设计制作,有大型网站制作公司丰富经验。已为梅江成百上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的梅江做网站的公司定做!

单机游戏因为没有服务器下发时间戳所以要自己获取,当然也可以用现成的时间API来获取。

如果获取本地时间,会导致玩家随意修改日期来达到数据更改,如每日奖品、每日奖励等等。

单机游戏本来就不要网络的,可是获取时间需要网络,这有点矛盾,有没有谁有更好的解决方案呢?

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
 
namespace ConsoleApplication1
{
 
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine( GetBeiJingTime());
      Console.ReadKey();
    }
 
    public static string GetBeiJingTime()
    {
      bool isget = false;
      string result = string.Empty;
      try
      {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://open.baidu.com/special/time/");//百度北京时间地址
        req.Headers.Add("content", "text/html; charset=gbk");
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        Stream stream = res.GetResponseStream();
        StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gbk"));
        string html = sr.ReadToEnd();
        Func f1 = (p) =>{
          Regex reg = new Regex("(?<=baidu_time\\().*?(?=\\))");
          return reg.Matches(p)[0].Value;};
        string time = f1(html).Substring(0, 10);//这里是时间戳
        stream.Dispose();
        sr.Dispose();
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(time + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        result = dtStart.Add(toNow).ToString("yyyyMMdd");
        isget = true;
      }
      catch (Exception)
      {
      }
      finally
      {
        if (!isget)result = "19700101";//如果没有网络就返回默认
      }
      return result;
    }
  }
 
}

看完上述内容,是不是对Unity3D获取时间戳或北京时间的方法有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


网站栏目:Unity3D获取时间戳或北京时间的方法
链接分享:http://scgulin.cn/article/pciies.html