1
1
using System . Collections ;
2
2
using UnityEngine ;
3
3
using UnityEngine . InputSystem ;
4
- using UnityEngine . UI ;
5
4
6
5
public class CreditsMenu : MonoBehaviour
7
6
{
8
7
[ SerializeField ] private MainMenuController mainMenuController ;
9
8
10
- [ SerializeField ] private VerticalLayoutGroup content ;
9
+ [ SerializeField ] private RectTransform content ;
11
10
12
- [ SerializeField ] private float speed = 10 ;
11
+ [ SerializeField ] private RectTransform textContent ;
13
12
14
13
[ SerializeField ] private RectTransform lastElement ;
15
14
16
- private float height = 5000 ;
15
+ [ SerializeField ] private float speed = 10 ;
16
+
17
17
private int tween ;
18
- private int initialTopPadding ;
18
+ private float height ;
19
+ private float initialInset ;
19
20
20
21
private InputManager inputManager ;
21
22
22
23
private void OnEnable ( )
23
24
{
25
+ if ( inputManager )
26
+ {
27
+ inputManager . onCancel += Back ;
28
+ inputManager . onSelect += Back ;
29
+ inputManager . onClick += Back ;
30
+ }
31
+
24
32
StartCoroutine ( StartAnimation ( ) ) ;
25
33
}
26
34
27
35
private IEnumerator StartAnimation ( )
28
36
{
29
37
// Wait until position of last element can be determined.
30
38
yield return new WaitForEndOfFrame ( ) ;
31
- initialTopPadding = content . padding . top ;
32
- height = Mathf . Abs ( lastElement . anchoredPosition . y + lastElement . sizeDelta . y * .5f ) ;
39
+ initialInset = content . anchorMin . y ;
40
+ var diffBetweenContentAndText = Mathf . Abs ( content . anchoredPosition . y - textContent . anchoredPosition . y ) ;
41
+ height = diffBetweenContentAndText + Mathf . Abs ( lastElement . anchoredPosition . y + lastElement . sizeDelta . y * .5f ) ;
33
42
34
43
tween = LeanTween . sequence ( )
35
44
. append ( 1 )
@@ -45,23 +54,18 @@ private void StopAnimation()
45
54
LeanTween . cancel ( tween ) ;
46
55
// Doubly cancel since the first doesn't seem to work (?)
47
56
LeanTween . cancel ( content . gameObject ) ;
48
- content . padding . Remove ( new Rect ( ) ) ;
49
- content . padding . top = initialTopPadding ;
57
+ content . SetInsetAndSizeFromParentEdge ( RectTransform . Edge . Top , initialInset , content . rect . height ) ;
50
58
}
51
59
52
60
private void SetPosition ( float t )
53
61
{
54
- var topPadding = Mathf . RoundToInt ( initialTopPadding - height * t ) ;
55
- content . padding = new RectOffset ( content . padding . left , content . padding . right , topPadding ,
56
- content . padding . bottom ) ;
62
+ var inset = Mathf . RoundToInt ( initialInset - height * t ) ;
63
+ content . SetInsetAndSizeFromParentEdge ( RectTransform . Edge . Top , inset , content . rect . height ) ;
57
64
}
58
65
59
66
public void SetPlayerInput ( InputManager inputManager )
60
67
{
61
68
this . inputManager = inputManager ;
62
- inputManager . onCancel += Back ;
63
- inputManager . onSelect += Back ;
64
- inputManager . onClick += Back ;
65
69
}
66
70
67
71
private void Back ( InputAction . CallbackContext ctx )
0 commit comments