Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid null pointer exception in the UIScaler #839

Merged
merged 2 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions TLM/TLM/U/UIScaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,40 @@ namespace TrafficManager.U {
using UnityEngine;

public static class UIScaler {
public const float GUI_WIDTH = 1920f;
public const float GUI_HEIGHT = 1080f;

/// <summary>Caching because UIView.Instance.uiCamera can be null sometimes.</summary>
private static float cachedGuiWidth = GUI_WIDTH;

/// <summary>Caching because UIView.Instance.uiCamera can be null sometimes.</summary>
private static float cachedGuiHeight = GUI_HEIGHT;

/// <summary>Screen width for GUI is always fixed at 1920.</summary>
public static float GuiWidth => Singleton<UIView>.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 = UIView.GetAView();
if (uiView != null) {
UIScaler.cachedGuiWidth = uiView.uiCamera.pixelWidth;
}

return UIScaler.cachedGuiWidth;
}
}

/// <summary>Screen height for GUI is always fixed at 1080.</summary>
public static float GuiHeight => Singleton<UIView>.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 = UIView.GetAView();
if (uiView != null) {
UIScaler.cachedGuiHeight = uiView.uiCamera.pixelHeight;
}

return UIScaler.cachedGuiHeight;
}
}

/// <summary>
/// Calculate UI scale based on GUI scale slider in options multiplied by uiView's scale.
Expand All @@ -26,8 +55,8 @@ public static float GetScale() {
/// <returns>GUI space position.</returns>
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);
}
}
}
7 changes: 5 additions & 2 deletions TLM/TLM/Util/GeometryUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public static class GeometryUtil {
/// <summary>Transforms a world point into a screen point.</summary>
/// <param name="worldPos">Position in the world.</param>
/// <param name="screenPos">2d position on screen.</param>
/// <returns>Screen point in pixels. Note: For use in UI transform to GUI coords.</returns>
/// <returns>
/// Screen point in pixels. Note: For use in UI transform to GUI coords
/// use <see cref="UIScaler.ScreenPointToGuiPoint"/>.
/// </returns>
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;
}
Expand Down