1. 程式人生 > >Unity獲取網路時間

Unity獲取網路時間

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IntTime : MonoBehaviour
{
    private int year, mouth, day, hour, min, sec;
    private string timeURL = "http://cgi.im.qq.com/cgi-bin/cgi_svrtime";
    private void Update()
    {
        StartCoroutine(GetTime());
    }
    IEnumerator GetTime()
    {
        WWW www = new WWW(timeURL);
        while (!www.isDone)
        {
            yield return www;
        }
        SplitTime(www.text);
    }

    void SplitTime(string dateTime)
    {
        dateTime = dateTime.Replace("-", "|");
        dateTime = dateTime.Replace(" ", "|");
        dateTime = dateTime.Replace(":", "|");
        string[] Times = dateTime.Split('|');
        year = int.Parse(Times[0]);
        mouth = int.Parse(Times[1]);
        day = int.Parse(Times[2]);
        hour = int.Parse(Times[3]);
        min = int.Parse(Times[4]);
        sec = int.Parse(Times[5]);
        print(hour + ":" + min + " :" + sec);
    }
}