I have a Unity VideoPlayer where I use a render texture with an audio source to play the video. VideoPlayer.Stop
pauses the video and resets the time to 0. I want to reset the VideoPlayer back to its default after I stop the video player. I searched the Scripting API but I did not see anything obvious on how to do this. How do I do this?
-
\$\begingroup\$ Could you explain a little more what do you mean by "I want to reset the VideoPlayer back to its default", do you mean like removing the video and showing nothing? \$\endgroup\$– Tom TsagkatosCommented Jun 1, 2018 at 11:10
-
\$\begingroup\$ Yes. You said it better than I did. When the application starts it shows a black screen. When I stop a video I want the screen to clear out and go to black as before. When I do a VideoPlayer.Stop command the video player pauses the video at the point where the command was executed. \$\endgroup\$– Pamela Cook - LightBe CorpCommented Jun 1, 2018 at 11:52
4 Answers
I did a quick test on my machine, a quick solution to that would be to disable the VideoPlayer
component by calling myVideoPlayer.enabled = false
. This would stop rending the video, and render the rest of the object. If you want to re-use it you can re-enable it the same way.
You can edit the object's material to change what the object looks like when the video is off.
-
\$\begingroup\$ I used steps from a YouTube video to create my video player with a render texture using a Sprites/Default shader. The tint color is white. If I change the color or the alpha I no longer see the video. The shader material takes on what the last image on the player is. When I look at the material initially the box to the top left is black. After I play a video the box is updated to where the last video stopped. Disabling & enabling the video player didn't work as a result. I will search to find how to change that material color after I stop the video player. \$\endgroup\$ Commented Jun 1, 2018 at 15:19
-
\$\begingroup\$ @PamelaCook-LightBeCorp Out of curiosity, why didn't you just add a
VideoPlayer
component to a regular game object? Is there a special reason/feature you were going after? \$\endgroup\$ Commented Jun 1, 2018 at 15:26 -
\$\begingroup\$ Here is the video I used youtube.com/watch?v=V8rwCWiRLWI. In this video they attached a video player to a empty game object. I attached a video player to a canvas that is against a 'wall'. I want a person to go to the wall & watch the video (play/pause). When a person leaves to go to another section of my application I want the video to stop and go back to a blank screen. I did another search using different words and found this is a problem others are having but so far has not been solved. I found one link on a Unity forum about this. \$\endgroup\$ Commented Jun 1, 2018 at 16:30
I ended up going on to other projects, but found myself needing a solution to this issue again. It took me a few hours, but I was able to find a solution. I wrote a script to use the render texture I wanted to reset as input.
public void ClearOutRenderTexture(RenderTexture renderTexture)
{
RenderTexture rt = RenderTexture.active;
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = rt;
}
I found the solution here in the Unity forum.
-
\$\begingroup\$ Don't forget to mark this answer as "Accepted" if it worked for you, by clicking the checkmark icon on the left. \$\endgroup\$– DMGregory ♦Commented Nov 17, 2023 at 11:57
You can also clear out the content of the render texture. Keep a reference of your render texture and when you stop the player you can just
renderTexture.Release();
[ContextMenu(nameof(Clear))]
private void Clear()
{
if (rt != null)
{
var cmd = new CommandBuffer();
cmd.SetRenderTarget(rt);
cmd.ClearRenderTarget(true, true, Color.clear);
Graphics.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
}