Problem scalling UI Buttons Generated on Runtime
Attached is the screenshot from mobile, Setting screenshot from Unity and the code snippet to generate the UI button.
My problem is of scaling the UI generated buttons according to the resolution of different devices, I am breaking my head with every wall since 7 hours but no good, i found different tutorials but couldn't solve my specific problem.
It is shown in the screenshot of unity the size showing on 16:10 ratio is quite large that is going out off screen but i am unable to get why why why it is showing so small on the device.
Please note that my app is live and i am looking for an urgent response.
using UnityEngine;
using System.Collections;
public class SkinImplementer : MonoBehaviour {
public GUISkin MainSkin = null;
public string buttonTitle = string.Empty;
public Texture2D playButtonImage = null;
public Texture2D LeaderboardButtonImage = null;
public Texture2D AchievementButtonImage = null;
public Texture2D CreditsButtonImage = null;
public Texture2D PlayAgainButtonImage = null;
void OnGUI(){
GUI.skin = MainSkin;
if (Application.loadedLevelName == "TapBallStart") {
if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 60, 300, 120), playButtonImage)) {
Application.LoadLevel ("TapBall");
}
} else if (Application.loadedLevelName == "TapBallGameOver") {
if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 60, 300, 120), PlayAgainButtonImage)) {
Application.LoadLevel ("TapBall");
}
}
if (GUI.Button(new Rect (Screen.width/2, Screen.height/2+60, 225, 225), LeaderboardButtonImage)) {
//show leaderboard script
Social.ShowLeaderboardUI();
}
if (GUI.Button(new Rect (Screen.width/2-225, Screen.height/2+60, 225, 225), AchievementButtonImage)) {
//show achievement script
Social.ShowAchievementsUI();
}
if (GUI.Button (new Rect (Screen.width / 2 -105, Screen.height / 2 + 285, 210, 120), CreditsButtonImage)) {
Application.LoadLevel ("Credit");
}
}
}
Answer by hexagonius · Feb 08, 2016 at 09:31 PM
The scale mode on your canvas scaler is wrong. use scale with screen size, set your reference pixel width and height (size you designed the sprites for) and the slider to how the UI should react on different aspects.
Thanks for your reply, I looked at the documentation sent that i read already and applied different option before putting up the question for support. i have changed it to "Scale with Screen Size" as guided all is going well "On SCREEN in UNITY" but the problem still persist for the menu in question that was generated by the c# code shared already.
Adjacent is the screenshot from mobile and guess what, c# generated menu is in place and nothing changed to it but other things like game/company and social logos are disappeared those were available in game view of unity on all aspect ratios including the one used by my mobile.
Your answer
![](https://koobas.hobune.stream/wayback/20220612063132im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get value in button OnClick() 1 Answer
Possible to long/short press a UI button - Phone device 0 Answers
How do I control my car with UI buttons? 0 Answers
UI Buttons Will Not Work After Building,UI Buttons do not work 0 Answers
UI Resizing out of screen 0 Answers