From 2605e4aeb49cf95a0a128daaa40c1d51598dff14 Mon Sep 17 00:00:00 2001 From: dmytro lytovchenko Date: Mon, 13 Apr 2020 14:24:34 +0200 Subject: [PATCH 1/2] Fixes #838; Avoid null pointer exception in the UIScaler --- TLM/TLM/U/UIScaler.cs | 43 ++++++++++++++++++++++++++++++++---- TLM/TLM/Util/GeometryUtil.cs | 7 ++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/TLM/TLM/U/UIScaler.cs b/TLM/TLM/U/UIScaler.cs index 64ec318ee..27e643d7a 100644 --- a/TLM/TLM/U/UIScaler.cs +++ b/TLM/TLM/U/UIScaler.cs @@ -5,11 +5,46 @@ namespace TrafficManager.U { using UnityEngine; public static class UIScaler { + public const float GUI_WIDTH = 1920f; + public const float GUI_HEIGHT = 1080f; + + /// Caching because UIView.Instance.uiCamera can be null sometimes. + private static float cachedGuiWidth = GUI_WIDTH; + + /// Caching because UIView.Instance.uiCamera can be null sometimes. + private static float cachedGuiHeight = GUI_HEIGHT; + /// Screen width for GUI is always fixed at 1920. - public static float GuiWidth => Singleton.instance.uiCamera.pixelWidth; + public static float GuiWidth { + // TODO: Double check if GUI never changes width, the code below can be a const + get { + UIView uiView = Singleton.instance; + if (uiView != null) { + Camera uiCamera = uiView.uiCamera; + if (uiCamera != null) { + UIScaler.cachedGuiWidth = uiCamera.pixelWidth; + } + } + + return UIScaler.cachedGuiWidth; + } + } /// Screen height for GUI is always fixed at 1080. - public static float GuiHeight => Singleton.instance.uiCamera.pixelHeight; + public static float GuiHeight { + // TODO: Double check if GUI never changes height, the code below can be a const + get { + UIView uiView = Singleton.instance; + if (uiView != null) { + Camera uiCamera = uiView.uiCamera; + if (uiCamera != null) { + UIScaler.cachedGuiHeight = uiCamera.pixelHeight; + } + } + + return UIScaler.cachedGuiHeight; + } + } /// /// Calculate UI scale based on GUI scale slider in options multiplied by uiView's scale. @@ -26,8 +61,8 @@ public static float GetScale() { /// GUI space position. internal static Vector2 ScreenPointToGuiPoint(Vector2 screenPos) { return new Vector2( - (screenPos.x * 1920f) / Screen.width, - (screenPos.y * 1080f) / Screen.height); + (screenPos.x * GUI_WIDTH) / Screen.width, + (screenPos.y * GUI_HEIGHT) / Screen.height); } } } \ No newline at end of file diff --git a/TLM/TLM/Util/GeometryUtil.cs b/TLM/TLM/Util/GeometryUtil.cs index ae7418ce9..06478fc5f 100644 --- a/TLM/TLM/Util/GeometryUtil.cs +++ b/TLM/TLM/Util/GeometryUtil.cs @@ -13,10 +13,13 @@ public static class GeometryUtil { /// Transforms a world point into a screen point. /// Position in the world. /// 2d position on screen. - /// Screen point in pixels. Note: For use in UI transform to GUI coords. + /// + /// Screen point in pixels. Note: For use in UI transform to GUI coords + /// use . + /// internal static bool WorldToScreenPoint(Vector3 worldPos, out Vector3 screenPos) { screenPos = Camera.main.WorldToScreenPoint(worldPos); - screenPos.y = UIScaler.GuiHeight - screenPos.y; + screenPos.y = Screen.height - screenPos.y; return screenPos.z >= 0; } From 5ad105041a6e65c7d761c1b24b2d86f4eb264905 Mon Sep 17 00:00:00 2001 From: dmytro lytovchenko Date: Mon, 13 Apr 2020 23:16:36 +0200 Subject: [PATCH 2/2] Using GetAView() instead of Singleton --- TLM/TLM/U/UIScaler.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/TLM/TLM/U/UIScaler.cs b/TLM/TLM/U/UIScaler.cs index 27e643d7a..ea8418002 100644 --- a/TLM/TLM/U/UIScaler.cs +++ b/TLM/TLM/U/UIScaler.cs @@ -18,12 +18,9 @@ public static class UIScaler { public static float GuiWidth { // TODO: Double check if GUI never changes width, the code below can be a const get { - UIView uiView = Singleton.instance; + UIView uiView = UIView.GetAView(); if (uiView != null) { - Camera uiCamera = uiView.uiCamera; - if (uiCamera != null) { - UIScaler.cachedGuiWidth = uiCamera.pixelWidth; - } + UIScaler.cachedGuiWidth = uiView.uiCamera.pixelWidth; } return UIScaler.cachedGuiWidth; @@ -34,12 +31,9 @@ public static float GuiWidth { public static float GuiHeight { // TODO: Double check if GUI never changes height, the code below can be a const get { - UIView uiView = Singleton.instance; + UIView uiView = UIView.GetAView(); if (uiView != null) { - Camera uiCamera = uiView.uiCamera; - if (uiCamera != null) { - UIScaler.cachedGuiHeight = uiCamera.pixelHeight; - } + UIScaler.cachedGuiHeight = uiView.uiCamera.pixelHeight; } return UIScaler.cachedGuiHeight;