public class TimeLimited : MonoBehaviour
{
public GameObject SYQ,LWTS;
public void Start()
{
//网络时间
#region 获取时间戳
string str = GetWebClient("http://www.hko.gov.hk/cgi-bin/gts/time5a.pr?a=2");
//string str = GetTimeStamp(); // 验证时间戳该有的长度
//Console.WriteLine(str);
string timeStamp = str.Split('=')[1].Substring(0, 10); //网页获取的数据长度超了,所以要裁剪
//Console.WriteLine(timeStamp);
DateTime datetime = GetTime(timeStamp);
Debug.Log((int)datetime.Year + "年" + (int)datetime.Month + "月" + (int)datetime.Day + "日");//输出时间
if ((int)datetime.Year > 2020&& (int)datetime.Month > 11&& (int)datetime.Day > 15) //判断是否大于2021年
{
Debug.Log(datetime + "123456789");
transform.GetComponent<CameraController>().enabled = false;
SYQ.SetActive(true);
}
#endregion
}
void Update()
{
//本地时间
if (System.DateTime.Today.Year > 2020 && System.DateTime.Today.Month > 11 && System.DateTime.Today.Day > 15)//限制使用日期2021.10.2日
{
SYQ.SetActive(true);
transform.GetComponent<CameraController>().enabled = false;
Debug.Log(1234);
}
IsConnectedInternet();
}
[DllImport("winInet.dll")] //引用外部库
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved); //库中函数
#region 判断本地的连接状态
private bool IsConnectedInternet()
{
int dwFlag = new int();
if (!InternetGetConnectedState(ref dwFlag, 0))
{
Debug.Log("当前没有联网,请您先联网后再进行操作!");
LWTS.gameObject.SetActive(true);
if ((dwFlag & 0x14) == 0) return false;
Debug.LogWarning("本地系统处于脱机模式。");
return false;
}
else
{
if ((dwFlag & 0x01) != 0)
{
Debug.Log("调制解调器上网。");
LWTS.gameObject.SetActive(false);
return true;
}
else if ((dwFlag & 0x02) != 0)
{
Debug.Log("网卡上网。");
LWTS.gameObject.SetActive(false);
return true;
}
else if ((dwFlag & 0x04) != 0)
{
Debug.Log("代理服务器上网。");
LWTS.gameObject.SetActive(false);
return true;
}
else if ((dwFlag & 0x40) != 0)
{
Debug.Log("虽然可以联网,但可能链接也可能不连接。");
LWTS.gameObject.SetActive(false);
return true;
}
}
return false;
}
#endregion
private static string GetWebClient(string url)
{
string strHTML = "";
WebClient myWebClient = new WebClient();
Stream myStream = myWebClient.OpenRead(url);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
strHTML = sr.ReadToEnd();
myStream.Close();
return strHTML;
}
#region 获取时间戳,本方法只是为了测试时间戳样式
private static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
#endregion
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name="timeStamp">Unix时间戳格式</param>
/// <returns>C#格式时间</returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
}