RenderTexture retaining last video player frame

Hi,

My problem was that my videos also kept the last frame in their target texture. When using:

videoPlayer.targetTexture.Release();

this caused the texture to just be a black square which is not a good starting point for videos with an alpha. (or a camera not set up to respect it)

This is what has worked for me to resolve it:

I made a random image in blender with an alpha layer. (A simple square with no color) and then imported it into unity.

code on the video player object:

public Texture Blank;

void Awake()
{
    videoPlayer.targetTexture.DiscardContents();
    videoPlayer.targetTexture.Release();

    Graphics.Blit(Blank, videoPlayer.targetTexture);
}

(Drag the blank image to the Blank variable in inspector)

This forces the target texture to the alpha texture (i.e nothing) and then when you call videoPlayer.Play() the next frame to be written to the target texture is the first frame of the video (which is the desired result).

Every time you load the scene the video player game object will blank the last frame with the alpha texture.

Word of warning unity will use a screen shot of the game view for the texture if you forget to drag the blank image in.

Again this is what worked for me, I hope it helps.

Adding “videoPlayer.targetTexture.Release();” after video (or whenever I stop it) like ookk4700 suggested worked for me.

1 Like

Working well, thanks!

1 Like

Work perfect for me, thanks!!

1 Like

targetTexture.Release() works in PrepareVideo method in VideoPlayerController script that is subbed to VideoPlayer:

IEnumerator PrepareVideo()
        {
            yield return new WaitForEndOfFrame();
            _videoPlayer.Prepare();
            _videoPlayer.targetTexture.Release();       //remove last frame of previously played video
        }

Not sure why this method works, but the last frame gets removed and its not played the second time I play the video. Don’t forget to add StartCoroutine(PrepareVideo()) in SetVideo method

1 Like

This is a working solution, but not quite the right one.

Thanks you ! It’s work perfectly !

1 Like

This might be late, but I think I have found a solution to the problem!
I added the rendered texture of the video player onto a raw image so that I can display the video on the raw image. After that all you have to do is just simply set the “enable” property to false on the raw image when the video player is at frame 0, and then change it to true when the frame has passed 0. Like this:

using UnityEngine.Video;
using UnityEngine.UI;

public RawImage videoDisplay;
public VideoPlayer videoPlayer;

private void Update()
{
        if (videoPlayer.frame < 0)
        {
            videoDisplay.enabled = false;
        }
        else if (videoPlayer.frame > 0)
        {
            videoDisplay.enabled = true;
        }
}

Hope this helps!

You can use Color.clear to make it transparent… that’s what I was here for… thanks!
Never thought a solution from Sept 2018 would help me in Nov 2024