Using Pixel Perfect Camera with various resolutions and applying the custom camera scale.
I have found a lot of different information about using Pixel Perfect Camera in Unity, but I realize that there are many possible problems and constraints with using it. So I’ll ask a question for my individual case.
Problem and setup
I’ve seen a lot of similar questions, but after trying to implement this in different ways, I saw that it still carries a lot of limitations.
- I have characters sprite atlases where each sprite is 128x128px, also I have other atlases for smaller/bigger world objects (64x64px & 256x256px accordingly).
- I set the PPU to 128 for all my sprites and turned off compression.
- In the sprite editor I set all pivots in pixel units. Then I added pixel perfect camera to the main camera (I do not use Cinemachine virtual camera).
- Pixel Perfect Camera Settings :
PPU : 128
Ref Resolution : X = 1920; Y = 1080
Crop Frame : None
Grid Snapping : Upscale Render Texture
So my pixel perfect camera works correctly except for the main problem - the camera zoom.
My characters and objects are too close to the camera, but I do not want to change their scales or sprite sizes. I also tried to set other PPU values but there are the same result or other problems appear.
I also tried to implement the pixel perfect camera logic without using the build in component itself, but there is the player jittery diagonal movement appears (I know that this is the correct behaviour but I do not want that).
So my final solution was to mannualy update Pixel Perfect Camera → Reference resolution using a script.
I noticed that when I set this settings in the PPC :
PPU : 128
Ref Resolution : X = 3840; Y = 2160
Crop Frame : WindowBox
Grid Snapping : Upscale Render Texture
Everything looks as I want It to (in 1920x1080 resolution) in 3840x2160 it is zoomed in again, so there is a certain pattern here, if I set the Reference Resolution to 2 times (or another value for scale correction) the screen size through the script, I get the desired camera distance.
Script :
[RequireComponent(typeof(PixelPerfectCamera))]
public class AutoCorrectResolution : MonoBehaviour
{
private PixelPerfectCamera _pixelPerfectCamera;
private int _lastScreenWidth;
private int _lastScreenHeight;
private void Awake()
{
_pixelPerfectCamera = GetComponent<PixelPerfectCamera>();
UpdateReferenceResolution();
}
private void Update()
{
TryUpdateReferenceResolution();
}
private void TryUpdateReferenceResolution()
{
if ((Screen.width != _lastScreenWidth) || (Screen.height != _lastScreenHeight))
{
UpdateReferenceResolution();
}
}
private void UpdateReferenceResolution()
{
_pixelPerfectCamera.refResolutionX = Screen.width * 2;
_pixelPerfectCamera.refResolutionY = Screen.height * 2;
_lastScreenWidth = Screen.width;
_lastScreenHeight = Screen.height;
}
}
Main question according to the problem
Is my solution acceptable in this situation, or is it too much of a stretch and it is worth doing something different? I have tried many different solutions and only this approach has given the required result, maybe I’m missing something?
P.s. I apologize if I have described something wrong or not mentioned, it is my first time working with Pixel Perfect and I’m asking a question on the forum, so I hope it’s okay.