using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Text.RegularExpressions; using Cysharp.Threading.Tasks; using UnityEngine.Networking; #if !UNITY_WEBGL using System.Net.NetworkInformation; using System.Net; using System.IO; #endif #if UNITY_IOS using UnityEngine.iOS; #endif public class DeviceUtility { static string mac = string.Empty; public static string GetMac() { #if !UNITY_WEBGL if (string.IsNullOrEmpty(mac)) { try { var netInterfaces = NetworkInterface.GetAllNetworkInterfaces(); if (netInterfaces != null && netInterfaces.Length > 0) { mac = netInterfaces[0].GetPhysicalAddress().ToString(); } } catch (Exception ex) { Debug.Log(ex); } } #endif return mac; } static string ipPattern = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"; static string ip = string.Empty; public static string GetCachedIp() => ip; public static async UniTask GetIp() { try { if (string.IsNullOrEmpty(ip)) { string url = "http://pv.sohu.com/cityjson"; using (var request = UnityWebRequest.Get(url)) { await request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { string str = request.downloadHandler.text; var match = Regex.Match(str, ipPattern); if (match != null) { ip = match.Value; } } } } } catch (Exception ex) { Debug.Log(ex); } return ip; } public static string GetDeviceUniquenessIdentify() { #if UNITY_IOS return UnityEngine.iOS.Device.advertisingIdentifier; #else // SystemInfo.deviceUniqueIdentifier is deprecated in Unity 2022+ // Use platform-specific solutions or GUID return System.Guid.NewGuid().ToString(); #endif } public static string GetDeviceOSLevel() { #if UNITY_IOS return UnityEngine.iOS.Device.systemVersion; #else return Application.platform.ToString(); #endif } public static string GetDeviceName() { #if UNITY_IOS return UnityEngine.iOS.Device.generation.ToString(); #else return Application.productName; #endif } public static string GetDeviceModel() { #if UNITY_IOS return UnityEngine.iOS.Device.generation.ToString(); #else return Application.platform.ToString(); #endif } public static bool IsLowMemory() { #if UNITY_IOS if (Application.platform == RuntimePlatform.IPhonePlayer) { switch (Device.generation) { case DeviceGeneration.iPadMini2Gen: case DeviceGeneration.iPhone5S: case DeviceGeneration.iPhone6: case DeviceGeneration.iPhone6Plus: return true; default: return false; } } else { return false; } #endif return false; } public static int cpu = 2; public static int memory = 2 * 1024; public static void GetCpuAndMemory(out int _cpu, out int _memory)//内存单位是MB { try { _cpu = Environment.ProcessorCount; switch (Application.platform) { case RuntimePlatform.Android: _memory = 0; break; case RuntimePlatform.IPhonePlayer: _memory = IsLowMemory() ? 1 : 2 * 1024; break; default: _memory = 4 * 1024; break; } cpu = _cpu; memory = _memory; } catch (Exception ex) { Debug.Log(ex); _cpu = 2; _memory = 2 * 1024; } } }