Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created August 14, 2023 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/6e689c7df763c02adb7526b14dad3852 to your computer and use it in GitHub Desktop.
Save kurtdekker/6e689c7df763c02adb7526b14dad3852 to your computer and use it in GitHub Desktop.
Unity3D screen metrics utility for 2D camera use
using UnityEngine;
// @kurtdekker
//
// Ultra simple script to give you the "live edges" of your screen.
// This updates for any screen orientation, dimensions or camera.
//
// Presumes:
// - orthographic Camera pointed +Z
// - returns values in the Z == 0 plane.
//
// To use: NEVER PLACE THIS IN YOUR SCENE!!! DO NOT DRAG IN!!!
//
// Simply access it with these static Vector3 properties:
// - ScreenMetrics2D.LowerLeft
// - ScreenMetrics2D.UpperRight
//
// There are also TopEdge, LeftEdge, RightEdge and BottomEdge properties
//
// If it offends you that this runs every frame, by all
// means feel free to cache this data and only run the
// MyUpdate when some input property changes. :)
//
// Enjoy!
public class ScreenMetrics2D : MonoBehaviour
{
public static Vector3 LowerLeft
{
get
{
if (!_I) I.ToString();
return _LowerLeft;
}
}
public static Vector3 UpperRight
{
get
{
if (!_I) I.ToString();
return _UpperRight;
}
}
// these are a bunch of quality of life improvers.
public static float TopEdge { get { return UpperRight.y; }}
public static float BottomEdge { get { return LowerLeft.y; }}
public static float LeftEdge { get { return LowerLeft.x; }}
public static float RightEdge { get { return UpperRight.x; }}
//--------------------------------------------------------------
// all business logic below... not necessary to use this script
// cached state
static Vector3 _LowerLeft;
static Vector3 _UpperRight;
static ScreenMetrics2D _I;
static ScreenMetrics2D I
{
get
{
if (!_I)
{
var go = new GameObject( "ScreenMetrics.Instance.");
_I = go.AddComponent<ScreenMetrics2D>();
_I.MyUpdate();
}
return _I;
}
}
Camera targetCamera;
void MyUpdate()
{
if (!targetCamera)
{
targetCamera = Camera.main;
}
{
Vector3 position = targetCamera.ViewportToWorldPoint( new Vector2( 0, 0));
position.z = 0;
_LowerLeft = position;
}
{
Vector3 position = targetCamera.ViewportToWorldPoint( new Vector2( 1, 1));
position.z = 0;
_UpperRight = position;
}
}
void Update()
{
MyUpdate();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker - demonstrates the ScreenMetrics2D class
public class ScreenMetrics2DTester : MonoBehaviour
{
GameObject LowerLeftMarker;
GameObject UpperRightMarker;
void Start()
{
// make some stuff to see in the corners
LowerLeftMarker = GameObject.CreatePrimitive( PrimitiveType.Capsule);
UpperRightMarker = GameObject.CreatePrimitive( PrimitiveType.Cube);
}
void Update ()
{
LowerLeftMarker.transform.position = ScreenMetrics2D.LowerLeft;
UpperRightMarker.transform.position = ScreenMetrics2D.UpperRight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment