- Home /
Restricting ratio of a 2D Game
I'm working on a game that I want to be the same resolution as a Game Boy Advance game, being only 240x160. Now obviously that would be annoying to play on a computer, so I want to scale the game up. What I want is to limit the camera to seeing 240x160 pixels, but when the game runs, be able to scale up the screen while keeping the ratio (even if I can't keep the ratio perfect, that's okay as long as I can limit the camera but can still scale the game in a window). How would I go about limiting the camera? I'm pretty sure the scaling will be handled by Unity when the game is run.
Answer by mtdrume · Aug 02, 2015 at 08:16 PM
Add a black frame to the UI using solid panels to your desired size, ensure you setup anchors to the frame position, whatever you see inside the frame will be the same on every device and platform.
or
setting
Screen.SetResolution(240, 180, true);
not sure how it will look, true being fullscreen
This didn't seem to do anything, unfortunately. I put it in my camera script and nothing changed.
ensure you include this in Start() or Awake() or you are calling it. This is for desktop only as mobiles and tablets res generally cannot be changed.
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour {
void Start() {
Screen.SetResolution(240, 180, true);
}
}
I will do so but what file do you recommend I put it in? $$anonymous$$y player, camera, game manager, or make a new file and attach it to an object or the canvas? If I had more time today I would try them all out but today's a busy one lol
Any object that is present during startup. If you make a new script you could also go to Edit > Project Settings > Script Execution Order to ensure it is the first thing to happen. Also the res does not change in editor, must compile and run to see results. To do it in the editor click the drop down box in the preview that defaults to Free Aspect and then click the + button.
after a quick test, with full screen enabled, as expected it fills the screen, but at set res. windowed looks better but is tiny, mobile is terrible! If you want something that's smaller like a gameboy but still have quality i would still suggest making a frame.
Answer by rv0000s · Aug 06, 2015 at 06:08 PM
I found this script from my old project. Maybe this will work
what this script does is crop the camera rect. Just attach this script to the camera.
public float aspect_ratio_x=9f;
public float aspect_ratio_y=16f;
void Start() {
fixaspect ();
}
void fixaspect(){
float targetaspect = aspect_ratio_x / aspect_ratio_y;
float windowaspect = (float)Screen.width / (float)Screen.height;
float scaleheight = windowaspect / targetaspect;
if(scaleheight>=1f) {// add pillarbox
float scalewidth = 1.0f / scaleheight;
Rect rect = GetComponent<Camera>().rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
GetComponent<Camera>().rect = rect;
}
else {
GetComponent<Camera>().rect=new Rect(0,0,1,1);
}
//Camera.main.aspect = aspect_ratio_x / aspect_ratio_y;
}