简体   繁体   English

Unity UI - 如何制作“复合”布局组以将多个图像组合在同一位置?

[英]Unity UI - How to make a “Composite” Layout Group to combine multiple images in the same location?

I'm making a simple auto-layout UI that consists of a Panel with a background image and three rows of text.我正在制作一个简单的自动布局 UI,它由一个带有背景图像和三行文本的面板组成。

To start with, I have a Vertical Layout Group component in a top-level "Panel" GameObject, set to Control Child Height.首先,我在顶级“面板”游戏对象中有一个垂直布局组组件,设置为控制子高度。 It also has a Content Size Fitter with "Preferred" set for Vertical Fit.它还有一个内容大小调整器,其中为垂直适合设置了“首选”。 The Panel object has a background image, and has a few children that represent rows in the view:面板 object 有一个背景图像,并且有几个子项表示视图中的行:

Panel [Vertical Layout Group] [Image] [Content Size Fitter]
  - Text 1
  - Text 2
  - Text 3

This all works well as-is.这一切都按原样运行。

My problem arises because I want the panel background to compose of two images - one is a filled background image, and the other is a mostly transparent "frame" image that fits over the background and adds detail just to the edges and corners.出现我的问题是因为我希望面板背景由两个图像组成 - 一个是填充的背景图像,另一个是大部分透明的“框架”图像,它适合背景并仅在边缘和角落添加细节。 Both images are sliced.两个图像都被切片。

Unfortunately Unity does not allow more than one Image component on a single GameObject, so this prevents me from simply adding both images to the Panel.不幸的是,Unity 不允许在单个 GameObject 上使用多个图像组件,因此这使我无法简单地将两个图像添加到面板中。 If this worked, it would suffice.如果这行得通,那就足够了。

Instead, if I add the image to a new child of the Panel then it gets included as a child in the VLG and the frame appears as the new first row, ie part of the vertical layout.相反,如果我将图像添加到 Panel 的新子项,则它将作为 VLG 中的子项包含在内,并且框架显示为新的第一行,即垂直布局的一部分。 Not what I'm after:不是我所追求的:

Panel [VLG] [Image:Background] [Content Size Fitter]
  - Frame [Image:Frame]
  - Text 1
  - Text 2
  - Text 3

I tried moving both Images as two children of the Panel, and then adding a third child as "Layout" with the original children as children of that, with the VLG (removing it from Panel):我尝试将两个图像作为面板的两个子级移动,然后将第三个子级添加为“布局”,并将原始子级作为其子级,使用 VLG(将其从面板中删除):

Panel [Content Size Fitter]
  - Background [Image: Background]
  - Frame [Image:Frame]
  - Layout [VLG]
    - Text 1
    - Text 2
    - Text 3

Unfortunately this doesn't seem to work either, because the dimensions of the two Images are not driven by the Panel if it doesn't contain a Layout Group of some kind.不幸的是,这似乎也不起作用,因为如果面板不包含某种布局组,则两个图像的尺寸不会由面板驱动。 But obviously adding a VLG to Panel would split out Background from Frame and from Layout when I really want all three superimposed.但是当我真的想要所有三个叠加时,显然将 VLG 添加到 Panel 会从 Frame 和 Layout 中分离出背景。

Is there a way to create a "Composite" Layout Group such that the children are combined on top of each other, rather than horizontally or vertically?有没有办法创建一个“复合”布局组,使孩子们相互组合,而不是水平或垂直? I looked at the source code for the UnityEngine.UI.LayoutGroup abstract base class and wondered if I could create something similar to VLG but just puts all the children at the same location by emulating UnityEngine.UI.VerticalLayoutGroup but returning the same Y value for each child position.我查看了UnityEngine.UI.LayoutGroup抽象基础 class 的源代码,想知道是否可以创建类似于 VLG 的东西,但只是通过模拟UnityEngine.UI.VerticalLayoutGroup将所有子级放在同一位置,但返回相同的 Y 值每个孩子 position。 Would this work?这行得通吗?

I know I can manually combine the two images into one and just use a single Image - however I'd like to better understand how I might be able to do this in the general case.我知道我可以手动将这两个图像组合成一个图像并只使用一个图像 - 但是我想更好地了解在一般情况下我如何能够做到这一点。 Also, you can get more interesting results with run-time composition when the Slice dimensions differ between images.此外,当图像之间的切片尺寸不同时,您可以通过运行时合成获得更有趣的结果。

try this one试试这个

Panel  [Image:Background] [Content Size Fitter]
  - Frame [VLG] [Image:Frame]
    - Text 1
    - Text 2
    - Text 3

With thanks to @Art Zolina III, the key is to put the second image on the only child of the Content Size Fitter, and use a Vertical Layout Group (VLG) on that same child to propagate the final children back to the top-level object (Panel).感谢@Art Zolina III,关键是将第二个图像放在 Content Size Fitter 的唯一孩子上,并在同一个孩子上使用垂直布局组 (VLG) 将最终的孩子传播回顶层object(面板)。

Panel [Image:Background, sliced] 
      [Content Size Fitter: Horiz Preferred, Vert Preferred]
      [Vertical Layout Group: Control Child Width, Height]
  - Frame [Image:Framing, sliced] 
          [VLG: Control Child Width, Height; Child Force Expand Width]
    - Text 1 [TextMeshPro Text]
    - Text 2 [TextMeshPro Text]
    - Text 3 [TextMeshPro Text]

As a note, the preferred height of a sliced image is the sum of the top and bottom border heights, which means that it cannot easily shrink to less than that height.请注意,切片图像的首选高度是顶部和底部边框高度的总和,这意味着它不能轻易缩小到小于该高度。 This tripped me up because my image had a combined vertical border space of 900 pixels which was fooling me into thinking my layout wasn't shrinking “to fit”.这让我大吃一惊,因为我的图像有一个 900 像素的组合垂直边框空间,这让我误以为我的布局没有缩小“以适应”。 Once I used a sliced image with smaller top & bottom margins, the panel layout appeared to work correctly.一旦我使用了具有较小顶部和底部边距的切片图像,面板布局似乎可以正常工作。

Additionally, the slicing boundaries of a Sprite seem to be affected by the Pixels Per Unit Multiplier, which means you can make the boundaries smaller than they ought to be visually, but increase the multiplier to still have all the margins rendered in entirety (but at a higher resolution).此外,Sprite 的切片边界似乎受到每单位像素乘数的影响,这意味着您可以使边界小于视觉上应有的值,但增加乘数以仍然完整呈现所有边距(但在更高的分辨率)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM