└── IDisposables ├── DotNetDisposables ├── DisposableGeneric.cs ├── DisposableLogStopwatch.cs └── DisposableSetBoolean.cs └── UnityDisposables ├── DisposableAddListener.cs ├── DisposableEnableComponent.cs ├── DisposableFreezeTime.cs ├── DisposableLockButton.cs ├── DisposableLockUIInput.cs ├── DisposablePlayAnimation.cs ├── DisposablePlayEnterExitAnimation.cs └── DisposableSetGameObjectActive.cs /IDisposables/DotNetDisposables/DisposableGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Utils.IDisposableUtils 4 | { 5 | public class DisposableGeneric : IDisposable 6 | { 7 | private readonly Action _onEndAction; 8 | 9 | public DisposableGeneric(Action onStartAction, Action onEndAction) 10 | { 11 | onStartAction?.Invoke(); 12 | _onEndAction = onEndAction; 13 | } 14 | 15 | public void Dispose() 16 | { 17 | _onEndAction?.Invoke(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /IDisposables/DotNetDisposables/DisposableLogStopwatch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableLogStopwatch : IDisposable 7 | { 8 | private readonly Stopwatch _stopwatch; 9 | 10 | public DisposableLogStopwatch() 11 | { 12 | _stopwatch = new Stopwatch(); 13 | _stopwatch.Start(); 14 | } 15 | 16 | public void Dispose() 17 | { 18 | _stopwatch.Stop(); 19 | var elapsedMS = _stopwatch.ElapsedMilliseconds; 20 | Console.WriteLine($"took: {elapsedMS} ms"); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /IDisposables/DotNetDisposables/DisposableSetBoolean.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Utils.IDisposableUtils 4 | { 5 | public class DisposableSetBoolean : IDisposable 6 | { 7 | private readonly Action _action; 8 | private readonly bool _startingValue; 9 | 10 | public DisposableSetBoolean(Action action, bool startingValue) 11 | { 12 | _action = action; 13 | _startingValue = startingValue; 14 | _action?.Invoke(startingValue); 15 | } 16 | 17 | public void Dispose() 18 | { 19 | _action?.Invoke(!_startingValue); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableAddListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine.Events; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableAddListener : IDisposable 7 | { 8 | private readonly UnityEvent _eventHandler; 9 | private readonly UnityAction _eventAction; 10 | 11 | public DisposableAddListener(UnityEvent eventHandler, UnityAction eventAction) 12 | { 13 | _eventHandler = eventHandler; 14 | _eventAction = eventAction; 15 | _eventHandler.AddListener(_eventAction); 16 | } 17 | 18 | public DisposableAddListener(UnityEvent eventHandler, Action eventAction) 19 | { 20 | _eventHandler = eventHandler; 21 | _eventAction = new UnityAction(eventAction); 22 | _eventHandler.AddListener(_eventAction); 23 | } 24 | 25 | public void Dispose() 26 | { 27 | _eventHandler.RemoveListener(_eventAction); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableEnableComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableEnableComponent : IDisposable 7 | { 8 | private readonly Behaviour _component; 9 | private readonly bool _isStartingEnabled; 10 | 11 | public DisposableEnableComponent(Behaviour component, bool isStartingEnabled = true) 12 | { 13 | _component = component; 14 | _isStartingEnabled = isStartingEnabled; 15 | _component.enabled = _isStartingEnabled; 16 | } 17 | 18 | public void Dispose() 19 | { 20 | _component.enabled = !_isStartingEnabled; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableFreezeTime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableFreezeTime : IDisposable 7 | { 8 | private readonly float _previousTimeScale; 9 | 10 | public DisposableFreezeTime() 11 | { 12 | _previousTimeScale = Time.timeScale; 13 | Time.timeScale = 0f; 14 | } 15 | 16 | public void Dispose() 17 | { 18 | Time.timeScale = _previousTimeScale; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableLockButton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine.UI; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableLockButton : IDisposable 7 | { 8 | private readonly Button _button; 9 | private readonly bool _isStartingLocked; 10 | 11 | public DisposableLockButton(Button button , bool isStartingLocked = true) 12 | { 13 | _button = button; 14 | _isStartingLocked = isStartingLocked; 15 | _button.enabled = !_isStartingLocked; 16 | } 17 | 18 | public void Dispose() 19 | { 20 | _button.enabled = _isStartingLocked; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableLockUIInput.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine.UI; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableLockUIInput : IDisposable 7 | { 8 | private readonly Graphic _graphic; 9 | 10 | public DisposableLockUIInput(Graphic graphic) 11 | { 12 | _graphic = graphic; 13 | graphic.raycastTarget = false; 14 | } 15 | 16 | public void Dispose() 17 | { 18 | _graphic.raycastTarget = true; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposablePlayAnimation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposablePlayAnimation : IDisposable 7 | { 8 | private readonly Animation _animation; 9 | private readonly Animator _animator; 10 | 11 | public DisposablePlayAnimation(Animation animation, AnimationClip animationClip) 12 | { 13 | _animation = animation; 14 | _animation.Play(animationClip.name); 15 | } 16 | 17 | public DisposablePlayAnimation(Animator animator, int stateNameHash) 18 | { 19 | _animator = animator; 20 | _animator.enabled = true; 21 | _animator.Play(stateNameHash); 22 | } 23 | 24 | public void Dispose() 25 | { 26 | if (_animation != null) 27 | { 28 | _animation.Stop(); 29 | } 30 | 31 | if (_animator != null) 32 | { 33 | _animator.enabled = false; 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposablePlayEnterExitAnimation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposablePlayEnterExitAnimation : IDisposable 7 | { 8 | private readonly Animation _animation; 9 | private readonly Animator _animator; 10 | private readonly AnimationClip _exitAnimationClip; 11 | private readonly int _exitStateNameHash; 12 | 13 | public DisposablePlayEnterExitAnimation(Animation animation, AnimationClip enterAnimationClip, AnimationClip exitAnimationClip) 14 | { 15 | _animation = animation; 16 | _exitAnimationClip = exitAnimationClip; 17 | _animation.Play(enterAnimationClip.name); 18 | } 19 | 20 | public DisposablePlayEnterExitAnimation(Animator animator, int enterStateNameHash, int exitStateNameHash) 21 | { 22 | _animator = animator; 23 | _exitStateNameHash = exitStateNameHash; 24 | _animator.Play(enterStateNameHash); 25 | } 26 | 27 | public void Dispose() 28 | { 29 | if (_animation != null) 30 | { 31 | _animation.Play(_exitAnimationClip.name); 32 | } 33 | 34 | if (_animator != null) 35 | { 36 | _animator.Play(_exitStateNameHash); 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /IDisposables/UnityDisposables/DisposableSetGameObjectActive.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | 4 | namespace Utils.IDisposableUtils 5 | { 6 | public class DisposableSetGameObjectActive : IDisposable 7 | { 8 | private readonly GameObject _gameObject; 9 | private readonly bool _isStartingActive; 10 | 11 | public DisposableSetGameObjectActive(GameObject gameObject, bool isStartingActive = true) 12 | { 13 | _gameObject = gameObject; 14 | _isStartingActive = isStartingActive; 15 | _gameObject.SetActive(_isStartingActive); 16 | } 17 | 18 | public void Dispose() 19 | { 20 | _gameObject.SetActive(!_isStartingActive); 21 | } 22 | } 23 | } --------------------------------------------------------------------------------