• 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
0
Question by ina · Oct 31, 2014 at 06:55 AM · guiuieditorlayout

uGUI How do you mix Relative + Precision-pixel layouts?

I've been trying to use uGUI to put together this odd layout, but the button alignment just doesn't work on different screen sizes. What's the right combination of uGUI elements to do this layout -

Basically the "touch" button is contained inside an image element panel that's 80% of the screen left aligned. (Yes, there's also arbitrary text in that panel.)

So, this also makes the button width/height relative as well - how do you get the margins to align so it's centered in the bottom part of the L?

See attached mockup

alt text

80percentprecise2.png (20.2 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by troien · Oct 31, 2014 at 12:36 PM

What is it you try to achieve exactly?

Since I'm not sure, I'll give you this answer making a lot of assumptions, in the hope they are correct :p

The way I understand it is that you want is:

  • a Panel that has a width of 80% of the screen. (Unknown height)

  • A button which is position below the panel on the right side (I suppose this is in pixels?)

If this is correct then this should do it...

Panel:

  • AnchorMin: X=0.0 | y=?

  • AnchorMax: x=0.8 | y=?

  • Pivot: x=? | y=?

  • Left: 0

  • Right: 0

  • Posy: ?

  • Posz: ?

  • Height:?

button (Make it a child of the panel)

  • AnchorMin: X=1.0 | y=0.0

  • AnchorMax: x=1.0 | y=0.0

  • Pivot: x=1.0 | y=1.0

  • PosX: 0

  • PosY: 0

  • Posz: 0

  • Width: ?

  • Height: ?

If the button is not in pixels, but should instead scale with the panel in the width. Then you should add a AspectRatioFitter and set it to WidthControlsHeight, and change the anchorminX of the button to whatever value you want...

If this does not answer your question then please make the question more clear :p

Comment
Add comment · Show 5 · 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 ina · Oct 31, 2014 at 02:43 PM 0
Share

Difficulty is that the button must fit exactly centered in the bottom part of the L..

avatar image ina · Oct 31, 2014 at 02:44 PM 0
Share

i've tried your method but it's still distorted... i'm currently using a panel + button (image) - not clickable + button (clickable) - each being parent of the other

avatar image troien · Oct 31, 2014 at 06:23 PM 0
Share

I'm still confused by what you have got.

The way I understand it now is that you have:

  • Panel (80% of width)

  • BackGround (Child of panel) (Image with L shape) (Has the same size as Panel?)

  • Button (The Text 'Toutch' with the white border) (Child of Background)

  • Text (The 'Lorem ipsum') (Child of Background)

Is this correct?

Thing that would really be handy for me to know if I want to answer your question:

  • What is the height of your Panel/L shaped background (percentage of screen or pixel?)

  • Does the bottom part of the L stretch in the width and/or height if the screen size stretches?

  • Is the button allowed to stretch in width and/or height (if not, is it allowed to resize while remaining it's aspect ratio?)

Assuming that you have the above setup, and that the answer to my second question is yes, the answer to my third question is no, followed by yes, I should try the following setup:

  • Background (80% of width and some height, with L shape image)

  • Container (Child of background) (Place the Anchormin and Anchor$$anonymous$$ax in a position so that they cover your right bottom part of your L shape, and set left-right-top-bottom margins to whatever you think is right, this is the margin you want the button to be from the sides)

  • Button (Child of Container) (Add AspectRatioFitter script and set it to FitInParent, with a aspect ratio of 1)

avatar image ina · Oct 31, 2014 at 10:58 PM 0
Share

Say the width is 750. The panel width is 665. The height should keep the same aspect ratio as the panel graphic. The button can stretch but should remain square in aspect ratio.

Setting the button to fit in parent seems to just blow it out of proportions..

avatar image troien · Nov 02, 2014 at 03:50 PM 0
Share

Well, I'm sure that what you want is possible, I'm just not sure about what you want exactly.

Therefore, this image is basically my last try :p I made my own empty project and filled it with the following. (I'm using a black squared image as substitude for your button) For me the scaling looks good if I use this setup, but then again, I don't know your setup :p alt text

example.png (83.0 kB)
avatar image
0

Answer by Scribe · Nov 02, 2014 at 07:31 PM

Maybe this script would work for you:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class alignUI : MonoBehaviour {
     public Sprite backgroundSprite;
     public Sprite buttonSprite;
     public Font f;
     public Vector2 rightAlign = new Vector2(0.8f, 0.5f);
     GameObject banner;
     GameObject buttonBackground;
     GameObject button;
     GameObject text;
     RectTransform bannerRect;
     RectTransform buttonBGRect;
     RectTransform buttonRect;
     RectTransform textRect;
     public int pixelSize = 80;
     public int inset = 5;
     
     void Start(){
         Init();
     }
     
     void Update(){
         CalcPosition();
     }
     
     void CalcPosition(){
         //BANNER
         bannerRect.anchorMin = new Vector2(0, rightAlign.y);
         bannerRect.anchorMax = rightAlign;
         bannerRect.offsetMin = Vector2.zero;
         bannerRect.offsetMax = new Vector2(0, pixelSize);
         //BUTTON BACKGROUND
         buttonBGRect.anchorMin = buttonBGRect.anchorMax = rightAlign;
         buttonBGRect.offsetMin = new Vector2(-pixelSize, -pixelSize);
         buttonBGRect.offsetMax = Vector2.zero;
         //BUTTON
         buttonRect.anchorMin = buttonRect.anchorMax = rightAlign;
         buttonRect.offsetMin = new Vector2(inset-pixelSize, inset-pixelSize);
         buttonRect.offsetMax = new Vector2(-inset, -inset);
         //TEXTBOX
         textRect.anchorMin = new Vector2(0, rightAlign.y);
         textRect.anchorMax = rightAlign;
         textRect.offsetMin = new Vector2(inset, inset);
         textRect.offsetMax = new Vector2(-inset, pixelSize-inset);
     }
     
     void Init(){
         banner = new GameObject();
         banner.name = "banner";
         bannerRect = banner.AddComponent<RectTransform>();
         Image iRef = banner.AddComponent<Image>();
         iRef.sprite = backgroundSprite;
         banner.transform.parent = transform;
         
         buttonBackground = new GameObject();
         buttonBackground.name = "buttonBackground";
         buttonBGRect = buttonBackground.AddComponent<RectTransform>();
         iRef = buttonBackground.AddComponent<Image>();
         iRef.sprite = backgroundSprite;
         buttonBackground.transform.parent = transform;
         
         button = new GameObject();
         button.name = "button";
         buttonRect = button.AddComponent<RectTransform>();
         button.AddComponent<CanvasRenderer>();
         iRef = button.AddComponent<Image>();
         iRef.sprite = buttonSprite;
         iRef.type = Image.Type.Sliced;
         Button bRef = button.AddComponent<Button>();
         bRef.targetGraphic = button.GetComponent<Graphic>();
         button.transform.parent = transform;
         
         text = new GameObject();
         text.name = "text";
         textRect = text.AddComponent<RectTransform>();
         Text tRef = text.AddComponent<Text>();
         tRef.text = "Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World!";
         tRef.font = f;
         tRef.color = Color.black;
         text.transform.parent = transform;
         CalcPosition();
     }
 }


You don't need to do much except choose a font, and a sprite for the background and the button, the rest should align itself how I think I understand you want it!

Feel free to ask any questions :)

Scribe

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

uGUI WorldSpace elements part of GameObjects 1 Answer

uGUI - aligning to the bottom of another element 0 Answers

uGUI text field iOS 0 Answers

EditorGUILayout.Foldout seems to stop working past indent 3 0 Answers

uGUI creating a list 0 Answers

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