Files
AppleHillsProduction/Assets/Scripts/AppleHillsCamera/ScreenReferenceMarker.cs

206 lines
8.8 KiB
C#
Raw Normal View History

using UnityEngine;
namespace AppleHillsCamera
{
/// <summary>
/// Defines reference sizes and distances for screen adaptation.
/// Used by CameraScreenAdapter and EdgeAnchor components.
/// </summary>
public class ScreenReferenceMarker : MonoBehaviour
{
public enum ScalingMode
{
Width,
Height
}
[Header("Scaling Mode")]
[Tooltip("Whether to scale the camera based on width or height")]
public ScalingMode scalingMode = ScalingMode.Width;
[Header("Reference Dimensions")]
[Tooltip("The target width that should match the screen width (used when scaling by width)")]
public float targetWidth = 10f;
[Tooltip("The target height that should match the screen height (used when scaling by height)")]
public float targetHeight = 5f;
[Header("Margins")]
[Tooltip("Distance from top of screen to use for anchoring")]
public float topMargin = 1f;
[Tooltip("Distance from bottom of screen to use for anchoring")]
public float bottomMargin = 1f;
[Tooltip("Distance from left of screen to use for anchoring")]
public float leftMargin = 1f;
[Tooltip("Distance from right of screen to use for anchoring")]
public float rightMargin = 1f;
[Header("Visualization")]
[Tooltip("Color to use for gizmo visualization")]
public Color gizmoColor = new Color(0f, 1f, 0f, 0.5f);
[Tooltip("Color to use for screen edge visualization")]
public Color screenEdgeColor = new Color(1f, 1f, 0f, 0.3f);
[Tooltip("Show the vertical margins in scene view")]
public bool showVerticalMargins = true;
[Tooltip("Show the horizontal margins in scene view")]
public bool showHorizontalMargins = true;
#if UNITY_EDITOR
private void OnDrawGizmos()
{
// Save original color
Color originalColor = Gizmos.color;
// Set the color for our gizmos
Gizmos.color = gizmoColor;
Vector3 position = transform.position;
// Calculate visual screen edges based on scaling mode and actual camera viewport
float halfWidth;
float halfHeight;
// Try to get camera references in the preferred order
// 1. Try to find the main camera in the scene (highest priority)
Camera mainCamera = Camera.main;
if (mainCamera != null && mainCamera.orthographic)
{
if (scalingMode == ScalingMode.Width)
{
// When scaling by width, the width is fixed and height varies
halfWidth = targetWidth / 2f;
// Use the main camera's actual orthographic size for the height
halfHeight = mainCamera.orthographicSize;
}
else // ScalingMode.Height
{
// When scaling by height, the height is fixed and width varies
halfHeight = targetHeight / 2f;
// Calculate width based on camera's aspect ratio
float screenAspect = (float)Screen.width / Screen.height;
halfWidth = halfHeight * screenAspect;
}
}
else
{
// 2. Use Game/Simulator window resolution
float gameViewAspect = (float)Screen.height / Screen.width;
if (scalingMode == ScalingMode.Width)
{
halfWidth = targetWidth / 2f;
halfHeight = halfWidth * gameViewAspect;
}
else // ScalingMode.Height
{
halfHeight = targetHeight / 2f;
halfWidth = halfHeight / gameViewAspect;
}
// 3. Fallback to the scene view camera if needed
UnityEditor.SceneView sceneView = UnityEditor.SceneView.lastActiveSceneView;
if (sceneView != null && sceneView.camera != null && sceneView.camera.orthographic)
{
// Use the scene view camera's aspect ratio instead
float sceneAspect = sceneView.camera.pixelHeight / (float)sceneView.camera.pixelWidth;
if (scalingMode == ScalingMode.Width)
{
halfHeight = halfWidth * sceneAspect;
}
else // ScalingMode.Height
{
float sceneAspectInv = sceneView.camera.pixelWidth / (float)sceneView.camera.pixelHeight;
halfWidth = halfHeight * sceneAspectInv;
}
}
}
// Draw the reference dimension indicator based on scaling mode
if (scalingMode == ScalingMode.Width)
{
// Draw the width reference (horizontal line)
Vector3 left = position + Vector3.left * halfWidth;
Vector3 right = position + Vector3.right * halfWidth;
Gizmos.DrawLine(left, right);
// Draw vertical endpoints
float endCapSize = 0.5f;
Gizmos.DrawLine(left, left + Vector3.up * endCapSize);
Gizmos.DrawLine(left, left + Vector3.down * endCapSize);
Gizmos.DrawLine(right, right + Vector3.up * endCapSize);
Gizmos.DrawLine(right, right + Vector3.down * endCapSize);
}
else // ScalingMode.Height
{
// Draw the height reference (vertical line)
Vector3 top = position + Vector3.up * halfHeight;
Vector3 bottom = position + Vector3.down * halfHeight;
Gizmos.DrawLine(top, bottom);
// Draw horizontal endpoints
float endCapSize = 0.5f;
Gizmos.DrawLine(top, top + Vector3.left * endCapSize);
Gizmos.DrawLine(top, top + Vector3.right * endCapSize);
Gizmos.DrawLine(bottom, bottom + Vector3.left * endCapSize);
Gizmos.DrawLine(bottom, bottom + Vector3.right * endCapSize);
}
// Screen edge positions
Vector3 topEdge = position + Vector3.up * halfHeight;
Vector3 bottomEdge = position + Vector3.down * halfHeight;
Vector3 leftEdge = position + Vector3.left * halfWidth;
Vector3 rightEdge = position + Vector3.right * halfWidth;
// Draw screen edges with yellow color
Gizmos.color = screenEdgeColor;
// Draw full screen rectangle
Gizmos.DrawLine(leftEdge + Vector3.up * halfHeight, rightEdge + Vector3.up * halfHeight); // Top edge
Gizmos.DrawLine(leftEdge + Vector3.down * halfHeight, rightEdge + Vector3.down * halfHeight); // Bottom edge
Gizmos.DrawLine(leftEdge + Vector3.up * halfHeight, leftEdge + Vector3.down * halfHeight); // Left edge
Gizmos.DrawLine(rightEdge + Vector3.up * halfHeight, rightEdge + Vector3.down * halfHeight); // Right edge
// Draw margin references if enabled
Gizmos.color = gizmoColor;
if (showVerticalMargins)
{
// Top margin (distance from top edge)
Gizmos.DrawLine(
topEdge + Vector3.down * topMargin + Vector3.left * (targetWidth / 4f),
topEdge + Vector3.down * topMargin + Vector3.right * (targetWidth / 4f));
// Bottom margin (distance from bottom edge)
Gizmos.DrawLine(
bottomEdge + Vector3.up * bottomMargin + Vector3.left * (targetWidth / 4f),
bottomEdge + Vector3.up * bottomMargin + Vector3.right * (targetWidth / 4f));
}
if (showHorizontalMargins)
{
// Left margin (distance from left edge)
Gizmos.DrawLine(
leftEdge + Vector3.right * leftMargin + Vector3.up * (halfHeight / 2f),
leftEdge + Vector3.right * leftMargin + Vector3.down * (halfHeight / 2f));
// Right margin (distance from right edge)
Gizmos.DrawLine(
rightEdge + Vector3.left * rightMargin + Vector3.up * (halfHeight / 2f),
rightEdge + Vector3.left * rightMargin + Vector3.down * (halfHeight / 2f));
}
// Restore original color
Gizmos.color = originalColor;
}
#endif
}
}