Video Fade In

hey,

i have a videoplayer attached to a quad that starts playing at the click of a button.

Id like the video to fade in.

How would i go about doing this?

Cheers

I didnt realise it was so difficult! 52 views!

Anyone fancy the challenge?

Well I don’t know much about the video player (never used it) and looking at the documentation for the video player in Unity I do not see any method you could use to fade, but you could put a sprite in front of the quad and fade that out.

I haven’t worked with the video player before but if you’re able to adjust the alpha of the material that the video render texture is assigned to I would write a little script that lerps it in the direction you need it to fade.

that was where i was stuck. Im not much of a coder, so that script is where id need help :slight_smile:

Any idea?

Here is my solution:
It works with NearPlane, FarPlane and RenderTexture.
Add the RenderTexture to the GameObject with the VideoPlayer.

        public static IEnumerator FadeVideoPlayerAlpha(UnityEngine.Video.VideoPlayer vp, FadeDirection fd, float fadeSpeed, Action<int> callback)
        {
            if (vp.renderMode == UnityEngine.Video.VideoRenderMode.CameraNearPlane || vp.renderMode == UnityEngine.Video.VideoRenderMode.CameraFarPlane || vp.renderMode == UnityEngine.Video.VideoRenderMode.RenderTexture)
            {
                RawImage rawImage = null;
                if (vp.renderMode == UnityEngine.Video.VideoRenderMode.RenderTexture)
                {
                    vp.gameObject.TryGetComponent<RawImage>(out rawImage);
                    if (!rawImage) Debug.LogWarning("No RawImage on the VideoPlayer GameObject found. -> (" + vp.gameObject.name + ")");
                }

                float alpha = (fd == FadeDirection.Out) ? 1f : 0f;
                float fadeEndValue = (fd == FadeDirection.Out) ? 0f : 1f;

                if (fd == FadeDirection.Out)
                {
                    Debug.Log("Stop Video");
                    while (alpha >= fadeEndValue)
                    {
                        alpha -= Time.deltaTime * fadeSpeed;
                        if (rawImage) rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, alpha);
                        else vp.targetCameraAlpha = alpha;
                        yield return null;
                    }
                    vp.Stop();
                    if (rawImage) rawImage.enabled = false;
                }
                else
                {
                    Debug.Log("Play Video");

                    //Make sure alpha is 0
                    if (rawImage) rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, alpha);
                    else vp.targetCameraAlpha = alpha;

                    //Enable the RawImage and start the player
                    if (rawImage) rawImage.enabled = true;
                    vp.Play();

                    //Delay - to make sure the Image has the correct Texture
                    yield return new WaitForSeconds(0.1f);

                    while (alpha <= fadeEndValue)
                    {
                        alpha += Time.deltaTime * fadeSpeed;
                        if (rawImage) rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, alpha);
                        else vp.targetCameraAlpha = alpha;
                        yield return null;
                    }
                }
                callback((int)fadeEndValue);
            }
            else
            {
                Debug.LogWarning("VideoRenderMode (for alpha) must be RenderTexture, CameraFarPlane or CameraNearPlane. GameObject -> (" + vp.gameObject.name + ")");
            }
        }

How to use:

//Fade Video In
StartCoroutine(VideoFader.FadeVideoPlayerAlpha(videoPlayer, VideoFader.FadeDirection.In, videoFadeTime, (endValue)=>
{
    Debug.Log("Fade Complete. Alpha is " + endValue.ToString());
}));

Edit:
I made some changes to fix the image flickering if you play a new clip

1 Like