I’m embarking on a 2d project that will need to run on a number of aspect ratios, mainly popular phones/tablets and PC/Mac. The project is an RPG, so there will be a number of menus, dialog, battle, shops, etc. Recently I completed a project with applying GUI scale to simply scale the GUI to the screen size. Basically code like this:
public class example: MonoBehaviour{
float originalWidth = 1920;
float originalHeight = 1080;
Vector3 scale;
public GUISkin guiSkin;
void OnGUI(){
GUI.depth = 1;
GUI.skin = guiSkin;
scale.x = (float)Screen.width/originalWidth;
scale.y = (float)Screen.height/originalHeight;
scale.z = 1;
Matrix4x4 svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector3(0,0,0), Quaternion.identity, scale);
//all my GUI code goes here
GUI.matrix = svMat;
}
}
This works Good. However, I am aware it will distort GUI to some extent on different aspect ratios. I have looked into Canvas and anchoring a few times, however I dislike creating my GUI in word space, and prefer to code everything, of which this approach doesn’t seem particular conducive.
So, my question:
Is there a good best practice for making clean 2d GUIs that support multiple resolutions, perhaps a tutorial, and or some code snips would be helpful. This is a general question, but I appreciate any pointers.