577 lines
22 KiB
C#
Raw Normal View History

2021-04-23 15:36:18 +08:00
using Assets.Scripts.UI.Control;
using DG.Tweening;
using Newtonsoft.Json;
2021-04-23 15:36:18 +08:00
using Newtonsoft.Json.Linq;
2021-03-25 17:18:51 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
2021-04-21 16:16:14 +08:00
using System.IO;
2021-03-25 17:18:51 +08:00
using System.Linq;
2021-04-16 17:49:11 +08:00
using System.Net.NetworkInformation;
using System.Net.Sockets;
2021-04-21 16:16:14 +08:00
using System.Runtime.InteropServices;
2021-03-25 17:18:51 +08:00
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
namespace Assets.Scripts
{
2021-04-16 17:49:11 +08:00
2021-03-25 17:18:51 +08:00
public static class Utils
{
2021-04-16 17:49:11 +08:00
/*获取ip*/
public enum ADDRESSFAM
{
IPv4, IPv6
}
/// <summary>
/// 获取本机IP
/// </summary>
/// <param name="Addfam">要获取的IP类型</param>
/// <returns></returns>
public static string GetIP(ADDRESSFAM Addfam)
{
if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
{
return null;
}
string output = "";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
//IPv4
if (Addfam == ADDRESSFAM.IPv4)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
output = ip.Address.ToString();
//Debug.Log("IP:" + output);
}
}
//IPv6
else if (Addfam == ADDRESSFAM.IPv6)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
{
output = ip.Address.ToString();
}
}
}
}
}
return output;
}
2021-10-09 10:42:27 +08:00
public static bool isPointInTransfrom(this RectTransform rect,Vector2 point)
{
var MainCanvas = UIManager.Instance.MainCanvas;
if (!MainCanvas) return false;
var size = rect.sizeDelta * MainCanvas.transform.localScale;
var sPoint = new Vector2(rect.position.x - rect.pivot.x * size.x, rect.position.y - rect.pivot.y * size.y);
var ePoint = new Vector2(rect.position.x + (1 - rect.pivot.x) * size.x, rect.position.y + (1 - rect.pivot.y) * size.y);
return point.x >= sPoint.x && point.x <= ePoint.x && point.y >= sPoint.y && point.y <= ePoint.y;
}
2021-09-16 19:49:45 +08:00
public static bool isLowIPhone()
{
var info = SystemInfo.deviceModel;
if (info.Contains("iPhone"))
{
//iPhone2,1
var s = info.Replace("iPhone", "").Split(',');
2021-09-17 09:35:15 +08:00
return s.Length == 2 && int.TryParse(s[0],out int gen) && gen < 9;
2021-09-16 19:49:45 +08:00
}
return false;
}
2021-04-16 17:49:11 +08:00
/*获取ip*/
2021-03-25 17:18:51 +08:00
/*显示简略提示需要拖Toast的预制件且保证其他组件名字不是Toast,ToastContainer*/
2021-04-23 15:36:18 +08:00
private static Dictionary<int, string> _toastDict;
private static Dictionary<int, string> toastDict
2021-03-25 17:18:51 +08:00
{
2021-04-23 15:36:18 +08:00
get
{
if (_toastDict == null)
{
_toastDict = new Dictionary<int, string>
{
{0,"Images/home/wrong|#f93086" },{1,"Images/home/right|#41A6FE" }
};
}
return _toastDict;
}
set
{
_toastDict = value;
}
}
2021-04-27 18:26:30 +08:00
private static Dictionary<string, string> _deviceDict;
public static Dictionary<string, string> deviceDict
{
get
{
if (_deviceDict == null)
{
_deviceDict = new Dictionary<string, string>
{
{"Trainer","Smart Trainer|Images/Devices/Trainer_3" },
{"Power","Power Meter|Images/Devices/Power_3" },
{"Cadence","Cadence Sensor|Images/Devices/Cadence_3" },
{"HeartRate","Heart Rate Monitor|Images/Devices/Heart Rate_3" },
{"SpeedCadence","Cadence Sensor|Images/Devices/Cadence_3" },
2021-09-23 18:14:53 +08:00
{"Speed","Speed Meter|Images/Devices/Speed_3" },
{"Rower","Rower Machine|Images/Rower/设备 - 划船机" }
2021-04-27 18:26:30 +08:00
};
}
return _deviceDict;
}
private set
{
_deviceDict = value;
}
}
public static void showToast(GameObject game,string text,int duration = 2,int type = 0)
2021-04-23 15:36:18 +08:00
{
//type 0错误 1正确
2021-04-28 15:21:06 +08:00
var parent = UIManager.Instance.ModalsPanel;
if (parent == null) return;
var toast = parent.transform.Find("ToastContainer");
2021-04-27 18:26:30 +08:00
if (toast != null)
2021-03-25 17:18:51 +08:00
{
2021-04-27 18:26:30 +08:00
MonoBehaviour.DestroyImmediate(toast.gameObject);
2021-03-25 17:18:51 +08:00
}
#if UNITY_IOS || UNITY_ANDROID
var newToast = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/ToastContainer-mobile"));
#else
2021-04-27 18:26:30 +08:00
var newToast = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/ToastContainer"));
#endif
2021-04-27 18:26:30 +08:00
newToast.name = "ToastContainer";
newToast.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
2021-04-28 15:21:06 +08:00
newToast.transform.parent = parent.transform;
2021-04-27 18:26:30 +08:00
toast = newToast.transform;
2021-04-23 15:36:18 +08:00
var toastStyle = toastDict[type].Split('|');
toast.Find("Image").GetComponent<Image>().sprite = Resources.Load<Sprite>(toastStyle[0]);
toast.GetComponent<Image>().color = HexToColorHtml(toastStyle[1]);
toast.GetComponent<Toast>().showToast(JsonConvert.SerializeObject(new
{
text,
duration
}));
2021-03-25 17:18:51 +08:00
}
/*显示简略提示需要拖Toast的预制件且保证其他组件名字不是Toast,ToastContainer*/
public static void showLoading(this GameObject game)
{
var parent = Utils.FindUpParent(game.transform);
var loading = parent.Find("Loading");
if (loading == null)
{
#if UNITY_IOS || UNITY_ANDROID
var newLoading = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/Common/Mobile/Loading"));
#else
var newLoading = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/Common/Loading"));
#endif
newLoading.name = "Loading";
newLoading.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
newLoading.transform.parent = parent;
loading = newLoading.transform;
}
loading.GetComponent<LoadingPf>().Show();
2021-04-27 20:28:03 +08:00
}
2021-10-13 19:03:14 +08:00
public static bool isUpdate(string currentVersion,string realVersion)
{
var a = currentVersion.Split('.');
var b = realVersion.Split('.');
if (a.Length != 3 || b.Length != 3) return false;
for (int i = 0; i < 3; i++)
{
var f1 = int.TryParse(a[i], out int ai);
var f2 = int.TryParse(b[i], out int bi);
if (!f1 || !f2) return false;
if (ai < bi)
{
return true;
}
}
return false;
}
2021-10-13 11:29:09 +08:00
public static void CallAndroidMethod(string methodName,params object[] args)
{
2021-10-18 14:55:44 +08:00
#if UNITY_ANDROID && !UNITY_EDITOR
2021-10-13 11:29:09 +08:00
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call(methodName, args);
2021-10-18 14:55:44 +08:00
#endif
2021-10-13 11:29:09 +08:00
}
2021-04-27 20:28:03 +08:00
public static void showLoadingExtension(this GameObject game)
{
2021-09-13 18:21:43 +08:00
game.SetActive(true);
2021-04-27 20:28:03 +08:00
var loading = game.transform.Find("Loading");
if (loading == null)
{
#if UNITY_IOS || UNITY_ANDROID
var newLoading = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/Common/Mobile/Loading"));
#else
2021-04-27 20:28:03 +08:00
var newLoading = MonoBehaviour.Instantiate(Resources.Load<GameObject>("UI/Prefab/Common/Loading"));
2021-10-18 14:55:44 +08:00
#endif
2021-04-27 20:28:03 +08:00
newLoading.name = "Loading";
newLoading.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
newLoading.transform.SetParent(game.transform);
2021-04-27 20:28:03 +08:00
loading = newLoading.transform;
2021-09-16 15:59:08 +08:00
loading.localScale = new Vector3(1, 1, 1);
2021-04-27 20:28:03 +08:00
}
loading.GetComponent<LoadingPf>().Show();
}
2021-09-13 18:21:43 +08:00
public static void hideLoadingExtension(this GameObject game)
{
var loading = game.transform.Find("Loading");
if (loading != null)
{
loading.GetComponent<LoadingPf>().Close();
}
game.SetActive(false);
}
public static void hideLoading(this GameObject game)
{
var parent = Utils.FindUpParent(game.transform);
var loading = parent.Find("Loading");
if (loading != null)
{
//loading.gameObject.SetActive(false);
loading.GetComponent<LoadingPf>().Close();
}
}
2021-03-25 17:53:48 +08:00
/*删除父亲节点下的所有孩子*/
static public void DestroyChildren(this Transform t)
{
bool isPlaying = Application.isPlaying;
while (t.childCount != 0)
{
Transform child = t.GetChild(0);
if (isPlaying)
{
2021-04-15 17:09:35 +08:00
child.SetParent(null);
2021-03-25 17:53:48 +08:00
UnityEngine.Object.Destroy(child.gameObject);
}
else UnityEngine.Object.DestroyImmediate(child.gameObject);
}
Resources.UnloadUnusedAssets();
GC.Collect();
2021-03-25 17:53:48 +08:00
}
/*删除父亲节点下的所有孩子*/
2021-03-25 17:18:51 +08:00
/*显示网络图片*/
2021-05-08 14:38:23 +08:00
private static Dictionary<string, Texture> propTextureCache = new Dictionary<string, Texture>();
2021-03-25 17:18:51 +08:00
public delegate Coroutine StartCoroutine(IEnumerator routine);
2021-05-08 14:38:23 +08:00
public static void DisplayImage(RawImage img, string url, bool cache = false)
2021-03-25 17:18:51 +08:00
{
if (string.IsNullOrEmpty(url))
return;
2021-05-08 14:38:23 +08:00
//可以考虑缓存到硬盘里
//Texture2D tex = null;
//byte[] fileData;
//
//if (File.Exists(filePath))
//{
// fileData = File.ReadAllBytes(filePath);
// tex = new Texture2D(2, 2);
// tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
//}
if (cache)
{
if (propTextureCache.ContainsKey(url))
{
img.texture = propTextureCache[url];
return;
}
}
UIManager.Instance.StartCoroutine(DownloadImage(img, url,(texture)=> {
2021-05-10 10:21:46 +08:00
if (cache && !propTextureCache.ContainsKey(url))
2021-05-08 14:38:23 +08:00
{
propTextureCache.Add(url, texture);
}
}));
2021-03-25 17:18:51 +08:00
}
2021-04-25 13:33:43 +08:00
2021-04-25 19:41:35 +08:00
public static void DisplayImageAysnc(StartCoroutine startCoroutine, RawImage img, string url,Action<string> action)
2021-04-25 13:33:43 +08:00
{
startCoroutine(DownloadImageCallBack(img, url, action));
}
2021-05-08 14:38:23 +08:00
static IEnumerator DownloadImage(RawImage img,string MediaUrl, Action<Texture> action = null)
2021-03-25 17:18:51 +08:00
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);
2021-08-27 15:06:56 +08:00
else if(img!=null)
{
2021-03-25 17:18:51 +08:00
img.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
if (action != null)
{
action.Invoke(img.texture);
}
}
2021-03-25 17:18:51 +08:00
}
2021-04-25 13:33:43 +08:00
2021-04-25 19:41:35 +08:00
static IEnumerator DownloadImageCallBack(RawImage img, string MediaUrl,Action<string> action)
2021-04-25 13:33:43 +08:00
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);
else
{
img.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
2021-04-25 19:41:35 +08:00
action.Invoke(MediaUrl);
2021-04-25 13:33:43 +08:00
}
}
2021-03-25 17:18:51 +08:00
/*显示网络图片*/
/*获取最顶层对象*/
public static Transform FindUpParent(Transform zi)
{
if (zi.parent == null)
return zi;
else
return FindUpParent(zi.parent);
}
/*获取最顶层对象*/
/*Texture相关*/
public static void WriteTextureToPlayerPrefs(string tag, Texture2D tex)
{
// if texture is png otherwise you can use tex.EncodeToJPG().
byte[] texByte = tex.EncodeToPNG();
// convert byte array to base64 string
string base64Tex = System.Convert.ToBase64String(texByte);
// write string to playerpref
PlayerPrefs.SetString(tag, base64Tex);
PlayerPrefs.Save();
}
public static Texture2D ReadTextureFromPlayerPrefs(string tag)
{
// load string from playerpref
string base64Tex = PlayerPrefs.GetString(tag, null);
if (!string.IsNullOrEmpty(base64Tex))
{
// convert it to byte array
byte[] texByte = System.Convert.FromBase64String(base64Tex);
Texture2D tex = new Texture2D(2, 2);
//load texture from byte array
if (tex.LoadImage(texByte))
{
return tex;
}
}
return null;
}
/*Texture相关*/
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
public static Color HexToColor(string hex)
{
byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
return new Color32(r, g, b, byte.MaxValue);
}
public static Color HexToColorHtml(string hex)
{
ColorUtility.TryParseHtmlString(hex, out Color c);
return c;
}
2021-04-14 15:02:33 +08:00
/// <summary>
/// Generate random digit code
/// </summary>
/// <param name="length">Length</param>
/// <returns>Result string</returns>
public static string GenerateRandomDigitCode(int length)
{
var random = new System.Random();
string str = string.Empty;
for (int i = 0; i < length; i++)
str = String.Concat(str, random.Next(10).ToString());
return str;
}
2021-04-21 11:19:31 +08:00
2021-04-21 16:16:14 +08:00
/*文件转精灵*/
public static Sprite PngToSprite(string fullPath, int x, int y)
{
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) //自动双清
{
fs.Seek(0, SeekOrigin.Begin); //设定当前流的位置
byte[] bytes = new byte[fs.Length]; //创建文件长度缓冲区
fs.Read(bytes, 0, (int)fs.Length); //读取文件
Texture2D texture = new Texture2D(x, y); //创建Texture
texture.LoadImage(bytes);
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one / 2);
}
}
/*文件转精灵*/
/*打开文件*/
public delegate void OpenFileCallBack(string path);
public static void OpenFile(OpenFileCallBack callBack,string filter = null)
{
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)\0*.jpg;*.png;*.jpeg;*.bmp\0";
if (filter != null) ofn.filter = filter;
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
ofn.title = "Open Project";
ofn.defExt = "JPG";//显示文件的类型
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
//0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008 OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
2021-04-21 16:16:14 +08:00
if (Win32.GetOpenFileName(ofn))
{
callBack(ofn.file);
}
}
/*打开文件*/
/*文本框/下拉框错误*/
2021-04-23 15:36:18 +08:00
public static void SetValidate(Dictionary<string, Selectable> dict, JArray errorList)
{
foreach (var error in errorList)
{
var className = dict[error.Value<string>("Field")].GetType().Name;
if (className == "InputField")
{
dict[error.Value<string>("Field")].GetComponent<PFUIInputField>()
.SetValidate(true);
}
else if (className == "Dropdown")
{
dict[error.Value<string>("Field")].GetComponent<PFUIDropdown>()
.SetValidate();
}
//formDict[error.Value<string>("Filed")].error.Value<string>("ErrMsg");
}
}
/*文本框/下拉框错误*/
2021-05-10 10:21:46 +08:00
2021-10-08 15:22:20 +08:00
public static void ExecFile(string path,string args = "")
2021-05-10 10:21:46 +08:00
{
System.Diagnostics.Process proc = null;
try
{
proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = path;
2021-10-08 15:22:20 +08:00
proc.StartInfo.Arguments = args;
2021-05-10 10:21:46 +08:00
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Verb = "runas";
proc.Start();
}
catch (Exception ex)
{
Console.WriteLine("执行批处理的时候出现了异常!");
}
}
/*下载文件*/
2021-05-20 20:00:33 +08:00
public static void DisplayHead(RawImage raw, string s)
{
if (string.IsNullOrEmpty(s))
{
raw.texture = Resources.Load<Texture>("Images/icon-1024");
}
else
{
DisplayImage(raw, s);
}
}
2021-07-20 09:58:01 +08:00
public static void SetTextWithEllipsis(this Text textComponent, string value)
{
var generator = new TextGenerator();
var rectTransform = textComponent.GetComponent<RectTransform>();
var settings = textComponent.GetGenerationSettings(rectTransform.rect.size);
generator.Populate(value, settings);
var characterCountVisible = generator.characterCountVisible;
var updatedText = value;
if (value.Length > characterCountVisible && characterCountVisible != -1)
{
updatedText = value.Substring(0, characterCountVisible - 3);
updatedText += "…";
}
textComponent.text = updatedText;
}
public static string GetCountDown(TimeSpan time)
{
var second = Math.Ceiling(time.TotalSeconds);
var ts = TimeSpan.FromSeconds(second);
return $"{ (int)ts.TotalHours }:{ts:mm}:{ts:ss}";
}
2021-07-26 19:50:10 +08:00
private static IEnumerator TimerFunc(float seconds, Action action)
{
while (true)
{
yield return new WaitForSeconds(seconds); // 停止执行1秒
action.Invoke();
//time = UIManager.Now.GetDateTime().AddSeconds(1);
}
}
public static void StartTimeUnLimit(float seconds,Action action)
{
UIManager.Instance.StartCoroutine(TimerFunc(seconds, action));
}
public static void MyDOFade(this Transform transform)
{
var cg = transform.GetComponent<CanvasGroup>();
if (cg)
{
cg.alpha = 0;
#if UNITY_IOS
cg.DOFade(1f, .6f);
#else
cg.DOFade(1f, .3f);
#endif
}
}
2021-03-25 17:18:51 +08:00
}
}