• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by IronNinjaGames · Sep 01, 2014 at 06:31 AM · guiui4.6resize

New 4.6 UI question: How do you resize a panel relative to the screen but remain height/width constrained?

I'm having a little trouble getting my title screen set up and I'm curious how I make something that stretches with the screen in proportion, yet remains size constrained. The title is a Panel with several other UI elements as children.

A simple way to describe it is that it should have a distance from one side of the screen of at least 20% of the screen's width/height on either the horizontal or vertical (whichever is less). Or put another way, it will always be 80% of the screen's smallest dimension.

So for example -- let's say the setup screen is 1000 X 1000 pixels and my title is 800 X 800 pixels.

1) When the screen is shrunk to 700 X 500 pixels, the title would shrink to 400 X 400 (80% of 500, because it's the lessor of the two).

2) When the screen is increased to 2000 X 3000 pixels, the title would be stretched to 1600 X 1600 (80% of 2000, because it's the lessor of the two).

How do I do that with the new 4.6 UI?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image U_Ku_Shu · Aug 06, 2016 at 07:05 PM 0
Share

The second way is: 1. Check that you have added Canvas Scaler (it's must be added by default) 2. Configure it: a. "Scale with Screen Size" b. "$$anonymous$$atch width Or Height" c. Set $$anonymous$$atch to Height (in case came is for mobile hardware an it's horisontal-oriented)

5 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by Johannski · Dec 06, 2014 at 06:32 PM

I don't know, if you are still searching for an answer, but Unity added an Aspect ratio fitter. The way I would do it: Create an empty game object and add a aspect ratio fitter to it (Layout->Aspect Ratio Fitter). Set the Aspect Mode to "Fit in Parent" and the aspect ratio to whatever you need it (1 in case it is a square). Then add a child object to it. There set the rect transform to stretch in both ways and the pivot point to 0.5 0.5. Then set the anchors to to where-ever you need them. For example if you would want 80% of the screen size, then it would be: Min: x: 0.1, y:0.1 Max: x: 0.9, y:0.9

And that's it, easy and robust.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image slippdouglas · Dec 18, 2014 at 05:56 PM 0
Share

+1: Easiest, simplest, most built-in way to solve this.

avatar image AndyNeoman · Jan 08, 2015 at 09:15 PM 0
Share

Want to save this, great answer and solves a problem I have been having too. would up vote if I could :-)

avatar image Jix · Jan 20, 2015 at 06:37 AM 1
Share

Why isn't this the selected answer? I would just like to add that you shouldn't set the aspect ratio to 1, set to whatever aspect ratio you're using

avatar image Johannski · Jan 20, 2015 at 08:11 AM 0
Share

I guess, because IronNinjaGames didn't really care anymore. Thanks for your correction, I updated my answer.

avatar image Johannski · May 18, 2015 at 08:07 PM 1
Share

No, the canvas rescaler works for all resolutions. The resolution you set is kind a reference resolution from which it will determine the resolution of an object in the canvas. It is really easy to use, try it with the settings from my screenshot and read the documentation on it :)

Show more comments
avatar image
0

Answer by HuskyPanda213 · Sep 04, 2014 at 01:11 AM

Another method is a custom script that I made, just keep in mind that the object should always be set to the highest screen resolution so the UI does not blur.

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("UI/UI Resizer")]
 public class UIResizer : MonoBehaviour
 {
 
     // The resolution that the UI was initially designed for.
     [SerializeField]
     Vector2 targetResolution = new Vector2(1920, 1080);
 
     // The Transform that should be resized, if not given then itself.
     [SerializeField]
     Transform UIObject;
 
     void Start()
     {
         if(!UIObject)
             UIObject = transform;
         SizeResolution (new Vector2 (Screen.width, Screen.height));
     }
 
     public void SizeResolution(Vector2 newResolution)
     {
         Vector3 scale = UIObject.localScale;
         scale.x = newResolution.x / targetResolution.x;
         scale.y = newResolution.y / targetResolution.y;
         UIObject.localScale = scale;
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

Answer by HanzaRu · Sep 30, 2014 at 09:24 PM

Actually isn't necessary anymore do extra-code to make the UI fit in screen. Usign "Canvas Scaler" component, you can select the "Scale With Screen Size" and define the best screen size for Height and move the slider entirely to height. you will have you canvas scaling and not deforming the elements in screen.

Of course you still needing have some work to anchor the UI on left/right to perform correct Aspect Ratio resize on multiple screens.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tylercook · Aug 13, 2015 at 06:33 PM 0
Share

If you do this, your entire UI gets smaller as you lower your game's resolution. Not always what you want, especially if you are making a game/application that exists in a resizable window.

avatar image
0

Answer by U_Ku_Shu · Nov 10, 2015 at 05:34 PM

The best solution for this is correct configuration of the Canvas:

As I see the best practice is:

1) to set render mode to "Screen Space - Camera"

2) Plane distance to 2-10

3. Assign one additional Canvas Scaler to your Canvas and set the following settings:

3.a: Ui ScaleMode:Scale with screen Size

3.b: Reference Resolution 2500x1440 (or your max srceen size that you planning to develop for)

3.c: Match mode: Height (1)

4) Set width and height of children with some stock

5) In all texts change Vertical Overflow to "Overflow"

this will solve lot of problems like:

1) large objects on scene

2) pixelization of objects edges

3) Absent ability to add ParticleSystem to your UI screen (sometimes needed in mobile menus games)

4) Absent ability to resize a panel relative to the screen but remain height/width constrained

5) Text hide on font resize

But in your case you can use only 3rd (highlited)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by leonardon · Apr 08, 2016 at 10:27 PM

Just go to the Canvas Scaler component that belongs to your canvas and set UI Scale Mode to Scale With Screen Size then set the Reference Resolution!

That is all!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

v4.6 UI Element's Positioning Incorrect 1 Answer

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer

Unity 4.6B UI Scrollbar Usage 1 Answer

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers

Sorting the children of a Grid Layout in 4.6 UI 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges