Add project files.
This commit is contained in:
104
Assets/Cryville/Common/Unity/UI/AspectRatioLayoutElement.cs
Normal file
104
Assets/Cryville/Common/Unity/UI/AspectRatioLayoutElement.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Common.Unity.UI {
|
||||
public class AspectRatioLayoutElement : UIBehaviour, ILayoutElement {
|
||||
[SerializeField]
|
||||
private RectTransform m_containerTransform = null;
|
||||
public RectTransform ContainerTransform {
|
||||
get { return m_containerTransform; }
|
||||
set { SetProperty(ref m_containerTransform, value); }
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private float m_aspectRatio = 1;
|
||||
public float AspectRatio {
|
||||
get { return m_aspectRatio; }
|
||||
set { SetProperty(ref m_aspectRatio, value); }
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private bool m_isVertical = false;
|
||||
public bool IsVertical {
|
||||
get { return m_isVertical; }
|
||||
set { SetProperty(ref m_isVertical, value); }
|
||||
}
|
||||
|
||||
private void SetProperty<T>(ref T prop, T value) {
|
||||
if (Equals(prop, value)) return;
|
||||
prop = value;
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
private void SetDirty() {
|
||||
if (!IsActive()) return;
|
||||
LayoutRebuilder.MarkLayoutForRebuild(transform as RectTransform);
|
||||
}
|
||||
|
||||
public float minWidth {
|
||||
get {
|
||||
return m_isVertical
|
||||
? m_containerTransform.rect.width
|
||||
: m_containerTransform.rect.height * m_aspectRatio;
|
||||
}
|
||||
}
|
||||
public float preferredWidth { get { return minWidth; } }
|
||||
public float flexibleWidth { get { return 0; } }
|
||||
|
||||
public float minHeight {
|
||||
get {
|
||||
return m_isVertical
|
||||
? m_containerTransform.rect.width / m_aspectRatio
|
||||
: m_containerTransform.rect.height;
|
||||
}
|
||||
}
|
||||
public float preferredHeight { get { return minHeight; } }
|
||||
public float flexibleHeight { get { return 0; } }
|
||||
|
||||
public int layoutPriority { get { return 1; } }
|
||||
|
||||
private bool isRootLayoutGroup {
|
||||
get {
|
||||
Transform parent = transform.parent;
|
||||
return parent == null || transform.parent.GetComponent(typeof(ILayoutGroup)) == null;
|
||||
}
|
||||
}
|
||||
|
||||
public void CalculateLayoutInputHorizontal() { }
|
||||
|
||||
public void CalculateLayoutInputVertical() { }
|
||||
|
||||
protected override void OnDidApplyAnimationProperties() {
|
||||
base.OnDidApplyAnimationProperties();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
protected override void OnDisable() {
|
||||
LayoutRebuilder.MarkLayoutForRebuild(transform as RectTransform);
|
||||
base.OnDisable();
|
||||
}
|
||||
|
||||
protected override void OnEnable() {
|
||||
base.OnEnable();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
protected override void OnRectTransformDimensionsChange() {
|
||||
base.OnRectTransformDimensionsChange();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
protected override void OnTransformParentChanged() {
|
||||
base.OnTransformParentChanged();
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
#pragma warning disable IDE0051
|
||||
new void OnValidate() {
|
||||
SetDirty();
|
||||
}
|
||||
#pragma warning restore IDE0051
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user