您好,登錄后才能下訂單哦!
這篇文章主要講解了Unity3D獲取時(shí)間戳或北京時(shí)間的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
單機(jī)游戲因?yàn)闆]有服務(wù)器下發(fā)時(shí)間戳所以要自己獲取,當(dāng)然也可以用現(xiàn)成的時(shí)間API來獲取。
如果獲取本地時(shí)間,會(huì)導(dǎo)致玩家隨意修改日期來達(dá)到數(shù)據(jù)更改,如每日獎(jiǎng)品、每日獎(jiǎng)勵(lì)等等。
單機(jī)游戲本來就不要網(wǎng)絡(luò)的,可是獲取時(shí)間需要網(wǎng)絡(luò),這有點(diǎn)矛盾,有沒有誰有更好的解決方案呢?
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/");//百度北京時(shí)間地址 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<string,string> f1 = (p) =>{ Regex reg = new Regex("(?<=baidu_time\\().*?(?=\\))"); return reg.Matches(p)[0].Value;}; string time = f1(html).Substring(0, 10);//這里是時(shí)間戳 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";//如果沒有網(wǎng)絡(luò)就返回默認(rèn) } return result; } } }
看完上述內(nèi)容,是不是對(duì)Unity3D獲取時(shí)間戳或北京時(shí)間的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。