Skip to content

Instantly share code, notes, and snippets.

@Fenikkel
Last active May 7, 2024 11:16
Show Gist options
  • Save Fenikkel/91043b42f965fbb10cd346afb4fa090b to your computer and use it in GitHub Desktop.
Save Fenikkel/91043b42f965fbb10cd346afb4fa090b to your computer and use it in GitHub Desktop.
Configuration for a 2D project with SpriteRenders

Orthographic camera configurator for 2D projects.

Config window Screen sizes

 

Notes

This window/tool helps you to configure your camera to fit correctly the SpriteRenderer on your game view.

 

Usage

  1. Open the Orthographic Camera Fitter window:
  • Tools -> Fenikkel -> Orthographic Camera Fitter
  1. Fill the fields:
  • Reference height: It should be the resolution of the height of the artist's project (.psd, .ai, etc) that never changes between the different resolutions of the different aspect ratios.
  • Pixels per unit: How many pixels in the sprite correspond to one unit in the world. You can see the value of your images in the import settings. By default is 100. You should have the same value in ALL your sprites to let this configuration work correctly.
  • Camera: Camera component to use for the setup. Usually your Main Camera.
  1. Click on "Configure camera": This will change the projection and size fields of the selected camera component.

 

It's important to have the script inside of a folder called "Editor"

 

Example

A 2D artist is working on Photoshop project with a canvas resolution of 2048 x 1536.
Open the Orthographic Camera Fitter window and set the "Pixels per unit" to the same as your sprites import settings configuration (by default 100) and set the "Reference height" to 1536.

 

Methodology

If you are doing a 2D project and you are gonna use SpriteRenderer, you probably want your sprites to have (by default) the same size as they have in the 2D artist project (photoshop, illustrator, etc).

To accomplish that, first of all, you need to import all your sprites with the same pixels per unit value in the import configuration. By default it's 100.

Then, you need to change the fields Projection and Size of your camera component to make your SpriteRenderer fit perfectly like the 2D artist project. The Projection must be set to Orthographic. The Size depends on the formula (Reference height / Pixels per unit) / 2. The variable "Reference height" it's the height resolution that the 2D artist was working with. And the variable "Pixels per unit" it's the same as your sprites import configuration.

Now your project is ready to maintain the same sprite size as the 2D artist designed. Also, you can now switch to any landscape resolution or aspect ratio and the game view will only be expanded or reduced from the sides. The height will remain perfectly adjusted.

 

Disclamer

This solution only works for landscape or squared games. If your game it's portrait, you probably want to use the PixelPerfectCamera component. This will have your orthographic camera size adapting to different resolutions but it will not respect the size of the Sprite between resolutions.

 

Compatibility

  • Any Unity version
  • Any pipeline (Build-in, URP, HDRP, etc)

 

Support

⭐ Star if you like it
❤️️ Follow me for more

using UnityEditor;
using UnityEngine;
public class OrthographicCamFitterWindow : EditorWindow
{
int _ReferenceHeight = 1080;
int _PixelsPerUnit = 100;
Camera _Camera;
string _IntroText = "<size=14><b>Notes</b></size>\n" +
"Pixel perfect the sprites provided by your 2D artist on the scene. \n" +
"It also mantains the game height on any resolution or aspect ratio (width will shrink or expand).\n\n" +
"<size=14><b>Setup</b></size>\n" +
"Set the reference height to the same as your 2D artist project.\n" +
"Set the pixels per unit the same as your sprites config (by default, Unity set to 100)";
string _FieldTip1 = "It should be the resolution of the height of the artist's project (.psd .ai) that never changes between the different resolutions of the different aspect ratios.";
string _FieldTip2 = "How many pixels in the sprite correspond to one unit in the world. You can see the value of your images in the import settings. By default is 100. You should have the same value in ALL your sprites to let this configuration work correctly.";
[MenuItem("Tools/Fenikkel/Orthographic Camera Fitter")]
public static void ShowWindow()
{
OrthographicCamFitterWindow window = EditorWindow.GetWindow<OrthographicCamFitterWindow>("Orthographic Camera Fitter", true);
window.position = new Rect(window.position.x, window.position.y, 400, 300);
}
private void OnEnable()
{
_Camera = Camera.main;
}
private void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField( _IntroText, new GUIStyle(EditorStyles.label) { richText = true, wordWrap = true });
EditorGUILayout.Space(20);
_ReferenceHeight = EditorGUILayout.IntField(new GUIContent("Reference height", _FieldTip1), _ReferenceHeight);
_PixelsPerUnit = EditorGUILayout.IntField(new GUIContent("Pixels per unit", _FieldTip2), _PixelsPerUnit);
_Camera = EditorGUILayout.ObjectField(new GUIContent("Camera", "Camera component to use for the setup. Usually it's your Main Camera."), _Camera, typeof(Camera), true) as Camera;
EditorGUILayout.Space();
if (GUILayout.Button("Configure main camera"))
{
ConfigCam(_Camera, _ReferenceHeight, _PixelsPerUnit);
this.Close();
}
}
private static void ConfigCam(Camera cam, int targetHeight = 1080, int pixelsPerUnit = 100)
{
if (cam == null)
{
Debug.LogError("No camera provided! Select the camera on the 'Orthographic Cam Fitter' window");
return;
}
cam.orthographic = true;
/* Float conversions are important */
cam.orthographicSize = ((float)targetHeight / (float)pixelsPerUnit) / 2.0f; // Simplification of -> (targetWidth / (float)pixelsPerUnit) / (2.0f * targetWidth / (float)targetHeight)
Debug.Log($"<b>{cam.name}</b> modifyed to orthographic projecton with an orthographic size of <b>{cam.orthographicSize}</b>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment