Skip to content

Commit 2a822ea

Browse files
committed
Reduce flickering in switching dialogs
1 parent 05d14d8 commit 2a822ea

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/UnityPackage/Assets/Middlewares/UiManager/UiManager.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private class ModalEntity
2727

2828
private List<ModalEntity> _modals = new List<ModalEntity>();
2929
private int _blackCurtainCount;
30+
private RectTransform _fadingOutBlackCurtain;
3031

3132
public static UiManager Instance { get; private set; }
3233

@@ -181,20 +182,32 @@ public UiDialogHandle ShowModalRoot<T>(object param = null,
181182

182183
private UiDialogHandle ShowModalInternal(UiDialog dialog, bool isTemporary, object param, ShowModalOption option)
183184
{
184-
float z = (_modals.Count + 2) * -10;
185-
186185
// create curtain for blocking input
187186

188-
var curtain = CreateCurtain(new Color(0, 0, 0, 0), _dialogRoot);
187+
RectTransform curtain = null;
188+
if ((option & ShowModalOption.BlackCurtain) != 0 && _fadingOutBlackCurtain != null)
189189
{
190-
curtain.SetSiblingIndex(dialog.GetComponent<RectTransform>().GetSiblingIndex());
190+
// When there is a curtain fading out, reuse it to reduce flickering
191191

192-
if ((option & ShowModalOption.BlackCurtain) != 0)
193-
{
194-
_blackCurtainCount += 1;
195-
var image = curtain.GetComponentInChildren<Image>();
196-
image.DOColor(new Color(0, 0, 0, 0.7f), 0.15f).SetUpdate(true);
197-
}
192+
curtain = _fadingOutBlackCurtain;
193+
DOTween.Kill(curtain.GetComponentInChildren<Image>());
194+
_fadingOutBlackCurtain = null;
195+
}
196+
else
197+
{
198+
curtain = CreateCurtain(new Color(0, 0, 0, 0), _dialogRoot);
199+
}
200+
201+
curtain.SetSiblingIndex(dialog.GetComponent<RectTransform>().GetSiblingIndex());
202+
203+
// unnecessary but it works as workaround for setting sibling index
204+
dialog.GetComponent<RectTransform>().SetSiblingIndex(curtain.GetSiblingIndex() + 1);
205+
206+
if ((option & ShowModalOption.BlackCurtain) != 0)
207+
{
208+
_blackCurtainCount += 1;
209+
var image = curtain.GetComponentInChildren<Image>();
210+
image.DOColor(new Color(0, 0, 0, 0.7f), 0.15f).SetUpdate(true);
198211
}
199212

200213
// fade in dialog
@@ -270,10 +283,16 @@ internal bool HideModal(UiDialog dialog, object returnValue)
270283
if ((entity.Option & ShowModalOption.BlackCurtain) != 0)
271284
{
272285
_blackCurtainCount -= 1;
286+
_fadingOutBlackCurtain = entity.Curtain;
273287

274288
var image = entity.Curtain.GetComponentInChildren<Image>();
275-
image.DOColor(new Color(0, 0, 0, 0), 0.1f).SetUpdate(true)
276-
.OnComplete(() => UnityEngine.Object.Destroy(entity.Curtain.gameObject));
289+
image.DOColor(new Color(0, 0, 0, 0), 0.1f).SetEase(Ease.InQuad).SetUpdate(true)
290+
.OnComplete(() =>
291+
{
292+
if (_fadingOutBlackCurtain == entity.Curtain)
293+
_fadingOutBlackCurtain = null;
294+
UnityEngine.Object.Destroy(entity.Curtain.gameObject);
295+
});
277296
}
278297
else
279298
{

0 commit comments

Comments
 (0)