using System; using System.Threading.Tasks; using UnityEngine; namespace Mogafa.Unity.Common.Contexts { public class MogafaApplication { private static string deviceId; public static Task GetDeviceId() { var tcs = new TaskCompletionSource(); if (!string.IsNullOrEmpty(deviceId)) { tcs.SetResult(deviceId); return tcs.Task; } #if UNITY_ANDROID && !UNITY_EDITOR try { AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = up.GetStatic("currentActivity"); AndroidJavaClass client = new AndroidJavaClass("com.google.android.gms.ads.identifier.AdvertisingIdClient"); AndroidJavaObject adInfo = client.CallStatic("getAdvertisingIdInfo", currentActivity); deviceId = adInfo.Call("getId").ToString(); if(Guid.Empty.ToString() == deviceId) { deviceId = SystemInfo.deviceUniqueIdentifier; } tcs.SetResult(deviceId); } catch (Exception ex) { deviceId = SystemInfo.deviceUniqueIdentifier; tcs.SetResult(deviceId); } #elif UNITY_IOS && !UNIRY_EDITOR deviceId = UnityEngine.iOS.Device.vendorIdentifier; tcs.SetResult(deviceId); #else deviceId = SystemInfo.deviceUniqueIdentifier; tcs.SetResult(deviceId); #endif return tcs.Task; } public static string PlatfromName { get { switch (Application.platform) { case RuntimePlatform.Android: return PlatformNames.Android; case RuntimePlatform.IPhonePlayer: return PlatformNames.iOS; case RuntimePlatform.WindowsPlayer: return PlatformNames.Windows; case RuntimePlatform.OSXPlayer: return PlatformNames.Mac; } return PlatformNames.Others; } } } }