Город МОСКОВСКИЙ
00:08:08

Fade in and Fade Out in Unity3d || Fade Unity Tutorial || Game Development || Part 5

Аватар
JavaScript Concepts
Просмотры:
32
Дата загрузки:
29.11.2023 02:21
Длительность:
00:08:08
Категория:
Обучение

Описание

#UnityTutorial #SimpleUnityGame #unityfadeout
In this tutorial, I will explain how to fade in and fade out smoothly with Coroutine. If you haven’t checked my Coroutine Tutorial then you can check it https://www.youtube.com/channel/UCJxzX30xeLszdlma_0WVQLg/playlists?view_as=subscriber
Let us consider an example of fading an image in a specific time:
Step 1) Take a Canvas and a Panel inside it.
Step 2) Create an empty C# Script (I prefer coding in C#) and you can name it as you like.
Write the code in your script which I have explained in Video:
Step 3) Assign it to the panel and hit the play button in your Unity to see the magic.

I'll explain the magic; here FadeIn () and FadeOut() is a Coroutine where fading is performed. As I said earlier, you need to call the Coroutine with StartCoroutine() function.
As you can see from the code that Coroutine is invoked on pressing the button. When it is executed, it starts increasing alpha of the image bit by bit every frame until it's done.
It's almost like it started a parallel execution to fade the screen.
Here we have calculated the amount to increment (value of i) for each frame execution, i.e. how much should the Alfa be increased, depending upon the given time.
Thus, Alfa will increase a fixed amount every frame and hence create a proper illusion of a fading image
Oh, wait that’s not all! There is an option to delay the execution as per your wish. Isn't that great?
This can be achieved by using the WaitForSecond function. Isn’t it convenient? Well trust me, it is!
Here, execution waits for some time as specified in the function and resumes afterward. It is repeated until Coroutine finishes its execution.
Let’s again take the same example and change yield statement as shown in the code above. After changing in the line of yield, save it and push the play button in Unity.
Did you see any change in fading behavior?
Yes, the fading was slow and performed in intervals
Here WaitForSeconds() waits for a specified amount of time and then continues its execution again.
For better understanding, play around with different values to see the effect of WaitForSeconds(). WaitForSeconds() is very useful in sequencing animations and stuff.

Coroutine: https://www.youtube.com/playlist?list=PLP2NkilRRFwI_yvcSHgx4mOiWVropLb3F
Haunted Zombie: https://www.youtube.com/playlist?list=PLP2NkilRRFwI7qp4X5Pl77TYiJbeXSwZj


LinkedIn: https://www.linkedin.com/in/ali-rahman-71986a160/
Facebook: https://www.facebook.com/profile.php?id=100006673426314&ref=bookmarks
Twitter: https://twitter.com/AliRahm77694767

Рекомендуемые видео