using System; using System.Runtime.InteropServices; using Unity.UIWidgets.ui; using UnityEngine; namespace Unity.UIWidgets.engine { [StructLayout(LayoutKind.Sequential)] public struct viewMetrics { public float insets_top; public float insets_bottom; public float insets_left; public float insets_right; public float padding_top; public float padding_bottom; public float padding_left; public float padding_right; } public static class DisplayMetricsProvider { public static Func provider = () => new PlayerDisplayMetrics(); } public interface DisplayMetrics { void OnEnable(); void OnGUI(); void Update(); void onViewMetricsChanged(); float devicePixelRatio { get; } viewMetrics viewMetrics { get; } WindowPadding viewPadding { get; } WindowPadding viewInsets { get; } } public class PlayerDisplayMetrics : DisplayMetrics { float _devicePixelRatio = 0; viewMetrics? _viewMetrics = null; public void OnEnable() { } public void OnGUI() { } public void Update() { } public void onViewMetricsChanged() { //view metrics marks dirty _viewMetrics = null; } public float devicePixelRatio { get { if (_devicePixelRatio > 0) { return _devicePixelRatio; } #if UNITY_ANDROID _devicePixelRatio = AndroidDevicePixelRatio(); #endif #if UNITY_WEBGL this._devicePixelRatio = UIWidgetsWebGLDevicePixelRatio(); #endif #if UNITY_IOS //this._devicePixelRatio = IOSDeviceScaleFactor(); #endif if (_devicePixelRatio <= 0) { _devicePixelRatio = 1; } return _devicePixelRatio; } } public WindowPadding viewPadding { get { return new WindowPadding(viewMetrics.padding_left, viewMetrics.padding_top, viewMetrics.padding_right, viewMetrics.padding_bottom); } } public WindowPadding viewInsets { get { return new WindowPadding(viewMetrics.insets_left, viewMetrics.insets_top, viewMetrics.insets_right, viewMetrics.insets_bottom); } } public viewMetrics viewMetrics { get { if (_viewMetrics != null) { return _viewMetrics.Value; } #if UNITY_ANDROID using ( AndroidJavaClass viewController = new AndroidJavaClass("com.unity.uiwidgets.plugin.UIWidgetsViewController") ) { AndroidJavaObject metrics = viewController.CallStatic("getMetrics"); float insets_bottom = metrics.Get("insets_bottom"); float insets_top = metrics.Get("insets_top"); float insets_left = metrics.Get("insets_left"); float insets_right = metrics.Get("insets_right"); float padding_bottom = metrics.Get("padding_bottom"); float padding_top = metrics.Get("padding_top"); float padding_left = metrics.Get("padding_left"); float padding_right = metrics.Get("padding_right"); _viewMetrics = new viewMetrics { insets_bottom = insets_bottom, insets_left = insets_left, insets_right = insets_right, insets_top = insets_top, padding_left = padding_left, padding_top = padding_top, padding_right = padding_right, padding_bottom = padding_bottom }; } #elif UNITY_WEBGL this._viewMetrics = new viewMetrics { insets_bottom = 0, insets_left = 0, insets_right = 0, insets_top = 0, padding_left = 0, padding_top = 0, padding_right = 0, padding_bottom = 0 }; #elif UNITY_IOS //viewMetrics metrics = IOSGetViewportPadding(); //this._viewMetrics = metrics; #else _viewMetrics = new viewMetrics { insets_bottom = 0, insets_left = 0, insets_right = 0, insets_top = 0, padding_left = 0, padding_top = 0, padding_right = 0, padding_bottom = 0 }; #endif return _viewMetrics.Value; } } #if UNITY_ANDROID static float AndroidDevicePixelRatio() { using ( AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer") ) { using ( AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"), activityInstance = unityPlayerClass.GetStatic("currentActivity"), windowManagerInstance = activityInstance.Call("getWindowManager"), displayInstance = windowManagerInstance.Call("getDefaultDisplay") ) { displayInstance.Call("getMetrics", metricsInstance); return metricsInstance.Get("density"); } } } #endif #if UNITY_WEBGL [DllImport("__Internal")] static extern float UIWidgetsWebGLDevicePixelRatio(); #endif #if UNITY_IOS //[DllImport("__Internal")] //static extern float IOSDeviceScaleFactor(); //[DllImport("__Internal")] //static extern viewMetrics IOSGetViewportPadding(); #endif } }