Update to force Enumerated start for Accordion elements

Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/455
pull/460/head
SimonDarksideJ 2023-11-26 13:41:48 +00:00
parent b615c4b681
commit 9c7a59c9f2
1 changed files with 182 additions and 174 deletions

View File

@ -2,217 +2,225 @@
///Sourced from - http://forum.unity3d.com/threads/accordion-type-layout.271818/ ///Sourced from - http://forum.unity3d.com/threads/accordion-type-layout.271818/
using System; using System;
using System.Collections;
using UnityEngine.UI.Extensions.Tweens; using UnityEngine.UI.Extensions.Tweens;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(RectTransform), typeof(LayoutElement))] [RequireComponent(typeof(RectTransform), typeof(LayoutElement))]
[AddComponentMenu("UI/Extensions/Accordion/Accordion Element")] [AddComponentMenu("UI/Extensions/Accordion/Accordion Element")]
public class AccordionElement : Toggle public class AccordionElement : Toggle
{ {
[SerializeField] private float m_MinHeight = 18f; [SerializeField] private float m_MinHeight = 18f;
public float MinHeight => m_MinHeight; public float MinHeight => m_MinHeight;
[SerializeField] private float m_MinWidth = 40f; [SerializeField] private float m_MinWidth = 40f;
public float MinWidth => m_MinWidth; public float MinWidth => m_MinWidth;
private Accordion m_Accordion; private Accordion m_Accordion;
private RectTransform m_RectTransform; private RectTransform m_RectTransform;
private LayoutElement m_LayoutElement; private LayoutElement m_LayoutElement;
[NonSerialized] [NonSerialized]
private readonly TweenRunner<FloatTween> m_FloatTweenRunner; private readonly TweenRunner<FloatTween> m_FloatTweenRunner;
protected AccordionElement() protected AccordionElement()
{ {
if (this.m_FloatTweenRunner == null) if (this.m_FloatTweenRunner == null)
this.m_FloatTweenRunner = new TweenRunner<FloatTween>(); this.m_FloatTweenRunner = new TweenRunner<FloatTween>();
this.m_FloatTweenRunner.Init(this); this.m_FloatTweenRunner.Init(this);
} }
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
base.transition = Transition.None; base.transition = Transition.None;
base.toggleTransition = ToggleTransition.None; base.toggleTransition = ToggleTransition.None;
this.m_Accordion = this.gameObject.GetComponentInParent<Accordion>(); this.m_Accordion = this.gameObject.GetComponentInParent<Accordion>();
this.m_RectTransform = this.transform as RectTransform; this.m_RectTransform = this.transform as RectTransform;
this.m_LayoutElement = this.gameObject.GetComponent<LayoutElement>(); this.m_LayoutElement = this.gameObject.GetComponent<LayoutElement>();
this.onValueChanged.AddListener(OnValueChanged); this.onValueChanged.AddListener(OnValueChanged);
} }
private new IEnumerator Start()
{
base.Start();
yield return new WaitForEndOfFrame(); // Wait for the first frame
OnValueChanged(this.isOn);
}
#if UNITY_EDITOR #if UNITY_EDITOR
protected override void OnValidate() protected override void OnValidate()
{ {
base.OnValidate(); base.OnValidate();
this.m_Accordion = this.gameObject.GetComponentInParent<Accordion>(); this.m_Accordion = this.gameObject.GetComponentInParent<Accordion>();
if (this.group == null) if (this.group == null)
{ {
ToggleGroup tg = this.GetComponentInParent<ToggleGroup>(); ToggleGroup tg = this.GetComponentInParent<ToggleGroup>();
if (tg != null)
{
this.group = tg;
}
}
LayoutElement le = this.gameObject.GetComponent<LayoutElement>();
if (le != null && m_Accordion != null) if (tg != null)
{ {
if (this.isOn) this.group = tg;
{ }
}
LayoutElement le = this.gameObject.GetComponent<LayoutElement>();
if (le != null && m_Accordion != null)
{
if (this.isOn)
{
if (m_Accordion.ExpandVerticval) if (m_Accordion.ExpandVerticval)
{ {
le.preferredHeight = -1f; le.preferredHeight = -1f;
}
else
{
le.preferredWidth = -1f;
} }
}
else
{
if (m_Accordion.ExpandVerticval)
{
le.preferredHeight = this.m_MinHeight;
}
else else
{ {
le.preferredWidth = this.m_MinWidth; le.preferredWidth = -1f;
}
}
else
{
if (m_Accordion.ExpandVerticval)
{
le.preferredHeight = this.m_MinHeight;
}
else
{
le.preferredWidth = this.m_MinWidth;
} }
} }
} }
} }
#endif #endif
public void OnValueChanged(bool state) public void OnValueChanged(bool state)
{ {
if (this.m_LayoutElement == null) if (this.m_LayoutElement == null)
return; return;
Accordion.Transition transition = (this.m_Accordion != null) ? this.m_Accordion.transition : Accordion.Transition.Instant;
if (transition == Accordion.Transition.Instant && m_Accordion != null) Accordion.Transition transition = (this.m_Accordion != null) ? this.m_Accordion.transition : Accordion.Transition.Instant;
{
if (state) if (transition == Accordion.Transition.Instant && m_Accordion != null)
{ {
if (m_Accordion.ExpandVerticval) if (state)
{ {
this.m_LayoutElement.preferredHeight = -1f; if (m_Accordion.ExpandVerticval)
} {
this.m_LayoutElement.preferredHeight = -1f;
}
else else
{ {
this.m_LayoutElement.preferredWidth = -1f; this.m_LayoutElement.preferredWidth = -1f;
} }
} }
else else
{ {
if (m_Accordion.ExpandVerticval) if (m_Accordion.ExpandVerticval)
{ {
this.m_LayoutElement.preferredHeight = this.m_MinHeight; this.m_LayoutElement.preferredHeight = this.m_MinHeight;
} }
else else
{ {
this.m_LayoutElement.preferredWidth = this.m_MinWidth; this.m_LayoutElement.preferredWidth = this.m_MinWidth;
} }
} }
} }
else if (transition == Accordion.Transition.Tween) else if (transition == Accordion.Transition.Tween)
{ {
if (state) if (state)
{ {
if (m_Accordion.ExpandVerticval) if (m_Accordion.ExpandVerticval)
{ {
this.StartTween(this.m_MinHeight, this.GetExpandedHeight()); this.StartTween(this.m_MinHeight, this.GetExpandedHeight());
} }
else else
{ {
this.StartTween(this.m_MinWidth, this.GetExpandedWidth()); this.StartTween(this.m_MinWidth, this.GetExpandedWidth());
} }
} }
else else
{ {
if (m_Accordion.ExpandVerticval) if (m_Accordion.ExpandVerticval)
{ {
this.StartTween(this.m_RectTransform.rect.height, this.m_MinHeight); this.StartTween(this.m_RectTransform.rect.height, this.m_MinHeight);
} }
else else
{ {
this.StartTween(this.m_RectTransform.rect.width, this.m_MinWidth); this.StartTween(this.m_RectTransform.rect.width, this.m_MinWidth);
} }
} }
} }
} }
protected float GetExpandedHeight()
{
if (this.m_LayoutElement == null)
return this.m_MinHeight;
float originalPrefH = this.m_LayoutElement.preferredHeight;
this.m_LayoutElement.preferredHeight = -1f;
float h = LayoutUtility.GetPreferredHeight(this.m_RectTransform);
this.m_LayoutElement.preferredHeight = originalPrefH;
return h;
}
protected float GetExpandedWidth() protected float GetExpandedHeight()
{ {
if (this.m_LayoutElement == null) if (this.m_LayoutElement == null)
return this.m_MinWidth; return this.m_MinHeight;
float originalPrefW = this.m_LayoutElement.preferredWidth; float originalPrefH = this.m_LayoutElement.preferredHeight;
this.m_LayoutElement.preferredWidth = -1f; this.m_LayoutElement.preferredHeight = -1f;
float w = LayoutUtility.GetPreferredWidth(this.m_RectTransform); float h = LayoutUtility.GetPreferredHeight(this.m_RectTransform);
this.m_LayoutElement.preferredWidth = originalPrefW; this.m_LayoutElement.preferredHeight = originalPrefH;
return w; return h;
} }
protected void StartTween(float startFloat, float targetFloat) protected float GetExpandedWidth()
{ {
float duration = (this.m_Accordion != null) ? this.m_Accordion.transitionDuration : 0.3f; if (this.m_LayoutElement == null)
return this.m_MinWidth;
FloatTween info = new FloatTween
{ float originalPrefW = this.m_LayoutElement.preferredWidth;
duration = duration, this.m_LayoutElement.preferredWidth = -1f;
startFloat = startFloat, float w = LayoutUtility.GetPreferredWidth(this.m_RectTransform);
targetFloat = targetFloat this.m_LayoutElement.preferredWidth = originalPrefW;
};
if (m_Accordion.ExpandVerticval) return w;
{ }
info.AddOnChangedCallback(SetHeight);
} protected void StartTween(float startFloat, float targetFloat)
{
float duration = (this.m_Accordion != null) ? this.m_Accordion.transitionDuration : 0.3f;
FloatTween info = new FloatTween
{
duration = duration,
startFloat = startFloat,
targetFloat = targetFloat
};
if (m_Accordion.ExpandVerticval)
{
info.AddOnChangedCallback(SetHeight);
}
else else
{ {
info.AddOnChangedCallback(SetWidth); info.AddOnChangedCallback(SetWidth);
} }
info.ignoreTimeScale = true; info.ignoreTimeScale = true;
this.m_FloatTweenRunner.StartTween(info); this.m_FloatTweenRunner.StartTween(info);
} }
protected void SetHeight(float height)
{
if (this.m_LayoutElement == null)
return;
this.m_LayoutElement.preferredHeight = height;
}
protected void SetWidth(float width) protected void SetHeight(float height)
{ {
if (this.m_LayoutElement == null) if (this.m_LayoutElement == null)
return; return;
this.m_LayoutElement.preferredWidth = width; this.m_LayoutElement.preferredHeight = height;
} }
}
protected void SetWidth(float width)
{
if (this.m_LayoutElement == null)
return;
this.m_LayoutElement.preferredWidth = width;
}
}
} }