// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** //#define LIMITTIME namespace G { using System; using UnityEngine; /// /// 游戏启动类 /// public class Launch : MonoBehaviour { #region Static private static int _Timestamp; /// /// /// public static int Timestamp { get { return _Timestamp; } } private static int _LocalTimestamp; /// /// /// public static int LocalTimestamp { get { return _LocalTimestamp; } } /// /// /// public static int TodaySeconds { get { var now = ServerNow; return (int)((now - now.Date).TotalSeconds); } } /// /// /// public static int TodayRemainSeconds { get { return 86400 - TodaySeconds; } } /// /// /// public static DateTime ServerUtcNow { get { return F.Utils.Time.ToDataTime(_Timestamp); } } /// /// /// public static DateTime ServerNow { get { return ServerUtcNow.ToLocalTime(); } } /// /// /// /// public static void SetServerTimestamp(int timestamp) { _ServerTimestamp = timestamp; SetCalibrationSeconds(); UpdateTimestamp(); } static int _ServerTimestamp; static DateTime _CalibrationDateTime; static DateTime _LastCalibrationDateTime; /// /// /// /// public static bool IsCalibration() { return GetCalibrationSeconds() > 0; } /// /// /// /// static int GetCalibrationSeconds() { var totalSeconds = (DateTime.UtcNow - _LastCalibrationDateTime).TotalSeconds; if (totalSeconds > 0 && totalSeconds < 600) { _LastCalibrationDateTime = DateTime.UtcNow; return (int)(_LastCalibrationDateTime - _CalibrationDateTime).TotalSeconds; } return 0; } /// /// /// static void SetCalibrationSeconds() { _CalibrationDateTime = DateTime.UtcNow; _LastCalibrationDateTime = _CalibrationDateTime; } /// /// /// public static void UpdateTimestamp() { #if !UNITY_EDITOR && !DEBUG_MY if (_ServerTimestamp > 0) _Timestamp = _ServerTimestamp + GetCalibrationSeconds(); else #endif _Timestamp = F.Utils.Time.ToUnixTime(DateTime.UtcNow); _LocalTimestamp = F.Utils.Time.ToUnixTime(DateTime.UtcNow); } private static int _InstallTimestamp; /// /// 安装 /// public static int InstallTimestamp { get { return _InstallTimestamp; } set { var key = "it_";// + KConfig.appVersion; _InstallTimestamp = PlayerPrefs.GetInt(key, 0); if (_InstallTimestamp == 0) { _InstallTimestamp = value; PlayerPrefs.SetInt(key, _InstallTimestamp); PlayerPrefs.Save(); } } } /// /// /// public static float RunningTime { get; private set; } public static string CurrLevelName { get; private set; } public static string LastLevelName { get; private set; } public static void LoadLevel(string levelName) { LastLevelName = CurrLevelName; CurrLevelName = levelName; #if UNITY_5_3_OR_NEWER UnityEngine.SceneManagement.SceneManager.LoadScene(levelName, UnityEngine.SceneManagement.LoadSceneMode.Single); #else Application.LoadLevel(levelName); #endif } public static AsyncOperation LoadLevelAsync(string levelName) { LastLevelName = CurrLevelName; CurrLevelName = levelName; Camera.main.clearFlags = CameraClearFlags.SolidColor; Camera.main.backgroundColor = Color.black; #if UNITY_5_3_OR_NEWER var asynOp = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(levelName, UnityEngine.SceneManagement.LoadSceneMode.Single); #else var asynOp = Application.LoadLevelAsync(levelName); #endif return asynOp; } private static float _TimeScale; public static void PauseGame(bool pause) { AudioListener.pause = pause; if (pause) { _TimeScale = Time.timeScale; Time.timeScale = 0f; } else { Time.timeScale = 1f; _TimeScale = 0f; } } public static bool IsPause() { return _TimeScale == 0f; } #endregion #region Field #endregion #region Method private void InitGame() { } #endregion #region Unity private void Awake() { #if DEBUG_MY || LIMITTIME if (System.DateTime.Now > new System.DateTime(2022, 7, 1)) { Application.Quit(); } #endif DontDestroyOnLoad(gameObject); #if UNITY_STANDALONE_WIN Screen.SetResolution(1280, 720, false); #else //SetDesignContentScale(); #endif UnityEngine.Random.InitState((int)System.DateTime.UtcNow.Ticks); UpdateTimestamp(); //InstallTimestamp = Timestamp; Application.targetFrameRate = 60; //Time.fixedDeltaTime = 0.02f; Screen.sleepTimeout = SleepTimeout.NeverSleep; #if !UNITY_EDITOR //Application.logMessageReceived += KLog.LogCallback; #endif //add Game Base InitGame(); } private void LateUpdate() { UpdateTimestamp(); RunningTime += Time.unscaledDeltaTime; } private void OnApplicationQuit() { //KLog.Save(); //KPlatform.SessionChange(false); //KPlatform.ProfileSign(false); } private void OnApplicationFocus(bool focusStatus) { if (!focusStatus) { //PlayerPrefs.Save(); //KLog.Save(); } } private void OnApplicationPause(bool pauseStatus) { //KPlatform.SessionChange(!pauseStatus); //KPlatform.ProfileSign(!pauseStatus); if (pauseStatus) { //KLog.Save(); //PlayerPrefs.Save(); } else { //SetDesignContentScale(); } } #if UNITY_ANDROID private int _scaleWidth = 0; private int _scaleHeight = 0; public void SetDesignContentScale() { if (_scaleWidth + _scaleHeight == 0) { int designWidth = 1334; int designHeight = 750; int screenWidth = Screen.width; int screenHeight = Screen.height; float s1 = designWidth / (float)designHeight; float s2 = screenWidth / (float)screenHeight; if (s1 < s2) { designWidth = Mathf.RoundToInt(designHeight * s2); } else if (s1 > s2) { designHeight = Mathf.RoundToInt(designWidth / s2); } float contentScale = designWidth / (float)screenWidth; if (contentScale < 1.0f) { _scaleWidth = designWidth; _scaleHeight = designHeight; } } if (_scaleWidth + _scaleHeight > 0) { //if (_scaleWidth % 2 == 0) //{ // _scaleWidth += 1; //} //else //{ // _scaleWidth -= 1; //} Screen.SetResolution(_scaleWidth, _scaleHeight, true); } } #endif #endregion } }