在Unity中完成簡略的偽時光同步。本站提示廣大學習愛好者:(在Unity中完成簡略的偽時光同步)文章只能為提供參考,不一定能成為您想要的結果。以下是在Unity中完成簡略的偽時光同步正文
在Unity中完成簡略的偽時光同步,只是讀取數據庫地點電腦確當前時光
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;
public class ChangeTime
{
//Kernel32.dll在32位體系和64位體系有差別,64位體系中須要設置為以治理員身份運轉
[DllImport("Kernel32.dll",SetLastError=true,EntryPoint="SetLocalTime")]
static extern int SetLocalTime(ref SystemDateTime lpSystemDateTime);
public static string GetCurrentTimeFromDB()
{
string result = "";
//從數據庫中獲得體系以後時光
//設置銜接字符串
SqlConnection con = new SqlConnection ("Data Source=192.168.0.1;Initial Catalog=DB;User ID=sa;password=123456");
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
//設置銜接語句
cmd.CommandText = "select getdate()";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//開啟
sda.SelectCommand.Connection.Open();
result = sda.SelectCommand.ExecuteScalar().ToString();
//封閉
sda.SelectCommand.Connection.Close();
return result;
}
public static void SetLocalDae(string dateTime)
{
System.DateTime date = System.DateTime.Parse(dateTime);
SystemDateTime sysNew = new SystemDateTime();
//設置屬性
sysNew.tYear = short.Parse(date.Year.ToString());
sysNew.tMonth = short.Parse(date.Month.ToString());
sysNew.tDay = short.Parse(date.Day.ToString());
sysNew.tHour = short.Parse(date.Hour.ToString());
sysNew.tMinute = short.Parse(date.Minute.ToString());
sysNew.tSecond = short.Parse(date.Second.ToString());
//挪用API,更新體系時光
SetLocalTime(ref sysNew);
}
}
/// <summary>
/// 界說變量用於吸收
/// </summary>
public class SystemDateTime
{
public short tYear;
public short tMonth;
public short tDayOfWeek;
public short tDay;
public short tHour;
public short tMinute;
public short tSecond;
public short tMilliseconds;
}
以上就是本文所述的全體內容了,願望年夜家可以或許愛好。