Add project files.
This commit is contained in:
42
Assets/Cryville/Common/Unity/UI/LayoutAspectRatioFitter.cs
Normal file
42
Assets/Cryville/Common/Unity/UI/LayoutAspectRatioFitter.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Common.Unity.UI {
|
||||
public class LayoutAspectRatioFitter : MonoBehaviour {
|
||||
[SerializeField]
|
||||
private float m_aspectRatioPerElement = 1;
|
||||
public float AspectRatioPerElement {
|
||||
get { return m_aspectRatioPerElement; }
|
||||
set { m_aspectRatioPerElement = value; }
|
||||
}
|
||||
|
||||
AspectRatioFitter aspectRatioFitter;
|
||||
DockAspectRatioLayoutGroup syncTo;
|
||||
int axis;
|
||||
#pragma warning disable IDE0051
|
||||
void Awake() {
|
||||
aspectRatioFitter = GetComponent<AspectRatioFitter>();
|
||||
if (aspectRatioFitter == null) {
|
||||
syncTo = GetComponentInParent<DockAspectRatioLayoutGroup>();
|
||||
axis = (syncTo.DockSide == DockLayoutGroup.Side.Top || syncTo.DockSide == DockLayoutGroup.Side.Bottom) ? 1 : 0;
|
||||
}
|
||||
else axis = aspectRatioFitter.aspectMode == AspectRatioFitter.AspectMode.WidthControlsHeight ? 1 : 0;
|
||||
OnTransformChildrenChanged();
|
||||
}
|
||||
void OnTransformChildrenChanged() {
|
||||
float r;
|
||||
switch (axis) {
|
||||
case 0:
|
||||
r = AspectRatioPerElement * transform.childCount;
|
||||
break;
|
||||
case 1:
|
||||
r = AspectRatioPerElement / transform.childCount;
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
if (aspectRatioFitter != null) aspectRatioFitter.aspectRatio = r;
|
||||
else syncTo.DockAspectRatio = r;
|
||||
}
|
||||
#pragma warning restore IDE0051
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user