Add horizontal camera scaling for devices
This commit is contained in:
@@ -19,6 +19,9 @@ namespace AppleHillsCamera
|
||||
|
||||
[Tooltip("Whether to adjust the camera automatically when the screen size changes")]
|
||||
public bool adjustOnScreenResize = true;
|
||||
|
||||
[Tooltip("Enable debug logging")]
|
||||
public bool debugLogging;
|
||||
|
||||
// Event that other components can subscribe to when camera is adjusted
|
||||
public event Action OnCameraAdjusted;
|
||||
@@ -71,7 +74,7 @@ namespace AppleHillsCamera
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manually trigger camera adjustment to match the reference width.
|
||||
/// Manually trigger camera adjustment to match the reference width or height.
|
||||
/// </summary>
|
||||
public void AdjustCamera()
|
||||
{
|
||||
@@ -81,13 +84,34 @@ namespace AppleHillsCamera
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate the orthographic size based on the target width and screen aspect ratio
|
||||
float targetWidth = referenceMarker.targetWidth;
|
||||
float screenAspect = (float)Screen.height / Screen.width;
|
||||
float orthoSize;
|
||||
|
||||
// Orthographic size is half the height, so we calculate:
|
||||
// orthoSize = (targetWidth / 2) * (screenHeight / screenWidth)
|
||||
float orthoSize = (targetWidth / 2f) * screenAspect;
|
||||
if (referenceMarker.scalingMode == ScreenReferenceMarker.ScalingMode.Width)
|
||||
{
|
||||
// Calculate the orthographic size based on the target width and screen aspect ratio
|
||||
float targetWidth = referenceMarker.targetWidth;
|
||||
float screenAspect = (float)Screen.height / Screen.width;
|
||||
|
||||
// Orthographic size is half the height, so we calculate:
|
||||
// orthoSize = (targetWidth / 2) * (screenHeight / screenWidth)
|
||||
orthoSize = (targetWidth / 2f) * screenAspect;
|
||||
|
||||
if (debugLogging)
|
||||
{
|
||||
Logging.Debug($"CameraScreenAdapter: Scaling by Width - targetWidth={targetWidth:F2}, aspect={screenAspect:F2}, orthoSize={orthoSize:F2}");
|
||||
}
|
||||
}
|
||||
else // ScalingMode.Height
|
||||
{
|
||||
// When scaling by height, the orthographic size directly matches half the target height
|
||||
float targetHeight = referenceMarker.targetHeight;
|
||||
orthoSize = targetHeight / 2f;
|
||||
|
||||
if (debugLogging)
|
||||
{
|
||||
Logging.Debug($"CameraScreenAdapter: Scaling by Height - targetHeight={targetHeight:F2}, orthoSize={orthoSize:F2}");
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the calculated size to the camera
|
||||
if (_usingCinemachine)
|
||||
@@ -96,7 +120,11 @@ namespace AppleHillsCamera
|
||||
var lens = _virtualCamera.Lens;
|
||||
lens.OrthographicSize = orthoSize;
|
||||
_virtualCamera.Lens = lens;
|
||||
Logging.Debug($"Cinemachine Camera adapted: Width={targetWidth}, Aspect={screenAspect:F2}, OrthoSize={orthoSize:F2}");
|
||||
|
||||
if (debugLogging)
|
||||
{
|
||||
Logging.Debug($"Cinemachine Camera adapted: OrthoSize={orthoSize:F2}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -104,7 +132,11 @@ namespace AppleHillsCamera
|
||||
if (_regularCamera.orthographic)
|
||||
{
|
||||
_regularCamera.orthographicSize = orthoSize;
|
||||
Logging.Debug($"Camera adapted: Width={targetWidth}, Aspect={screenAspect:F2}, OrthoSize={orthoSize:F2}");
|
||||
|
||||
if (debugLogging)
|
||||
{
|
||||
Logging.Debug($"Camera adapted: OrthoSize={orthoSize:F2}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -8,11 +8,24 @@ namespace AppleHillsCamera
|
||||
/// </summary>
|
||||
public class ScreenReferenceMarker : MonoBehaviour
|
||||
{
|
||||
[Header("Horizontal Reference")]
|
||||
[Tooltip("The target width that should match the screen width")]
|
||||
public float targetWidth = 10f;
|
||||
public enum ScalingMode
|
||||
{
|
||||
Width,
|
||||
Height
|
||||
}
|
||||
|
||||
[Header("Vertical References")]
|
||||
[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;
|
||||
|
||||
@@ -49,20 +62,8 @@ namespace AppleHillsCamera
|
||||
|
||||
Vector3 position = transform.position;
|
||||
|
||||
// Draw the width reference
|
||||
Vector3 left = position + Vector3.left * (targetWidth / 2f);
|
||||
Vector3 right = position + Vector3.right * (targetWidth / 2f);
|
||||
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);
|
||||
|
||||
// Calculate visual screen edges based on actual camera viewport
|
||||
float halfWidth = targetWidth / 2f;
|
||||
// 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
|
||||
@@ -71,14 +72,37 @@ namespace AppleHillsCamera
|
||||
Camera mainCamera = Camera.main;
|
||||
if (mainCamera != null && mainCamera.orthographic)
|
||||
{
|
||||
// Use the main camera's actual orthographic size for the height
|
||||
halfHeight = mainCamera.orthographicSize;
|
||||
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;
|
||||
halfHeight = halfWidth * gameViewAspect;
|
||||
|
||||
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;
|
||||
@@ -86,10 +110,49 @@ namespace AppleHillsCamera
|
||||
{
|
||||
// Use the scene view camera's aspect ratio instead
|
||||
float sceneAspect = sceneView.camera.pixelHeight / (float)sceneView.camera.pixelWidth;
|
||||
halfHeight = halfWidth * sceneAspect;
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user