95 lines
2.3 KiB
C#
95 lines
2.3 KiB
C#
using EasyLayout;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
public class EasyLayoutBridge : ILayoutBridge
|
|
{
|
|
private bool isHorizontal;
|
|
|
|
private global::EasyLayout.EasyLayout Layout;
|
|
|
|
private RectTransform DefaultItem;
|
|
|
|
public bool IsHorizontal
|
|
{
|
|
get
|
|
{
|
|
return isHorizontal;
|
|
}
|
|
set
|
|
{
|
|
isHorizontal = value;
|
|
UpdateDirection();
|
|
}
|
|
}
|
|
|
|
public bool UpdateContentSizeFitter { get; set; }
|
|
|
|
public EasyLayoutBridge(global::EasyLayout.EasyLayout layout, RectTransform defaultItem)
|
|
{
|
|
Layout = layout;
|
|
DefaultItem = defaultItem;
|
|
}
|
|
|
|
private void UpdateDirection()
|
|
{
|
|
Layout.Stacking = (isHorizontal ? Stackings.Vertical : Stackings.Horizontal);
|
|
if (UpdateContentSizeFitter)
|
|
{
|
|
ContentSizeFitter component = Layout.GetComponent<ContentSizeFitter>();
|
|
if ((bool)component)
|
|
{
|
|
component.horizontalFit = (IsHorizontal ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained);
|
|
component.verticalFit = ((!IsHorizontal) ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained);
|
|
}
|
|
}
|
|
RectTransform rectTransform = Layout.transform as RectTransform;
|
|
rectTransform.pivot = new Vector2(0f, 1f);
|
|
if (isHorizontal)
|
|
{
|
|
rectTransform.anchorMin = new Vector2(0f, 0f);
|
|
rectTransform.anchorMax = new Vector2(0f, 1f);
|
|
}
|
|
else
|
|
{
|
|
rectTransform.anchorMin = new Vector2(0f, 1f);
|
|
rectTransform.anchorMax = new Vector2(1f, 1f);
|
|
}
|
|
rectTransform.sizeDelta = new Vector2(0f, 0f);
|
|
}
|
|
|
|
public void UpdateLayout()
|
|
{
|
|
Layout.UpdateLayout();
|
|
}
|
|
|
|
public void SetFiller(float first, float last)
|
|
{
|
|
Padding paddingInner = ((!IsHorizontal) ? new Padding(0f, 0f, first, last) : new Padding(first, last, 0f, 0f));
|
|
Layout.PaddingInner = paddingInner;
|
|
}
|
|
|
|
public Vector2 GetItemSize()
|
|
{
|
|
return new Vector2(DefaultItem.rect.width, DefaultItem.rect.height);
|
|
}
|
|
|
|
public float GetMargin()
|
|
{
|
|
return (!IsHorizontal) ? Layout.GetMarginTop() : Layout.GetMarginLeft();
|
|
}
|
|
|
|
public float GetFullMargin()
|
|
{
|
|
return (!IsHorizontal) ? (Layout.GetMarginTop() + Layout.GetMarginBottom()) : (Layout.GetMarginLeft() + Layout.GetMarginRight());
|
|
}
|
|
|
|
public float GetSpacing()
|
|
{
|
|
return (!IsHorizontal) ? Layout.Spacing.y : Layout.Spacing.x;
|
|
}
|
|
}
|
|
}
|