I’m trying to build a gui but I’m stuck on making a 3d text object or gui texture fit the screen resolution. I found this:
var originalWidth = 640.0; // define here the original resolution
var originalHeight = 400.0; // you used to create the GUI contents
private var scale: Vector3;
function OnGUI(){
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// draw your GUI controls here:
GUI.Box(Rect(10,10,200,50), "Box");
GUI.Button(Rect(400,180,230,50), "Button");
//...
// restore matrix before returning
GUI.matrix = svMat; // restore matrix
}
But I don’t know how to use it for non scripted objects.
What you could do is change the scale of the GUI GameObjects according to your resolution compared to the original resolution. Create an empty GameObject and attach the following script, that will find all 3d text objects (TextMesh) and all gui textures (GuiTextures), and change their scale according to the current resolution:
var originalWidth = 640.0; // define here the original resolution
var originalHeight = 400.0; // you used to create the GUI contents
function Start() {
// calculate scales
var scaleX : float = Screen.width / originalWidth;
var scaleY : float = Screen.height / originalHeight;
// find all texts and change the scale
var texts : TextMesh[] = FindObjectsOfType(TextMesh) as TextMesh[];
for (var text : TextMesh in texts) {
text.transform.localScale = new Vector3(text.transform.localScale.x * scaleX, text.transform.localScale.y * scaleY, text.transform.localScale.z);
}
// find all gui textures and change the scale
var textures : GuiTexture[] = FindObjectsOfType(GuiTexture) as GuiTexture[];
for (var texture : GuiTexture in textures) {
texture.transform.localScale = new Vector3(texture.transform.localScale.x * scaleX, texture.transform.localScale.y * scaleY, texture.transform.localScale.z);
}
}