Repository: UnityTechnologies/open-project-1
File: UOP1_Project/Assets/Scripts/UI/FadeManager.cs (C#)

Code before:
			yield return null;
		}
		_imageComponent.color = color; // Here to guarentee the image is exactly the requested color at the end of the loop.
		IsCurrentlyFading = false;
		yield return null;
	}

	/// <summary>
	/// Enumerators that fades out the canvas's imageComponent to turn the screen to normal gameplay color over time. Fadeouts called simeutaneously will only fade out the earliest call and discard any others.
	/// </summary>
	/// <param name="duration">How long it takes to the image to fade out.</param>
	/// <returns></returns>
	private IEnumerator FadeOutEnum(float duration)
	{
		Color oldColor = _imageComponent.color; // Temporarily stores the old color of the image component, as we can't assume the image will always be black.
		float totalTime = 0f; // Total amount of time this coroutine has taken. Determines when the fadeout will end and what color the imageComponent should be at every frame.
		while (totalTime <= duration)
		{
			totalTime += Time.deltaTime;
			_imageComponent.color = Color.Lerp(oldColor, new Color(0, 0, 0, 0), totalTime / duration); // Sets the image's color to a mixture between the old color and  total transparency, and interpolates based on the amount of time to completion.
			yield return null;
		}
		_imageComponent.color = new Color(0, 0, 0, 0); // Here to guarentee the image is fully transparent at the end of the loop.
		IsCurrentlyFading = false;
		yield return null;
	}

	private void Start()
	{
		_imageComponent = _fadeoutCanvas.GetComponent<Image>();
	}

	private void OnEnable()
	{
		if (_fadeChannelSO != null)
		{
			_fadeChannelSO.OnEventRaised += fadeGeneral;
		}
	}

	private void OnDisable()
	{
		if (_fadeChannelSO != null)
		{
			_fadeChannelSO.OnEventRaised += fadeGeneral;
		}
	}

	/// <summary>
	/// Controls the fade-in and fade-out.
	/// </summary>

Review comment (suggestion):
Here I said [in the Thread](https://forum.unity.com/threads/ui-fader-to-black.1038013/#post-6728329) that I would like to keep the ability to choose a FadeOut starting color

you could do this with a default value so that if you don't put anything as a parameter it automatically chooses the old color:
```suggestion
	private IEnumerator FadeOutEnum(float duration, Color color = default)
	{
		if (color == default) color = _imageComponent.color; // Stores the old color of the image component, as we can't assume the image will always be black, if no color is specified.
		float totalTime = 0f; // Total amount of time this coroutine has taken. Determines when the fadeout will end and what color the imageComponent should be at every frame.
		while (totalTime <= duration)
		{
			totalTime += Time.deltaTime;
			_imageComponent.color = Color.Lerp(color, new Color(0, 0, 0, 0), totalTime / duration); // Sets the image's color to a mixture between the old color and  total transparency, and interpolates based on the amount of time to completion.
			yield return null;
		}
		_imageComponent.color = new Color(0, 0, 0, 0); // Here to guarentee the image is fully transparent at the end of the loop.
		IsCurrentlyFading = false;
		yield return null;
	}
```

Code after:
		_imageComponent.color = color; // Here to guarentee the image is exactly the requested color at the end of the loop.
		IsCurrentlyFading = false;
		yield return null;
	}

	/// <summary>
	/// Enumerators that fades out the canvas's imageComponent to turn the screen to normal gameplay color over time. Fadeouts called simeutaneously will only fade out the earliest call and discard any others.
	/// </summary>
	/// <param name="duration">How long it takes to the image to fade out.</param>
	/// <returns></returns>
	private IEnumerator FadeOutEnum(float duration, Color color = default)
	{
		if (color == default)
			color = _imageComponent.color; // Stores the old color of the image component, as we can't assume the image will always be black, if no color is specified.
		float totalTime = 0f; // Total amount of time this coroutine has taken. Determines when the fadeout will end and what color the imageComponent should be at every frame.
		while (totalTime <= duration)
		{
			totalTime += Time.deltaTime;
			_imageComponent.color = Color.Lerp(color, new Color(0, 0, 0, 0), totalTime / duration); // Sets the image's color to a mixture between the old color and  total transparency, and interpolates based on the amount of time to completion.
			yield return null;
		}
		_imageComponent.color = new Color(0, 0, 0, 0); // Here to guarentee the image is fully transparent at the end of the loop.
		IsCurrentlyFading = false;
		yield return null;
	}

	private void OnEnable()
	{
		if (_fadeChannelSO != null)
		{
			_fadeChannelSO.OnEventRaised += fadeGeneral;
		}
	}

	private void OnDisable()
	{
		if (_fadeChannelSO != null)
		{
			_fadeChannelSO.OnEventRaised += fadeGeneral;
		}
	}

	/// <summary>
	/// Controls the fade-in and fade-out.
	/// </summary>
	/// <param name="fadeIn">If true, the rectangle fades in. If false, the rectangle fades out.</param>
	/// <param name="duration">How long it takes to the image to fade in/out.</param>
	/// <param name="color">Target color for the image to reach. Disregarded when fading out.</param>
	private void fadeGeneral(bool fadeIn, float duration, Color color)
	{
		if (!IsCurrentlyFading) // Makes sure multiple fade-ins or outs don't happen at the same time. Note this will mean fadeouts called at the same time will be discarded.