yyl
10 天以前 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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<string> 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;
        }
 
    }
 
}