Tanoda
AccordionElement.cs
Go to the documentation of this file.
1
3
4using System;
6
8{
9 [RequireComponent(typeof(RectTransform), typeof(LayoutElement))]
10 [AddComponentMenu("UI/Extensions/Accordion/Accordion Element")]
11 public class AccordionElement : Toggle
12 {
13
14 [SerializeField] private float m_MinHeight = 18f;
15
16 private Accordion m_Accordion;
17 private RectTransform m_RectTransform;
18 private LayoutElement m_LayoutElement;
19
20 [NonSerialized]
21 private readonly TweenRunner<FloatTween> m_FloatTweenRunner;
22
23 protected AccordionElement()
24 {
25 if (this.m_FloatTweenRunner == null)
26 this.m_FloatTweenRunner = new TweenRunner<FloatTween>();
27
28 this.m_FloatTweenRunner.Init(this);
29 }
30
31 protected override void Awake()
32 {
33 base.Awake();
34 base.transition = Transition.None;
35 base.toggleTransition = ToggleTransition.None;
36 this.m_Accordion = this.gameObject.GetComponentInParent<Accordion>();
37 this.m_RectTransform = this.transform as RectTransform;
38 this.m_LayoutElement = this.gameObject.GetComponent<LayoutElement>();
39 this.onValueChanged.AddListener(OnValueChanged);
40 }
41
42#if UNITY_EDITOR
43 protected override void OnValidate()
44 {
45 base.OnValidate();
46
47 if (this.group == null)
48 {
49 ToggleGroup tg = this.GetComponentInParent<ToggleGroup>();
50
51 if (tg != null)
52 {
53 this.group = tg;
54 }
55 }
56
57 LayoutElement le = this.gameObject.GetComponent<LayoutElement>();
58
59 if (le != null)
60 {
61 if (this.isOn)
62 {
63 le.preferredHeight = -1f;
64 }
65 else
66 {
67 le.preferredHeight = this.m_MinHeight;
68 }
69 }
70 }
71#endif
72
73 public void OnValueChanged(bool state)
74 {
75 if (this.m_LayoutElement == null)
76 return;
77
78 Accordion.Transition transition = (this.m_Accordion != null) ? this.m_Accordion.transition : Accordion.Transition.Instant;
79
80 if (transition == Accordion.Transition.Instant)
81 {
82 if (state)
83 {
84 this.m_LayoutElement.preferredHeight = -1f;
85 }
86 else
87 {
88 this.m_LayoutElement.preferredHeight = this.m_MinHeight;
89 }
90 }
91 else if (transition == Accordion.Transition.Tween)
92 {
93 if (state)
94 {
95 this.StartTween(this.m_MinHeight, this.GetExpandedHeight());
96 }
97 else
98 {
99 this.StartTween(this.m_RectTransform.rect.height, this.m_MinHeight);
100 }
101 }
102 }
103
104 protected float GetExpandedHeight()
105 {
106 if (this.m_LayoutElement == null)
107 return this.m_MinHeight;
108
109 float originalPrefH = this.m_LayoutElement.preferredHeight;
110 this.m_LayoutElement.preferredHeight = -1f;
111 float h = LayoutUtility.GetPreferredHeight(this.m_RectTransform);
112 this.m_LayoutElement.preferredHeight = originalPrefH;
113
114 return h;
115 }
116
117 protected void StartTween(float startFloat, float targetFloat)
118 {
119 float duration = (this.m_Accordion != null) ? this.m_Accordion.transitionDuration : 0.3f;
120
121 FloatTween info = new FloatTween
122 {
123 duration = duration,
124 startFloat = startFloat,
125 targetFloat = targetFloat
126 };
128 info.ignoreTimeScale = true;
129 this.m_FloatTweenRunner.StartTween(info);
130 }
131
132 protected void SetHeight(float height)
133 {
134 if (this.m_LayoutElement == null)
135 return;
136
137 this.m_LayoutElement.preferredHeight = height;
138 }
139 }
140}
void StartTween(float startFloat, float targetFloat)
float transitionDuration
Gets or sets the duration of the transition.
Definition: Accordion.cs:36
Transition transition
Gets or sets the transition.
Definition: Accordion.cs:26
Credit Erdener Gonenc - @PixelEnvision.
void AddOnChangedCallback(UnityAction< float > callback)
Adds a on changed callback.
Definition: FloatTween.cs:76