GUI Matrix vs Canvas vs Alternate 2D GUI Multi Resolution Support
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.
I'm also considering having a list of button locations (x, y, length, width), generally including individual ones for every interface. Then I can call out different placements based on different aspect ratios, of which I would detect on launch or resolution change. I would still use the matrix to scale it to the actual screen size.
Answer by Trlgger · Oct 02, 2015 at 09:52 PM
If your not completely attached to GUI scripting you can use uGUI, the new Unity4.6-5 GUI system also called Canvas. It will scale with the screen resolution depending on the settings you choose and it's very easy to get the hang of.
Here is a quick tutorial supplied by Unity to get you started: https://www.youtube.com/watch?v=OD-p1eMsyrU
Interesting, I was preferring scripting it, but this looks like a generally good approach, I'll look into it.
Your answer
Follow this Question
Related Questions
Set Game Resolution For Every Screen 0 Answers
How to create scene with multiple panels? 0 Answers
How do I change the orientation of my game to portrait? 0 Answers
Standalone game's built version is zoomed in at most resolutions. Looks fine in game view. 1 Answer
Resolution messed up after switching between fullscreen modes. 1 Answer