- Home /
Fixed to the screen GUI ?
Hello everyone at Unity Answer, I have a GUI script for a gun hud but when the screen resolution changes the hud moves i wont it to say in one place. The script i'm useing is this.
 function OnGUI (){
     if(selected){
         GUI.skin = mySkin;
         var style1 = mySkin.customStyles[0];
         if(ammoMode == Ammo.Magazines){
             if(firstMode != fireMode.none && firstMode != fireMode.launcher || secondMode != fireMode.none && secondMode != fireMode.launcher){
                 GUI.Label (Rect(Screen.width - 200,Screen.height-35,200,80),"Ammo : ");
                 GUI.Label (Rect(Screen.width - 110,Screen.height-35,200,80),"" + bulletsLeft, style1);
                 GUI.Label (Rect(Screen.width - 80,Screen.height-35,200,80)," / " + magazines);
             }
         }    
         
         if(ammoMode == Ammo.Bullets){
             if(firstMode != fireMode.none && firstMode != fireMode.launcher || secondMode != fireMode.none && secondMode != fireMode.launcher){
                 GUI.Label (Rect(Screen.width - 200,Screen.height-35,200,80),"Bullets : ");
                 GUI.Label (Rect(Screen.width - 110,Screen.height-35,200,80),"" + bulletsLeft, style1);
                 GUI.Label (Rect(Screen.width - 80,Screen.height-35,200,80)," |  " + magazines);
             }    
         }
         
         if(firstMode != fireMode.none || secondMode != fireMode.none){
             GUI.Label (Rect(Screen.width - 200,Screen.height-65,200,80),"Firing Mode :");
             GUI.Label (Rect(Screen.width - 110,Screen.height-65,200,80),"" + currentMode, style1);
         }
         
         if(secondMode == fireMode.launcher || firstMode == fireMode.launcher){
             GUI.Label (Rect(Screen.width - 200,Screen.height-95,200,80),"Projectiles : ");
             GUI.Label (Rect(Screen.width - 110,Screen.height-95,200,80),"" + projectiles, style1);
         }    
         
         if(crosshairFirstModeHorizontal != null){    
             if(currentMode != fireMode.launcher){
                 var w = crosshairFirstModeHorizontal.width;
                 var h = crosshairFirstModeHorizontal.height;
                 position1 = Rect((Screen.width + w)/2 + (triggerTime * adjustMaxCroshairSize),(Screen.height - h)/2, w, h);
                 position2 = Rect((Screen.width - w)/2,(Screen.height + h)/2 + (triggerTime * adjustMaxCroshairSize), w, h);
                 position3 = Rect((Screen.width - w)/2 - (triggerTime * adjustMaxCroshairSize) - w,(Screen.height - h )/2, w, h);
                 position4 = Rect((Screen.width - w)/2,(Screen.height - h)/2 - (triggerTime * adjustMaxCroshairSize) - h, w, h);
                 if (!aiming) { 
                     GUI.DrawTexture(position1, crosshairFirstModeHorizontal);     
                     GUI.DrawTexture(position2, crosshairFirstModeVertical);     
                     GUI.DrawTexture(position3, crosshairFirstModeHorizontal);     
                     GUI.DrawTexture(position4, crosshairFirstModeVertical);        
                 }
             }
         }    
     
Could someone help me ?
Thank you for your time, Have a nice day.
Answer by GuyTidhar · Jan 27, 2013 at 11:44 AM
Try using GUI.Matrix instead of positioning according to the current width or height. This will stretch or shrink your whole GUI:
 float originalWidth = 1024; // Design resoultion
 float originalHeight = 768;
 void OnGUI()
 {
   // Set matrix
   Vector2 ratio = new Vector2(Screen.width/originalWidth , Screen.height/originalHeight );
   Matrix4x4 guiMatrix = Matrix4x4.identity;
   guiMatrix.SetTRS(new Vector3(1, 1, 1), Quaternion.identity, new Vector3(ratio.x, ratio.y, 1));
   GUI.matrix = guiMatrix;
  
   // Do you GUI here
  
   // Reset matrix
   GUI.matrix = Matrix4x4.identity;
 }
Don't forget to NOT use the Screen.width or Screen.height within your GUI functions if you use this.
Your answer
 
 
             Follow this Question
Related Questions
Fixed Screen Resolution 2 Answers
Player Screen Resolution 2 Answers
Clamping after forcing aspect ratio 0 Answers
Fit GUI to various android screen resolutions and densities 6 Answers
NOT auto resizing GUI, keeping GUI always the same aspect ratio 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                