437 lines
12 KiB
C#
437 lines
12 KiB
C#
//#define EDITOR_GAMEVIEW_CHANGE_ENABLE
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using GameCore;
|
|
using asap.core;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
public class GlobalUtils
|
|
{
|
|
public static string gameVersion
|
|
{
|
|
get
|
|
{
|
|
#if UNITY_EDITOR
|
|
return Application.version;
|
|
#else
|
|
return string.Format("{0}.{1}", Application.version, Application.buildGUID.GetHashCode().ToString("x"));
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public static T StringToEnum<T>(string modestr, T defaultValue) where T : Enum
|
|
{
|
|
foreach (T mode in Enum.GetValues(typeof(T)))
|
|
{
|
|
if (mode.ToString() == modestr)
|
|
return mode;
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public static bool SaveFile(string content, string destPath)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(content);
|
|
return SaveFile(bytes, destPath);
|
|
}
|
|
|
|
public static bool SaveFile(Byte[] bytes, string destPath)
|
|
{
|
|
if (bytes == null || string.IsNullOrEmpty(destPath))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool result = true;
|
|
int index = destPath.LastIndexOf("/");
|
|
string directory = destPath.Substring(0, index);
|
|
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GameDebug.LogError("[GlobalUtils]SaveFile: {0}", ex.Message);
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
if (Directory.Exists(directory) && !File.Exists(destPath))
|
|
{
|
|
try
|
|
{
|
|
using (var file = File.Create(destPath)) { }
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GameDebug.LogError("[GlobalUtils]SaveFile: {0}", ex.Message);
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
if (File.Exists(destPath))
|
|
{
|
|
try
|
|
{
|
|
File.WriteAllBytes(destPath, bytes);
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
GameDebug.LogError("[GlobalUtils]SaveFile: {0}", ex.Message);
|
|
result = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GameDebug.LogError("[GlobalUtils]SaveFile: {0}", ex.Message);
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
if (!result)
|
|
{
|
|
MessageDialog.ShowSomethingWrong();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
public static byte[] AesEncrypt(byte[] array, string key)
|
|
{
|
|
if (array == null || array.Length <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
var toEncryptArray = array;
|
|
var rm = new System.Security.Cryptography.RijndaelManaged
|
|
{
|
|
Key = Encoding.UTF8.GetBytes(key),
|
|
Mode = System.Security.Cryptography.CipherMode.ECB,
|
|
Padding = System.Security.Cryptography.PaddingMode.PKCS7
|
|
};
|
|
|
|
var cTransform = rm.CreateEncryptor();
|
|
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
|
|
return resultArray;
|
|
}
|
|
|
|
public static byte[] AesDecrypt(byte[] array, string key)
|
|
{
|
|
if (array == null || array.Length <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
var toEncryptArray = array;
|
|
var rm = new System.Security.Cryptography.RijndaelManaged
|
|
{
|
|
Key = Encoding.UTF8.GetBytes(key),
|
|
Mode = System.Security.Cryptography.CipherMode.ECB,
|
|
Padding = System.Security.Cryptography.PaddingMode.PKCS7
|
|
};
|
|
|
|
var cTransform = rm.CreateDecryptor();
|
|
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
|
|
return resultArray;
|
|
}
|
|
|
|
static decimal unitT = new decimal(1000000000000L);
|
|
static decimal unitQ = new decimal(1000000000000000L);
|
|
|
|
public static string GetNumStringFloorInt(ulong coin_num)
|
|
{
|
|
decimal v = 0m;
|
|
var suffix = string.Empty;
|
|
|
|
if (coin_num < 1000)
|
|
{
|
|
v = coin_num;
|
|
}
|
|
else if (coin_num >= 1000 && coin_num < 1000000)
|
|
{
|
|
v = coin_num / 1000m;
|
|
suffix = "K";
|
|
}
|
|
else if (coin_num >= 1000000 && coin_num < 1000000000)
|
|
{
|
|
v = coin_num / 1000000m;
|
|
suffix = "M";
|
|
}
|
|
else if (coin_num >= 1000000000 && coin_num < 1000000000000)
|
|
{
|
|
v = coin_num / 1000000000m;
|
|
suffix = "B";
|
|
}
|
|
else if (coin_num >= 1000000000000 && coin_num < 1000000000000000)
|
|
{
|
|
v = coin_num / unitT;
|
|
suffix = "T";
|
|
}
|
|
else if (coin_num >= 1000000000000000)
|
|
{
|
|
v = coin_num / unitQ;
|
|
suffix = "Q";
|
|
}
|
|
|
|
var floor_v = decimal.Truncate(v);
|
|
var num_str = $"{floor_v:0}{suffix}";
|
|
|
|
return num_str;
|
|
}
|
|
|
|
public static string GetNumString(ulong coin_num)
|
|
{
|
|
decimal v = 0m;
|
|
var suffix = string.Empty;
|
|
|
|
if (coin_num < 1000)
|
|
{
|
|
v = coin_num;
|
|
}
|
|
else if (coin_num >= 1000 && coin_num < 1000000)
|
|
{
|
|
v = coin_num / 1000m;
|
|
suffix = "K";
|
|
}
|
|
else if (coin_num >= 1000000 && coin_num < 1000000000)
|
|
{
|
|
v = coin_num / 1000000m;
|
|
suffix = "M";
|
|
}
|
|
else if (coin_num >= 1000000000 && coin_num < 1000000000000)
|
|
{
|
|
v = coin_num / 1000000000m;
|
|
suffix = "B";
|
|
}
|
|
else if (coin_num >= 1000000000000 && coin_num < 1000000000000000)
|
|
{
|
|
v = coin_num / unitT;
|
|
suffix = "T";
|
|
}
|
|
else if (coin_num >= 1000000000000000)
|
|
{
|
|
v = coin_num / unitQ;
|
|
suffix = "Q";
|
|
}
|
|
|
|
var truncate_v = decimal.Truncate(v * 10) / 10;
|
|
var num_str = $"{truncate_v:0.#}{suffix}";
|
|
|
|
return num_str;
|
|
}
|
|
|
|
// public static string GetPayCountStr(uint price)
|
|
// {
|
|
// double pricereal = (double)((double)price / 100f);
|
|
// return pricereal.ToString("N");
|
|
// }
|
|
|
|
static readonly string[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB" };
|
|
|
|
public static string GetSizeString(Int64 bytes)
|
|
{
|
|
int counter = 0;
|
|
decimal number = (decimal)bytes;
|
|
while (Math.Round(number / 1024) >= 1)
|
|
{
|
|
number /= 1024;
|
|
counter++;
|
|
}
|
|
|
|
return string.Format("{0:n1}{1}", number, suffixes[counter]);
|
|
}
|
|
|
|
public static RectTransform SetParent(Transform child, Transform parent)
|
|
{
|
|
RectTransform childrect = child.transform.GetComponent<RectTransform>();
|
|
RectTransform _parent = parent.transform.GetComponent<RectTransform>();
|
|
childrect.SetParent(_parent);
|
|
childrect.localScale = Vector3.one;
|
|
childrect.anchoredPosition = Vector2.zero;
|
|
return childrect;
|
|
}
|
|
|
|
//public static void SetRendererGray(SpriteRenderer rend)
|
|
//{
|
|
//Material mat = AssetManager.LoadAsset<Material>("ImageGrayMat");
|
|
|
|
//if (mat != null)
|
|
//{
|
|
//rend.material = mat;
|
|
//}
|
|
//}
|
|
|
|
public static int TryParseInt(string data_string, int default_value = 0)
|
|
{
|
|
int set_data = default_value;
|
|
|
|
if (int.TryParse(data_string, out int parsed_value))
|
|
{
|
|
set_data = parsed_value;
|
|
}
|
|
|
|
return set_data;
|
|
}
|
|
|
|
public static ulong TryParseUlong(string data_string, ulong default_value = 0)
|
|
{
|
|
ulong set_data = default_value;
|
|
|
|
if (ulong.TryParse(data_string, out ulong parsed_value))
|
|
{
|
|
set_data = parsed_value;
|
|
}
|
|
|
|
return set_data;
|
|
}
|
|
|
|
public static DateTime TryParseDateTime(string data_string, DateTime default_value)
|
|
{
|
|
DateTime set_data = default_value;
|
|
|
|
if (DateTime.TryParse(data_string, out DateTime parsed_value))
|
|
{
|
|
set_data = parsed_value;
|
|
}
|
|
|
|
return set_data;
|
|
}
|
|
|
|
#if UNITY_EDITOR && EDITOR_GAMEVIEW_CHANGE_ENABLE
|
|
static ScreenType currentGameViewType = ScreenType.INVALID;
|
|
#endif
|
|
|
|
public static void ChangeEditorGameView(ScreenType game_view_type)
|
|
{
|
|
#if UNITY_EDITOR && EDITOR_GAMEVIEW_CHANGE_ENABLE
|
|
if (currentGameViewType == game_view_type) return;
|
|
|
|
var is_auto_change_gameview = EditorPrefs.GetBool("AutoChangeGameView", false);
|
|
|
|
if (is_auto_change_gameview)
|
|
{
|
|
var windows = (EditorWindow[])Resources.FindObjectsOfTypeAll(typeof(EditorWindow));
|
|
EditorWindow gameview_landscape = null;
|
|
EditorWindow gameview_portrait = null;
|
|
EditorWindow ab_buildview = null;
|
|
EditorWindow inspector_window = null;
|
|
EditorWindow jconsole_window = null;
|
|
int num_gameview = 0;
|
|
|
|
foreach (var window in windows)
|
|
{
|
|
if (window == null) continue;
|
|
|
|
if (window.GetType().FullName == "UnityEditor.GameView")
|
|
{
|
|
var gameview_type = GameViewUtils.GetGameViewType(window);
|
|
|
|
if (gameview_type == ScreenType.LANDSCAPE)
|
|
{
|
|
gameview_landscape = window;
|
|
}
|
|
else
|
|
{
|
|
gameview_portrait = window;
|
|
}
|
|
|
|
num_gameview++;
|
|
}
|
|
else if (window.titleContent.text == "Inspector")
|
|
{
|
|
inspector_window = window;
|
|
}
|
|
else if (window.titleContent.text == "AB打包窗口")
|
|
{
|
|
ab_buildview = window;
|
|
}
|
|
else if (window.titleContent.text == "JConsole")
|
|
{
|
|
jconsole_window = window;
|
|
}
|
|
}
|
|
|
|
if (game_view_type == ScreenType.PORTRAIT)
|
|
{
|
|
if (num_gameview == 1)
|
|
{
|
|
GameViewUtils.SetSize(5);
|
|
}
|
|
else if (num_gameview == 2)
|
|
{
|
|
if (ab_buildview != null)
|
|
{
|
|
ab_buildview.Focus();
|
|
}
|
|
|
|
if (gameview_portrait != null)
|
|
{
|
|
gameview_portrait.Focus();
|
|
}
|
|
|
|
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (num_gameview == 1)
|
|
{
|
|
GameViewUtils.SetSize(6);
|
|
}
|
|
else if (num_gameview == 2)
|
|
{
|
|
if (jconsole_window != null)
|
|
{
|
|
jconsole_window.Focus();
|
|
}
|
|
else if (inspector_window != null)
|
|
{
|
|
inspector_window.Focus();
|
|
}
|
|
|
|
if (gameview_landscape != null)
|
|
{
|
|
gameview_landscape.Focus();
|
|
}
|
|
|
|
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var windows = (EditorWindow[])Resources.FindObjectsOfTypeAll(typeof(EditorWindow));
|
|
foreach (var window in windows)
|
|
{
|
|
if (window != null && window.GetType().FullName == "UnityEditor.GameView")
|
|
{
|
|
if (!window.hasFocus)
|
|
{
|
|
window.Focus();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
|
|
}
|
|
|
|
currentGameViewType = game_view_type;
|
|
#endif
|
|
}
|
|
|
|
}
|