在Unity中实现简单的伪时间同步
程序员文章站
2023-12-13 10:54:22
在unity中实现简单的伪时间同步,只是读取数据库所在电脑的当前时间
复制代码 代码如下:
using unityengine;
using system.colle...
在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;
}
以上就是本文所述的全部内容了,希望大家能够喜欢。