131 lines
3.2 KiB
C#
131 lines
3.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
public class StandardLayoutBridge : ILayoutBridge
|
|
{
|
|
private bool isHorizontal;
|
|
|
|
private HorizontalOrVerticalLayoutGroup Layout;
|
|
|
|
private RectTransform DefaultItem;
|
|
|
|
private LayoutElement FirstFiller;
|
|
|
|
private LayoutElement LastFiller;
|
|
|
|
public bool IsHorizontal
|
|
{
|
|
get
|
|
{
|
|
return isHorizontal;
|
|
}
|
|
set
|
|
{
|
|
throw new NotSupportedException("HorizontalLayoutGroup Or VerticalLayoutGroup direction cannot be change in runtime.");
|
|
}
|
|
}
|
|
|
|
public bool UpdateContentSizeFitter { get; set; }
|
|
|
|
public StandardLayoutBridge(HorizontalOrVerticalLayoutGroup layout, RectTransform defaultItem)
|
|
{
|
|
Utilites.UpdateLayout(layout);
|
|
Layout = layout;
|
|
DefaultItem = defaultItem;
|
|
isHorizontal = layout is HorizontalLayoutGroup;
|
|
GameObject gameObject = new GameObject("FirstFiller");
|
|
RectTransform rectTransform = (gameObject.transform as RectTransform) ?? gameObject.AddComponent<RectTransform>();
|
|
rectTransform.SetParent(Layout.transform);
|
|
rectTransform.localScale = Vector3.one;
|
|
FirstFiller = gameObject.AddComponent<LayoutElement>();
|
|
GameObject gameObject2 = new GameObject("LastFiller");
|
|
RectTransform rectTransform2 = (gameObject2.transform as RectTransform) ?? gameObject2.AddComponent<RectTransform>();
|
|
rectTransform2.SetParent(Layout.transform);
|
|
rectTransform2.localScale = Vector3.one;
|
|
LastFiller = gameObject2.AddComponent<LayoutElement>();
|
|
Vector2 itemSize = GetItemSize();
|
|
if (IsHorizontal)
|
|
{
|
|
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemSize.y);
|
|
rectTransform2.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemSize.y);
|
|
}
|
|
else
|
|
{
|
|
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, itemSize.x);
|
|
rectTransform2.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, itemSize.x);
|
|
}
|
|
}
|
|
|
|
public void UpdateLayout()
|
|
{
|
|
Utilites.UpdateLayout(Layout);
|
|
}
|
|
|
|
public void SetFiller(float first, float last)
|
|
{
|
|
if (FirstFiller != null)
|
|
{
|
|
if (first == 0f)
|
|
{
|
|
FirstFiller.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
FirstFiller.gameObject.SetActive(true);
|
|
FirstFiller.transform.SetAsFirstSibling();
|
|
if (IsHorizontal)
|
|
{
|
|
FirstFiller.preferredWidth = first;
|
|
}
|
|
else
|
|
{
|
|
FirstFiller.preferredHeight = first;
|
|
}
|
|
}
|
|
}
|
|
if (!(LastFiller != null))
|
|
{
|
|
return;
|
|
}
|
|
if (last == 0f)
|
|
{
|
|
LastFiller.gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
LastFiller.gameObject.SetActive(true);
|
|
if (IsHorizontal)
|
|
{
|
|
LastFiller.preferredWidth = last;
|
|
}
|
|
else
|
|
{
|
|
LastFiller.preferredHeight = last;
|
|
}
|
|
LastFiller.transform.SetAsLastSibling();
|
|
}
|
|
|
|
public Vector2 GetItemSize()
|
|
{
|
|
return new Vector2(LayoutUtility.GetPreferredWidth(DefaultItem), LayoutUtility.GetPreferredHeight(DefaultItem));
|
|
}
|
|
|
|
public float GetMargin()
|
|
{
|
|
return (!IsHorizontal) ? Layout.padding.top : Layout.padding.left;
|
|
}
|
|
|
|
public float GetFullMargin()
|
|
{
|
|
return (!IsHorizontal) ? Layout.padding.vertical : Layout.padding.horizontal;
|
|
}
|
|
|
|
public float GetSpacing()
|
|
{
|
|
return Layout.spacing;
|
|
}
|
|
}
|
|
}
|