- Home /
another android dpi question...
Ok so I'm a complete nook at unity.. but I'm a pretty good self teacher.. I've done almost all the main popular tutorials out there.. and I pride myself at trying to find the answer for myself before running to some community for guidance.. But I've hit a brick wall with an android game I'm developing... See I'm confident with almost everything I'm doing.. but I can't get my gui to work properly on my device.. in unty and whilst using the unity remote it all looks fine... but after I build, all my gui's are shrunken and the buttons are almost impossible to use in Thier miniature state... So I've had tried to figure this out myself... it seems to me that it could be an issue with the resolution or with dpi.. most likely both.. my test device is a galaxy s3 ... I've tried heaps of things and played around with screen.dpi and screen.resolution.. tried changing them from gui textures... tried experimentin with screen.height/width etc... and still can't get it to work.. arrggh! !! This is the one thing holding me back and I can't seem to find a straight "noob friendly" answer out there..
I found a couple of things that looked hopeful.. including this answer that required plugins
http://answers.unity3d.com/questions/161281/is-there-a-way-to-android-physical-screen-size.html
But I can't figure out how to use this to solve my issues. I made this script put it in my plugins folder .. now what's next.. does the game call it by itself.. or do I have to call it somehow... unfortunate there isn't to many helpfully docs out There that tells a noob how to use plugins.. so that failed for me. Also found this really hopefull script here
https://gist.github.com/2018687
But again.. nothing to help me figure how to incorporate this to solve my troubles....
So now I find myself raging and throwing myself at the mercy of this community please help me understand what I need to do to fit my gui to any dpi or resolution Thank you for reading my rant.. and I really hope u can help me and the other hundred thousand odd noobs who have found themselves I this situation
Answer by Dharke · Jan 25, 2013 at 01:39 PM
okie dokie.. nice to know the community is as ignorant as i am.. i mean 8 days later and no answers.. even though i would assume there are many experienced people here who have encountered this problem and found a way around it..
so i guess i'll have to answer my own question for the other noobs out there that are scratching their heads over the android GUI conundrums..
took me a bit but i gathered all the info i had and messed around with it all.. creating a hybrid of all solutions and scripts on the internet.. and strangely enough i got my hybrid to work... so heres what i did...
1) create a java script in your project view.. name it GUIScaler..
2) open the script and get ready to edit it... copy and paste the following script...
public static class GUIScaler {
var BASE_SCALE : float = 150.0f; // this value is the base DPI.. the dpi of the galaxy s3 is 306 var initialized : boolean = false; var scaling : boolean = false; var guiScale : Vector3 = Vector3.one; var restoreMatrix : Matrix4x4 = Matrix4x4.identity; // i found the average dpi of most android devices around 200dpi.. but to me 150 dpi got my gui looking really well scaled //this will initialise the class.. public function Initialize(scale : float) // the scale will be 0 on platforms that have unknown dpi { if (initialized) return; initialized = true; if (scale == 0 || scale < 1.1f) return; // if the scale is less than 10% don't bother, it just makes gui look like poop... guiScale.Set(scale, scale, scale); scaling = true; } // this is the same as above just with no value being passsed through.. this will detect the dpi of the device and adjust accordingly public function Initialize() { Initialize(Screen.dpi / BASE_SCALE); } // this begins the scaling.. call this on the first line inside your "function ONGUI()" ... public function Begin() { if (!initialized) Initialize(); if (!scaling) return; restoreMatrix = GUI.matrix; GUI.matrix = GUI.matrix * Matrix4x4.Scale(guiScale); } // this ends the scaling.. use this after you script your GUI's.. // for some reason this needs to be used.. creates all kinds of problems if not.. public function End() { if (!scaling) return; GUI.matrix = restoreMatrix; } }
ok now you've done that.. heres how you use it..
3) when you are writing the script that calls your GUI... call GUIScaler.Initialize()inside your function start() or function awake()..
4) then when you create the function ONGUI() make sure the first thing you call is GUIScaler.Begin
5) and finally close it all off with GUIScaler.End()...
IMPORTANT.. You must use GUIScaler.End because it will mess you up.. before i figured this out i had my computer crash.. unity freeze and once my project folder became corrupted and refused to open.. so make sure you do this ...
here is an example incase my noob lingo was too confusing or inaccurate..
#pragma strict
function Start ()
{
GUIScaler.Initialize();
}
function OnGUI ()
{
GUIScaler.Begin();
GUI.Label (Rect (10, 10, 150, 40), "Hello World");
GUI.Label (Rect (435, 10 , 100, 40), "why does the world never say hi back?? " );
GUIScaler.End();
}
OK so that's it.. test it out.. play with it.. go to bed with it.. it worked good for me.. so heres hoping it does the job for you..
and please i love constructive critisism.. so if your one of the experienced guys and you can see a flaw in my perfect script please let me know.. even after this awesome feat i still feel real noob lost in a world of mathf's and enumerations(whateva they are...)
There are several possible reasons for a question to remain unanswered - nobody knowing the answer is one of them, for sure, but sometimes the question is quickly buried by new ones before someone that knows the answer ever see it - an useful trick in these cases is to bump the question up by adding a comment like "No one knows the answer?".
You should accept your own answer (check button behind thumb buttons), so that other users would know this answer solves their problems and thankfully upvote it!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with acclerometer 0 Answers
Problem with jcar script for android mobile 1 Answer
Android Screen Resolution (Joysticks) help. 2 Answers
Android project debugging.. 1 Answer