- Home /
Android screen size script not working
I found this script on the internet, that should make the game fit all size phones. But it does not work! Maybe i missed something and thats why im asking here.
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);
}
}
Yeah sorry about not writing that. What i overall want is a script that can make my game fit in any android phone. And in that case i found this script. But it doesen't do any difference. When i change the resolution of the game, the Gui texts isnt where they should be. And what i found this script for is to make all texts and object stay where they are even though the resolution has changed. But the script does not do anything. If you have any solutions please tell me :-)!
Answer by zharik86 · Feb 17, 2014 at 06:51 PM
You use two types of a conclusion of the text. TextMesh in fact is the three-dimensional text. Most likely you should use GuiText. Then you should scale your GuiText and GUITexture correctly. I will show on one element, the rest becomes similarly(write on CSharp):
float origW = 640.0f;
float origH = 320.0f;
void Start() {
float scalex = (float) (Screen.width) / origW; //your scale x
float scaley = (float) (Screen.height) / origH; //your scale y
GUIText myText = Gameobject.Find("Mytext"); //find your element
Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
int origSizeText = myText.fontSize;
myText.pixelOffset = new Vector2(pixOff.x*scalex, pixOff.y*scaley);
myText.fontSize = origSizeText * scalex;
}
And texture.localScale don't scale your texture. For GUITexture you use pixelInsert, see docs Unity, or ask me. I hope that it will help you.
Well this doesn't help that much since its a csharp script, and i use javascript. And i don't have enough experience to translate this script to java .. :p And i don't really understand how i need to attach this script
@NinjaRubberBand Ok, I'm translating for you:
var origW: float = 640;
var origH: float = 320;
function Start() {
var scalex: float = Screen.width / origW; //your scale x
var scaley: float = Screen.height / origH; //your scale y
var texts: GUIText[] = FindObjectsOfType(GUIText) as GUIText[];
for(var myText: GUIText in texts) { //find your element
var pixOff: Vector2 = myText.pixelOffset; //your pixel offset on screen
var origSizeText: int = myText.fontSize;
myText.pixelOffset = new Vector2(pixOff.x*scalex, pixOff.y*scaley);
myText.fontSize = origSizeText * scalex;
}
}
That's all. Attach this script to any object, for example, main camera. Simple:) And check this answer, if I help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Android GUI resize problem 1 Answer
problem with gui??? 0 Answers
Resolution for assets on Android 1 Answer
Resolution Multiplier on Android 1 Answer
GUI Overlay using mobile camera 1 Answer