aspect ratio solution

This commit is contained in:
Jean Lamarre 2022-10-31 09:23:07 -04:00
parent 3c93a1da68
commit bd097bed3d
8 changed files with 111 additions and 0 deletions

View File

@ -1881,6 +1881,7 @@ GameObject:
- component: {fileID: 1530935836}
- component: {fileID: 1530935839}
- component: {fileID: 1530935840}
- component: {fileID: 1530935841}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -2021,6 +2022,19 @@ MonoBehaviour:
m_CameraActivatedEvent:
m_PersistentCalls:
m_Calls: []
--- !u!114 &1530935841
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1530935835}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b3c79a3620af0f143ab3d5736c2ab534, type: 3}
m_Name:
m_EditorClassIdentifier:
targetAspect: {x: 4, y: 3}
--- !u!1001 &1650059524
PrefabInstance:
m_ObjectHideFlags: 0

View File

@ -585,6 +585,7 @@ GameObject:
- component: {fileID: 212203381}
- component: {fileID: 212203380}
- component: {fileID: 212203383}
- component: {fileID: 212203384}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -691,6 +692,19 @@ MonoBehaviour:
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2
--- !u!114 &212203384
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 212203379}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b3c79a3620af0f143ab3d5736c2ab534, type: 3}
m_Name:
m_EditorClassIdentifier:
targetAspect: {x: 4, y: 3}
--- !u!1001 &221153709
PrefabInstance:
m_ObjectHideFlags: 0

View File

@ -30617,6 +30617,7 @@ GameObject:
- component: {fileID: 1530935836}
- component: {fileID: 1530935839}
- component: {fileID: 1530935840}
- component: {fileID: 1530935841}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -30757,6 +30758,19 @@ MonoBehaviour:
m_CameraActivatedEvent:
m_PersistentCalls:
m_Calls: []
--- !u!114 &1530935841
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1530935835}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b3c79a3620af0f143ab3d5736c2ab534, type: 3}
m_Name:
m_EditorClassIdentifier:
targetAspect: {x: 16, y: 9}
--- !u!1 &1538549526
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,42 @@
using UnityEngine;
/**
https://gamedev.stackexchange.com/questions/144575/how-to-force-keep-the-aspect-ratio-and-specific-resolution-without-stretching-th
*/
[RequireComponent(typeof(Camera))]
public class CameraCrop : MonoBehaviour {
// Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
public Vector2 targetAspect = new Vector2(16, 9);
Camera _camera;
void Start () {
_camera = GetComponent<Camera>();
UpdateCrop();
}
// Call this method if your window size or target aspect change.
public void UpdateCrop() {
// Determine ratios of screen/window & target, respectively.
float screenRatio = Screen.width / (float)Screen.height;
float targetRatio = targetAspect.x / targetAspect.y;
if(Mathf.Approximately(screenRatio, targetRatio)) {
// Screen or window is the target aspect ratio: use the whole area.
_camera.rect = new Rect(0, 0, 1, 1);
}
else if(screenRatio > targetRatio) {
// Screen or window is wider than the target: pillarbox.
float normalizedWidth = targetRatio / screenRatio;
float barThickness = (1f - normalizedWidth)/2f;
_camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
}
else {
// Screen or window is narrower than the target: letterbox.
float normalizedHeight = screenRatio / targetRatio;
float barThickness = (1f - normalizedHeight) / 2f;
_camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3c79a3620af0f143ab3d5736c2ab534
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 20b5207e544e287489edeb6c3943bd45
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1 @@
{"Keys":["com.unity.services.core.cloud-environment","com.unity.services.core.version"],"Values":[{"m_Value":"production","m_IsReadOnly":true},{"m_Value":"1.4.2","m_IsReadOnly":true}]}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 528c76750e583c144b167f8cfa20183f
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: