Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

problem with UI not resizing in android

Discussion in 'Scripting' started by M0RT3ZA, Sep 13, 2016.

  1. M0RT3ZA

    M0RT3ZA

    Joined:
    Sep 13, 2016
    Posts:
    2
    this is the script for mainmenu

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mainmeu : MonoBehaviour {
    5.  
    6.     public GUISkin skin;
    7.     public Texture2D Logo;
    8.  
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     void Update () {
    14.    
    15.     }
    16.    
    17.     public void OnGUI(){
    18.         if(skin)
    19.         GUI.skin = skin;
    20.        
    21.         GUI.DrawTexture(new Rect(Screen.width/2 - Logo.width/2 , Screen.height/2 - 150,Logo.width,Logo.height),Logo);
    22.        
    23.         if(GUI.Button(new Rect(Screen.width/2 - 150,Screen.height/2 + 50,300,40),"Classic")){
    24.             Application.LoadLevel("Classic");
    25.         }
    26.         if(GUI.Button(new Rect(Screen.width/2 - 150,Screen.height/2 + 100,300,40),"Modern")){
    27.             Application.LoadLevel("Modern");
    28.         }
    29.         if(GUI.Button(new Rect(Screen.width/2 - 150,Screen.height/2 + 150,300,40),"Invasion")){
    30.             Application.LoadLevel("Invasion");
    31.         }
    32.         if(GUI.Button(new Rect(Screen.width/2 - 150,Screen.height/2 + 200,300,40),"Star Fighter")){
    33.             Application.LoadLevel("StarFighter");
    34.         }
    35.        
    36.         GUI.skin.label.alignment = TextAnchor.MiddleCenter;
    37.         GUI.Label(new Rect(0,Screen.height-90,Screen.width,50),"");
    38.     }
    39. }
    40.  
    this is the script for game UI

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameUI : MonoBehaviour
    5. {
    6.  
    7.     public GUISkin skin;
    8.     public Texture2D Logo;
    9.     public int Mode;
    10.     private GameManager game;
    11.     private PlayerController play;
    12.     private WeaponController weapon;
    13.            
    14.     void Start ()
    15.     {
    16.         game = (GameManager)GameObject.FindObjectOfType (typeof(GameManager));
    17.         play = (PlayerController)GameObject.FindObjectOfType (typeof(PlayerController));
    18.         weapon = play.GetComponent<WeaponController> ();
    19.         // define player
    20.        
    21.     }
    22.  
    23.     public void OnGUI ()
    24.     {
    25.        
    26.         if (skin)
    27.             GUI.skin = skin;
    28.        
    29.        
    30.         switch (Mode) {
    31.         case 0:
    32.             if (Input.GetKeyDown (KeyCode.Escape)) {
    33.                 Mode = 2;  
    34.             }
    35.            
    36.             if (play) {
    37.                
    38.                 play.Active = true;
    39.            
    40.                 GUI.skin.label.alignment = TextAnchor.UpperLeft;
    41.                 GUI.skin.label.fontSize = 30;
    42.                 GUI.Label (new Rect (20, 20, 200, 50), "Kills " + game.Killed.ToString ());
    43.                 GUI.Label (new Rect (20, 60, 200, 50), "Score " + game.Score.ToString ());
    44.                
    45.                 GUI.skin.label.alignment = TextAnchor.UpperRight;
    46.                 GUI.Label (new Rect (Screen.width - 220, 20, 200, 50), "ARMOR " + play.GetComponent<DamageManager> ().HP);
    47.                 GUI.skin.label.fontSize = 16;
    48.                
    49.                 // Draw Weapon system
    50.                 //if (weapon != null && weapon.WeaponLists.Length > 0 && weapon.WeaponLists.Length < weapon.CurrentWeapon && weapon.WeaponLists [weapon.CurrentWeapon] != null) {
    51.                     if (weapon.WeaponLists [weapon.CurrentWeapon].Icon)
    52.                         GUI.DrawTexture (new Rect (Screen.width - 100, Screen.height - 100, 80, 80), weapon.WeaponLists [weapon.CurrentWeapon].Icon);
    53.                
    54.                     GUI.skin.label.alignment = TextAnchor.UpperRight;
    55.                     if (weapon.WeaponLists [weapon.CurrentWeapon].Ammo <= 0 && weapon.WeaponLists [weapon.CurrentWeapon].ReloadingProcess > 0) {
    56.                         if (!weapon.WeaponLists [weapon.CurrentWeapon].InfinityAmmo)
    57.                             GUI.Label (new Rect (Screen.width - 230, Screen.height - 120, 200, 30), "Reloading " + Mathf.Floor ((1 - weapon.WeaponLists [weapon.CurrentWeapon].ReloadingProcess) * 100) + "%");
    58.                     } else {
    59.                         if (!weapon.WeaponLists [weapon.CurrentWeapon].InfinityAmmo)
    60.                             GUI.Label (new Rect (Screen.width - 230, Screen.height - 120, 200, 30), weapon.WeaponLists [weapon.CurrentWeapon].Ammo.ToString ());
    61.                     }
    62.                 //}else{
    63.                     //weapon = play.GetComponent<WeaponController> ();
    64.                 //}
    65.                
    66.                 GUI.skin.label.alignment = TextAnchor.UpperLeft;
    67.                 GUI.Label (new Rect (20, Screen.height - 50, 250, 30), "R Mouse : Switch Guns C : Change Camera");
    68.            
    69.             }else{
    70.                 play = (PlayerController)GameObject.FindObjectOfType (typeof(PlayerController));
    71.                 weapon = play.GetComponent<WeaponController> ();
    72.             }
    73.             break;
    74.         case 1:
    75.             if (play)
    76.                 play.Active = false;
    77.            
    78.             MouseLock.MouseLocked = false;
    79.            
    80.             GUI.skin.label.alignment = TextAnchor.MiddleCenter;
    81.             GUI.Label (new Rect (0, Screen.height / 2 + 10, Screen.width, 30), "Game Over");
    82.        
    83.             GUI.DrawTexture (new Rect (Screen.width / 2 - Logo.width / 2, Screen.height / 2 - 150, Logo.width, Logo.height), Logo);
    84.        
    85.             if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 50, 300, 40), "Restart")) {
    86.                 Application.LoadLevel (Application.loadedLevelName);
    87.            
    88.             }
    89.             if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 100, 300, 40), "Main menu")) {
    90.                 Application.LoadLevel ("Mainmenu");
    91.             }
    92.             break;
    93.        
    94.         case 2:
    95.             if (play)
    96.                 play.Active = false;
    97.            
    98.             MouseLock.MouseLocked = false;
    99.             Time.timeScale = 0;
    100.             GUI.skin.label.alignment = TextAnchor.MiddleCenter;
    101.             GUI.Label (new Rect (0, Screen.height / 2 + 10, Screen.width, 30), "Pause");
    102.        
    103.             GUI.DrawTexture (new Rect (Screen.width / 2 - Logo.width / 2, Screen.height / 2 - 150, Logo.width, Logo.height), Logo);
    104.        
    105.             if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 50, 300, 80), "Resume")) {
    106.                 Mode = 0;
    107.                 Time.timeScale = 1;
    108.             }
    109.             if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 100, 300, 40), "Main menu")) {
    110.                 Time.timeScale = 1;
    111.                 Mode = 0;
    112.                 Application.LoadLevel ("Mainmenu");
    113.             }
    114.             break;
    115.            
    116.         }
    117.        
    118.     }
    119. }
    120.  
    I have two scripts here, one for my main menu and one for my game UI
    the problem is when i attach these scripts they wont show up nicely in android output (they are really small) i dont have this problem while playing the game in unity.
    i can not use the canvas scale because i am not using the unity's UI system. these scripts are from the air strike starter pack.
    can anyone help please?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  3. M0RT3ZA

    M0RT3ZA

    Joined:
    Sep 13, 2016
    Posts:
    2
    Thanks for your reply
    I am using this package called "air strike starter pack" and these scripts are included in that package.
    I am making this game for a school project so i started modifying this package's demo scenes and I dont have enough time to get into details on making the game's UI with the unity's new UI system.
    i am just looking for a quick solution.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if that's a paid asset you really shouldn't be redistributing the source code openly... something to be aware of.
     
  5. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    To tell you the truth there is no easy solution to the problem.
    The rect 's that define how big your ui elements are, are defining the size at a fixed pixel size so it does not scale at all for your screen size on your mobile device.
    You would have to redefine every size in the ui to be a percentage of the screen width / height.