- Home /
Android Screen Resolution (Joysticks) help.
Hey guys, I've tried every single help information on getting the GUI's to stay scaled for every screen resolution, but it just doesn't work!
I 0 out all the Pixel Insets, change the scale to my liking and position the Joysticks where I want.
Well, what happens is, if I 0 out the Width and Height (In Pixel Inset) I can no longer use the Joysticks.
Well that problems number 1.
Problem number 2 is, they are never scaled with any droid I use.
Figured it might have been my G3 with 4K Resolution, well nope. I tried with my Ellipsis 7 Tablet at 720P and it's still not right.
Both Joysticks become super small like they are 5 x 5 in pixels and only take the first half of the screen no matter which device I use.
Please help me guys!
Oh yeah, forgot to mention (It stays perfectly aligned) IN (GA$$anonymous$$E VIEW) In the editor, it's only when I actually deploy to phone.
I try changing to all the resolutions in Game View and it works. It's only on Phone or Tablet where it messes up.
Are you use for Joysticks GUITexture or draw it in function OnGUI()?
I'm using the Joysticks GUITexture that came with the Standard Assets ($$anonymous$$obile) package.
Answer by zharik86 · Nov 20, 2014 at 08:37 AM
Ok, for different resolutions of the android (or other screens), it is necessary to scale your GUITextures and GUIText. I will write a small explaining script (I write on CSharp):
//define here the original resolution
public float origW = 1280;
public float origH = 720;
void Start() {
float scaleX = (float)(Screen.width) / origW; //your scale x
float scaleY = (float)(Screen.height) / origH; //your scale y
//Find all GUIText object on your scene
//Update 1: for C# add "typeof"
GUIText[] texts = FindObjectsOfType(typeof(GUIText)) as GUIText[];
foreach(GUIText myText in texts) { //find your element of text
Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
int origSizeText = myText.fontSize;
myText.pixelOffset = new Vector2(pixOff.x*scaleX, pixOff.y*scaleY); //new position
myText.fontSize = (int)(origSizeText * scaleX); //new size font
}
//Find all GUITexture on your scene
//Update 1: for C# add "typeof"
GUITexture[] textures = FindObjectsOfType(typeof(GUITexture)) as GUITexture[];
foreach(GUITexture myTexture in textures) { //find your element of texture
Rect pixIns = myTexture.pixelInset; //your dimention of texture
//Change size pixIns for our screen
pixIns.x = pixIns.x * scaleX;
pixIns.y = pixIns.y * scaley;
pixIns.width = pixIns.width * scaleX;
pixIns.height = pixIns.height * scaleY;
//Sets new rectangle for our texture
myTexture.pixelInset = pixIns;
}
}
That's all. Attach this script to any object, for example, Main Camera. I hope that it will help you.
I'm getting a few errors
First one -
Assets/_Imported/_Scripts/_GUI/_GuiFixedResolution.cs(13,35): error CS1502: The best overloaded method match for `UnityEngine.Object.FindObjectsOfType(System.Type)' has some invalid arguments
(SECOND ONE)
Assets/_Imported/_Scripts/_GUI/_GuiFixedResolution.cs(13,35): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'
(THIRD ONE)
Assets/_Imported/_Scripts/_GUI/_GuiFixedResolution.cs(21,59): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
(FORTH ONE)
Assets/_Imported/_Scripts/_GUI/_GuiFixedResolution.cs(21,41): error CS1502: The best overloaded method match for `UnityEngine.Object.FindObjectsOfType(System.Type)' has some invalid arguments
(FIFTH ONE)
Assets/_Imported/_Scripts/_GUI/_GuiFixedResolution.cs(21,41): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'
Can you help guide me on how to fix this? I'm using 4.6 if that helps.
Doesn't look any different LOL. I mean it doesn't appear updated lol.
(I appreciate you trying to help me though!)
I at first wrote the comment, and then update of my answer. Simply for the FindObjectsOfType() it is necessary to add "typeof" inside. See my answer. Below comments with name "Update 1:".
Answer by N1warhead · Nov 20, 2014 at 10:57 AM
Not sure if you're script helped getting it to work, but I found this one and its working I APPRECIATE YOUR HELP THOUGH BUDDY!
But here is the code I found that works.
public Vector2 scaleOnRatio1 = new Vector2(0.1f, 0.1f);
private Transform myTrans;
private float widthHeightRatio;
// Use this for initialization
void Start () {
myTrans = transform;
SetScale();
}
// Update is called once per frame
void SetScale()
{
//aspect ratio
widthHeightRatio = (float)Screen.width/Screen.height;
//Apply the scale. We only calculate y since our aspect ratio is x (width) authoritative: width/height (x/y)
myTrans.localScale = new Vector3 (scaleOnRatio1.x, widthHeightRatio * scaleOnRatio1.y, 1);
}
}