很多游戏具有每日登陆奖励功能,为防止玩家修改本地时间,需要读取服务器时间。下面分两部分介绍获取网络时间的两种方法,第一部分解析xml,第二部分截取string。
第一部分:通过国家授时中心链接获得的数据如下:

获取网络时间、解析xml、截取string
| 01 |
<?xml version="1.0" encoding="GB2312" ?> |
| 02 |
- <ntsc> |
| 03 |
- <time> |
| 04 |
<year>2011</year> |
| 05 |
<month>7</month> |
| 06 |
<day>10</day> |
| 07 |
<Weekday ></Weekday> |
| 08 |
<hour>19</hour> |
| 09 |
<minite>45</minite> |
| 10 |
<second>37</second> |
| 11 |
<Millisecond ></Millisecond> |
| 12 |
</time> |
| 13 |
</ntsc>` |
然后解析xml获得当前时间,完整代码如下:
| 01 |
using UnityEngine; |
| 02 |
using System.Collections; |
| 03 |
using System.Xml; |
| 04 |
|
| 05 |
/// <summary> |
| 06 |
/// Get web time and parse xml |
| 07 |
/// 获取网络时间/解析xml |
| 08 |
/// </summary> |
| 09 |
public class Test1 : MonoBehaviour |
| 10 |
{ |
| 11 |
public string timeURL = "http://www.time.ac.cn/timeflash.asp?user=flash";//授时中心地址 |
| 12 |
// Use this for initialization |
| 13 |
void Start() |
| 14 |
{ |
| 15 |
StartCoroutine(GetTime()); |
| 16 |
} |
| 17 |
|
| 18 |
IEnumerator GetTime() |
| 19 |
{ |
| 20 |
Debug.Log("Start get web time"); |
| 21 |
WWW www = new WWW(timeURL); |
| 22 |
while (!www.isDone) |
| 23 |
{ |
| 24 |
Debug.Log("Getting web time"); |
| 25 |
yield return www; |
| 26 |
Debug.Log("Finish getting web time and whole xml is : " + www.text); |
| 27 |
ParseXml(www); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public void ParseXml(WWW www) |
| 32 |
{ |
| 33 |
XmlDocument xmlDoc = new XmlDocument(); |
| 34 |
xmlDoc.LoadXml(www.text); |
| 35 |
XmlElement root = xmlDoc.DocumentElement; |
| 36 |
XmlNodeList nodeList = root.SelectNodes("/ntsc/time"); |
| 37 |
foreach (XmlElement xe in nodeList) |
| 38 |
{ //Unity3D教程手册:www.unitymanual.com |
| 39 |
foreach (XmlElement x1 in xe.ChildNodes) |
| 40 |
{ |
| 41 |
if (x1.Name == "year") |
| 42 |
Debug.Log("Current year: " + x1.InnerText); |
| 43 |
if (x1.Name == "month") |
| 44 |
Debug.Log("Current month: " + x1.InnerText); |
| 45 |
if (x1.Name == "day") |
| 46 |
Debug.Log("Current day: " + x1.InnerText); |
| 47 |
if (x1.Name == "hour") |
| 48 |
Debug.Log("Current hour: " + x1.InnerText); |
| 49 |
if (x1.Name == "minite") |
| 50 |
Debug.Log("Current minite: " + x1.InnerText); |
| 51 |
if (x1.Name == "second") |
| 52 |
Debug.Log("Current second: " + x1.InnerText); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
获得的网络时间如下:

获取网络时间、解析xml、截取string
第二部分:通过北京时间链接地址获得的数据如下:
| 1 |
t0=new Date().getTime(); nyear=2013; nmonth=6; nday=3; nwday=1; nhrs=16; nmin=57; nsec=29; |
然后截取string获得网络时间,具体方法是截取“;”获得年月日...然后截取“=”获得对应的时间.
完整代码如下:
| 01 |
using UnityEngine; |
| 02 |
using System.Collections; |
| 03 |
|
| 04 |
/// <summary> |
| 05 |
/// Get web time and split string |
| 06 |
/// 获取网络时间/截取string |
| 07 |
/// </summary> |
| 08 |
public class Test2 : MonoBehaviour |
| 09 |
{ |
| 10 |
public string timeURL = "http://www.beijing-time.org/time.asp";//北京时间地址 |
| 11 |
// Use this for initialization |
| 12 |
void Start() |
| 13 |
{ |
| 14 |
StartCoroutine(GetTime()); |
| 15 |
} |
| 16 |
|
| 17 |
IEnumerator GetTime() |
| 18 |
{ |
| 19 |
Debug.Log("Start get web time"); |
| 20 |
WWW www = new WWW(timeURL); |
| 21 |
while (!www.isDone) |
| 22 |
{ //Unity3D教程手册:www.unitymanual.com |
| 23 |
Debug.Log("Getting web time"); |
| 24 |
yield return www; |
| 25 |
Debug.Log("Finish getting web time and whole xml is : " + www.text); |
| 26 |
SplitString(www); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public void SplitString(WWW www) |
| 31 |
{ |
| 32 |
string[] timeData = www.text.Split(';'); |
| 33 |
for (int i = 0; i < timeData.Length; i++) |
| 34 |
{ |
| 35 |
string[] exactTime = timeData[i].Split('='); |
| 36 |
foreach (string temp in exactTime) |
| 37 |
Debug.Log(temp); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
获得的网络时间如下:

获取网络时间、解析xml、截取string


















