-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUIGroup.cs
137 lines (81 loc) · 2.44 KB
/
CUIGroup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace CUI {
/// <summary>
/// CUIViews are groups of different UI components, and can include UIPanels for "sub-menus"
/// </summary>
[RequireComponent(typeof(CanvasGroup))]
public class CUIGroup : MonoBehaviour {
[Header("CUI Configuration")]
[SerializeField] private bool hideAtStart = false;
[SerializeField] public bool disableCanvasWhenHidden = true;
[Header("Animation Settings")]
[SerializeField] private bool childGroupsFollowState = false;
[SerializeField] public CUIAnimation showingAnimation = CUIAnimation.FadeIn;
[SerializeField] public CUIAnimation hidingAnimation = CUIAnimation.FadeOut;
private bool isFirstOpen = true;
private List<CUIGroup> childGroups = new List<CUIGroup>();
private RectTransform _rectTransform;
public RectTransform rectTransform {
get {
if (_rectTransform == null) _rectTransform = GetComponent<RectTransform>();
return _rectTransform;
}
}
[HideInInspector] public bool isVisible = false;
private CanvasGroup _canvasGroup;
public CanvasGroup canvasGroup {
get {
if (_canvasGroup == null) _canvasGroup = GetComponent<CanvasGroup>();
return _canvasGroup;
}
}
protected virtual void Start() {
RefreshChildGroups();
if (hideAtStart) StartCoroutine(HideGOAtStartDelayed());
else OnShowing();
}
private IEnumerator HideGOAtStartDelayed() {
yield return null;
gameObject.SetActive(false);
}
public void RefreshChildGroups() {
childGroups.Clear();
childGroups = GetComponentsInChildren<CUIGroup>().ToList();
if (childGroups.Contains(this)) childGroups.Remove(this);
}
public virtual void OnInit() {
isFirstOpen = false;
}
public virtual void OnShowing() {
if (isFirstOpen) OnInit();
if (isVisible) return;
isVisible = true;
if (childGroupsFollowState) {
//Trigger events in children
foreach (CUIGroup group in childGroups) {
group.OnShowing();
}
}
}
public virtual void OnHiding() {
if (!isVisible) return;
isVisible = false;
if (childGroupsFollowState) {
//Trigger events in children
foreach (CUIGroup group in childGroups) {
group.OnHiding();
}
}
}
public void Hide() {
CUIManager.Animate(this, false);
}
public void Show() {
CUIManager.Animate(this, true);
}
}
}