-
-
Save noio/1705f5255e64484aee19 to your computer and use it in GitHub Desktop.
Screen Zooming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ScreenManager : MonoBehaviour | |
{ | |
public const float pixelsPerUnit = 32; | |
public static Vector2 renderSize = new Vector2(512,256) / pixelsPerUnit; | |
public float minHorizontalUnits = 12; | |
public float verticalUnits = 4; | |
public Transform _screenQuad; | |
public Mesh _screenQuadMesh; | |
bool _dirty = true; | |
Resolution _resolution; | |
void Awake() | |
{ | |
// Force Unity to switch to fullscreen regardless of stored user preferences | |
_resolution = Screen.currentResolution; | |
Screen.SetResolution(_resolution.width, _resolution.height, true); | |
_screenQuad = Require.ChildWithTag(Tags.ScreenQuad, this); | |
_screenQuadMesh = Require.Component<MeshFilter>(_screenQuad).mesh; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (_dirty) | |
{ | |
// TODO Maybe we need to do this in a kind of "OnResize" or just at game start.. | |
float heightPortion = 0.5f * verticalUnits / renderSize.y; | |
_screenQuad.localScale = new Vector3(renderSize.x, verticalUnits, 1); | |
Vector2[] uvs = _screenQuadMesh.uv; | |
uvs[0] = new Vector2(0, 0.5f - heightPortion); | |
uvs[1] = new Vector2(1, 0.5f + heightPortion); | |
uvs[2] = new Vector2(1, 0.5f - heightPortion); | |
uvs[3] = new Vector2(0, 0.5f + heightPortion); | |
_screenQuadMesh.uv = uvs; | |
// Always fit the full height of the game. | |
float maxZoomHeight = Screen.height / (verticalUnits * pixelsPerUnit); | |
// Show at least some portion of the width of the game. | |
float maxZoomWidth = Screen.width / (minHorizontalUnits * pixelsPerUnit); | |
float pixzoom = Mathf.Min(maxZoomHeight, maxZoomWidth); | |
if (pixzoom > 1 && pixzoom < 5) | |
{ | |
pixzoom = Mathf.Floor(pixzoom); | |
} | |
// zoom = 1; | |
// this.camera.orthographicSize = screenDisplay.transform.localScale.y / 2 * zoom; | |
this.GetComponent<Camera>().orthographicSize = Screen.height / pixelsPerUnit / 2 / pixzoom; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment