SoFunction
Updated on 2025-03-07

Implement simple pseudo-time synchronization in Unity


using UnityEngine;
using ;
using ;
using ;
using ;
public class ChangeTime
{
//There is a difference between 32-bit systems and 64-bit systems. In 64-bit systems, they need to be set to run as administrator.
    [DllImport("",SetLastError=true,EntryPoint="SetLocalTime")]
    static extern int SetLocalTime(ref SystemDateTime lpSystemDateTime);
    public static string GetCurrentTimeFromDB()
    {
        string result = "";
//Get the current system time from the database
//Set the connection string
        SqlConnection con = new SqlConnection ("Data Source=192.168.0.1;Initial Catalog=DB;User ID=sa;password=123456");
        SqlCommand cmd = new SqlCommand ();
        = con;
        = ;
//Set connection statement
        = "select getdate()";
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
//Open
        ();
        result = ().ToString();
//closure
        ();
        return result;
    }
    public static void SetLocalDae(string dateTime)
    {
        date = (dateTime);
        SystemDateTime sysNew = new SystemDateTime();
//Set properties
        = (());
        = (());
        = (());
        = (());
        = (());
        = (());
//Calling the API to update the system time
        SetLocalTime(ref sysNew);
    }
}
/// <summary>
/// Define variables for receiving
/// </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;
}