Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Location
    CO, USA
    Posts
    63

    iOS and Android Stretch and Scaling properly

    Hey Everyone,

    So I am having a Stretch/Scaling issue. I am upgrading my project from iOS to iOS and Android. Originally it was 3 seperate image resolutions per asset (1024x768, 960, 480) the iOS resolutions. I have decided that I want to change that because otherwise we would have to create 18 different resolution assets just to support Android, and well even the above multiple assets per resolutions made my eye twitch. So I basically have the below main menu in Unity iOS (1024x768) and when I use my scaling code that I found online, and from reading a few books on this, I do get a scaled down look, but it is smaller to fit but perfectly scaled, and thus you have empty space that needs to be filled. I am trying to make it Fit to resolution and still look good. Any pointers would be awesome

    I have decided to use the iOS 1024x768 Asset set as my base scaling point and scale up or down from it.

    (Unity iOS sample(1024x768))
    https://www.dropbox.com/s/1c9mj1mm8o...ad1024x768.png

    (UnityAndroid sample(1024x600))
    https://www.dropbox.com/s/sviohjbqxe...d_1024x600.png

    Here is my code:

    GUISizer class
    Code:
     public class GUISizer
        {
            private static float WIDTH = 1024;
            private static float HEIGHT = 768;
    
            static List<Matrix4x4>  stack = new List<Matrix4x4>();
    
            static public void BeginGUI()
            {
                stack.Add(UnityEngine.GUI.matrix);
                var m = new Matrix4x4();
                var w = (float) Screen.width;
                var h = (float) Screen.height;
                var aspect = w/h;
                var scale = 1f;
                var offset = Vector3.zero;
    
                if(aspect < (WIDTH/HEIGHT)) // Screen is taller 
                {
                    scale = (Screen.width/WIDTH);
                    offset.y += (Screen.height - (HEIGHT * scale))*0.5f;
    
                }
                else
                {
                    scale = (Screen.height/HEIGHT);
                    offset.x += (Screen.width - (WIDTH * scale))*0.5f;
                    
                }
    
                m.SetTRS(offset, Quaternion.identity, Vector3.one*scale);
                UnityEngine.GUI.matrix *= m;
    
            }
    
            static public void EndGUI()
            {
                UnityEngine.GUI.matrix = stack[stack.Count - 1];
                stack.RemoveAt(stack.Count-1);
            }
        }
    OnGUI call
    Code:
     
    public GUIButton[] buttons;
    ...
    void OnGUI()
        {
            GUISizer.BeginGUI();
            GUI.DrawTexture(new Rect(0, 0, 1024, 768), background);
            GUI.DrawTexture(new Rect(0, 0, 1024, 768), MenuOverlay);
    
            
            if(p_buttonStyle == null)
            {
                p_buttonStyle = GUI.skin.FindStyle(buttonStyle);
            }
          
            foreach (var t in buttons)
            {
                GUI.Button(t.rect, t.content, p_buttonStyle);
                //Pressed(buttons[i].name);
            }
    
    
            // Restore matrix before returning
            GUISizer.EndGUI();
        }

    GUIButton Class
    Code:
    	[System.Serializable]
    	public class GUIButton
    	{
            
    	    public string name;
    	    public GUIContent content;
    		public float width=0;
    		public float height=0;
    	    public Vector2 position;
    
    	    private Rect r;
    	    public Rect rect
    	    {
    	        get
    	        {
    	           
    	           if(width == 0 && content.image.width != null)
    				{
    
                        width =  content.image.width;
                        
    				}
    				if(height == 0 && content.image.height != null)
    				{
    				    height = content.image.height;
    				}
    
                    r = new Rect(position.x , position.y , width , height );
                    Debug.Log(string.Format("Position: ({0},{1}), Size: ({2},{3})", position.x, position.y, width, height));
    	            return r;
    	        }
    	    }
    	}
    Gav: You're going to perform a magic trick?
    Nelson: Yes, but it's actually going to work.
    My blog! http://darxstudios.com
    AllocateThis! Studios http://AllocateThis.com
    AllocateThis! Studios Blog http://blog.AllocateThis.com

    Mud Designer Kit
    Mud Designer Tutorial Vids [YouTube]

  2. #2
    Join Date
    Nov 2009
    Location
    CO, USA
    Posts
    63

    iOS and Android Stretch and Scaling properly {SOLVED}

    Hey guys i ended up solving this!! If you want to use it that is kewl, I dont mind I struggled to find something that works. update the GUISizer class like so.

    Code:
    public class GUISizer     
    {
             private static float WIDTH = 1024;         
            private static float HEIGHT = 768;          
            static List<Matrix4x4>  stack = new List<Matrix4x4>();
    
            static public void BeginGUI()        
            {                
                stack.Add(UnityEngine.GUI.matrix); 
                var m = new Matrix4x4();             
                var scaleX = 1f;           
                var scaleY = 1f;          
               
                ScaleY = (Screen.height / HEIGHT);
                ScaleX = (Screen.width / WIDTH);
              
                m.SetTRS(Vector3.zero, Quaternion.identity, new Vector3(ScaleX,ScaleY,1));
                 UnityEngine.GUI.matrix *= m;
                 
    
               }
              
             static public void EndGUI()         
             {            
                 UnityEngine.GUI.matrix = stack[stack.Count - 1];
                  stack.RemoveAt(stack.Count-1);
              }
    }
    Last edited by radio-active; 05-18-2012 at 10:19 AM. Reason: Code got all compacted....
    Gav: You're going to perform a magic trick?
    Nelson: Yes, but it's actually going to work.
    My blog! http://darxstudios.com
    AllocateThis! Studios http://AllocateThis.com
    AllocateThis! Studios Blog http://blog.AllocateThis.com

    Mud Designer Kit
    Mud Designer Tutorial Vids [YouTube]

  3. #3
    Join Date
    Nov 2012
    Posts
    1
    It's exactly what I was looking for, thanks a lot !

    Does the android version still look good with the stretching ?

    I really like the fact you post your solution to fix the problem. It's quicker for me, so again, thank you.

  4. #4
    Join Date
    Nov 2009
    Location
    CO, USA
    Posts
    63
    I haven't tested this on Unity4 yet. but it should work. I am contemplating making a unity3d Mobile app template for all this and have it stored via Github heh
    Gav: You're going to perform a magic trick?
    Nelson: Yes, but it's actually going to work.
    My blog! http://darxstudios.com
    AllocateThis! Studios http://AllocateThis.com
    AllocateThis! Studios Blog http://blog.AllocateThis.com

    Mud Designer Kit
    Mud Designer Tutorial Vids [YouTube]

  5. #5
    Join Date
    May 2013
    Posts
    1

    Thumbs up Help some noobs

    Quote Originally Posted by radio-active View Post
    I haven't tested this on Unity4 yet. but it should work. I am contemplating making a unity3d Mobile app template for all this and have it stored via Github heh
    Sir , Since i am noob please guide me , tell me how to implement it , how to use it on our gui textures or own custom gui scrips

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •