Skip to content

Scene text position fixed when non-100% scale #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 26 additions & 12 deletions Assets/RuntimeDebugDraw.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using UnityEngine;
using RuntimeDebugDraw.Internal;
Expand Down Expand Up @@ -793,7 +793,7 @@ private void DrawTextOnGUI()
if (!entry.occupied)
continue;

GUIDrawTextEntry(camera, entry);
GUIDrawTextEntry(camera, entry, isSceneCam: false);
entry.flag |= DrawFlag.DrawnGUI;
}

Expand All @@ -803,18 +803,17 @@ private void DrawTextOnGUI()
if (!entry.occupied)
continue;

GUIAttachTextEntry(camera, entry);
GUIAttachTextEntry(camera, entry, isSceneCam: false);
entry.flag |= DrawFlag.DrawnGUI;
}

return;
}

private void GUIDrawTextEntry(Camera camera, DrawTextEntry entry)
private void GUIDrawTextEntry(Camera camera, DrawTextEntry entry, bool isSceneCam)
{
Vector3 worldPos = entry.anchor;
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
screenPos.y = Screen.height - screenPos.y;
Vector3 screenPos = GetCameraScreenPos(camera, worldPos, isSceneCam);

if (entry.popUp)
{
Expand All @@ -830,14 +829,13 @@ private void GUIDrawTextEntry(Camera camera, DrawTextEntry entry)
return;
}

private void GUIAttachTextEntry(Camera camera, AttachTextEntry entry)
private void GUIAttachTextEntry(Camera camera, AttachTextEntry entry, bool isSceneCam)
{
if (entry.transform == null)
return;

Vector3 worldPos = entry.transform.position + entry.offset;
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
screenPos.y = Screen.height - screenPos.y;
Vector3 screenPos = GetCameraScreenPos(camera, worldPos, isSceneCam);

_textStyle.normal.textColor = entry.color;
_textStyle.fontSize = entry.size;
Expand Down Expand Up @@ -866,7 +864,7 @@ private void DrawTextOnDrawGizmos()
if (!entry.occupied)
continue;

GUIDrawTextEntry(camera, entry);
GUIDrawTextEntry(camera, entry, isSceneCam: true);
entry.flag |= DrawFlag.DrawnGizmo;
}

Expand All @@ -876,7 +874,7 @@ private void DrawTextOnDrawGizmos()
if (!entry.occupied)
continue;

GUIAttachTextEntry(camera, entry);
GUIAttachTextEntry(camera, entry, isSceneCam: true);
entry.flag |= DrawFlag.DrawnGizmo;
}

Expand All @@ -885,7 +883,23 @@ private void DrawTextOnDrawGizmos()
return;
}
#endif

private Vector3 GetCameraScreenPos(Camera camera, Vector3 worldPos, bool isSceneCam)
{
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
screenPos.y = Screen.height - screenPos.y;

#if UNITY_EDITOR
if(isSceneCam)
{
float appScale = Screen.dpi == 0 ? 1 : Screen.dpi / 96;
screenPos /= appScale;
screenPos.y -= 40;
}
#endif

return screenPos;
}
#endregion
}
}