Created
August 23, 2023 19:55
-
-
Save RimuruDev/758165d8428e57c03a89488bfe18ba07 to your computer and use it in GitHub Desktop.
⭐️My favorite and lazy class implementing game loading animation on Unity⭐️
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable All | |
// **************************************************************** // | |
// | |
// Copyright (c) RimuruDev. All rights reserved. | |
// Contact me: | |
// - Gmail: rimuru.dev@gmail.com | |
// - GitHub: https://github.com/RimuruDev | |
// - LinkedIn: https://www.linkedin.com/in/rimuru/ | |
// | |
// **************************************************************** // | |
using UnityEngine; | |
namespace RimuruDev.Internal.Codebase.Runtime.Curtain | |
{ | |
[CreateAssetMenu(menuName = "StaticData/Create CurtainConfig", fileName = "CurtainConfig", order = 0)] | |
public sealed class CurtainConfig : ScriptableObject | |
{ | |
[field: SerializeField] public CurtainView CurtainView { get; private set; } | |
[field: Space(20)] | |
[field: SerializeField] | |
public float HideDelay { get; private set; } = 1.3f; | |
[field: SerializeField] public float AnimationDuration { get; private set; } = 1.5f; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable All | |
// **************************************************************** // | |
// | |
// Copyright (c) RimuruDev. All rights reserved. | |
// Contact me: | |
// - Gmail: rimuru.dev@gmail.com | |
// - GitHub: https://github.com/RimuruDev | |
// - LinkedIn: https://www.linkedin.com/in/rimuru/ | |
// | |
// **************************************************************** // | |
using System; | |
using DG.Tweening; | |
using UnityEngine; | |
namespace RimuruDev.Internal.Codebase.Runtime.Curtain | |
{ | |
[SelectionBase] | |
[DisallowMultipleComponent] | |
public sealed class CurtainView : MonoBehaviour | |
{ | |
[SerializeField] private CanvasGroup canvasGroup; | |
private float animationDuration; | |
public void Constructor(float animationDuration) | |
{ | |
this.animationDuration = animationDuration; | |
DontDestroyOnLoad(this); | |
} | |
public void ShowCurtain(bool isAnimated, Action callback) | |
{ | |
canvasGroup.DOKill(); | |
gameObject.SetActive(true); | |
if (!isAnimated) | |
{ | |
canvasGroup.alpha = 1; | |
callback?.Invoke(); | |
return; | |
} | |
canvasGroup.alpha = 0; | |
canvasGroup | |
.DOFade(1, animationDuration) | |
.OnComplete(() => callback?.Invoke()); | |
} | |
public void HideCurtain(bool isAnimated, Action callback) | |
{ | |
canvasGroup.DOKill(); | |
if (!isAnimated) | |
{ | |
gameObject.SetActive(false); | |
callback?.Invoke(); | |
return; | |
} | |
canvasGroup | |
.DOFade(0, animationDuration) | |
.OnComplete(() => | |
{ | |
gameObject.SetActive(false); | |
callback?.Invoke(); | |
}); | |
} | |
public void HideCurtain(float startDelay, Action callback) | |
{ | |
canvasGroup | |
.DOFade(0, animationDuration) | |
.SetDelay(startDelay) | |
.OnComplete(() => | |
{ | |
gameObject.SetActive(false); | |
callback?.Invoke(); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable All | |
// **************************************************************** // | |
// | |
// Copyright (c) RimuruDev. All rights reserved. | |
// Contact me: | |
// - Gmail: rimuru.dev@gmail.com | |
// - GitHub: https://github.com/RimuruDev | |
// - LinkedIn: https://www.linkedin.com/in/rimuru/ | |
// | |
// **************************************************************** // | |
namespace RimuruDev.Internal.Codebase.Infrastructure.StateMachine.States | |
{ | |
public sealed class LoadMaiMenuState : IStateNext | |
{ | |
private readonly ICurtainService curtain; | |
private readonly ISceneLoaderService sceneLoader; | |
private readonly IStaticDataService staticData; | |
private readonly IUIFactory uiFactory; | |
private IGameStateMachine gameStateMachine; | |
[Inject] | |
public LoadMaiMenuState( | |
IStaticDataService staticData, | |
ICurtainService curtain, | |
IUIFactory uiFactory, | |
ISceneLoaderService sceneLoader) | |
{ | |
this.sceneLoader = sceneLoader; | |
this.staticData = staticData; | |
this.curtain = curtain; | |
this.uiFactory = uiFactory; | |
} | |
public void Init(IGameStateMachine gameStateMachine) => | |
this.gameStateMachine = gameStateMachine; | |
public void Enter() | |
{ | |
// *** Hide Curtain *** // | |
var config = staticData.ForCurtain(); | |
curtain.HideCurtain(config.HideDelay); | |
sceneLoader.LoadScene(SceneName.Gameplay, OnSceneLoaded); | |
} | |
public void Exit() {} | |
private void OnSceneLoaded() => | |
curtain.ShowCurtain(true, () => { gameStateMachine.EnterState<LoadGameplayState>(); }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment