Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Playback lag with VideoPlayer rendering to RenderTexture for VisualElement BackgroundImage

Discussion in 'Scripting' started by infinitejess, May 14, 2024.

  1. infinitejess

    infinitejess

    Joined:
    Jan 29, 2019
    Posts:
    13
    Hi,

    I'm implementing some UI screens with the UI toolkit. Several of the screens have looping video backdrops. I did some research and it seemed like the best (only?) way to accomplish this is by using a VideoPlayer component to render the VideoClip to a render texture, and then use that as the VisualElement's BackgroundImage. I've implemented this a few ways. Aside from struggling with some issues transitioning from one clip to another without any stutter or frame loss, I've mostly got it working by doing this:
    - Create VideoPlayer and blank RenderTexture at runtime
    - Assign clip and new RenderTexture to the VideoPlayer
    - Call VideoPlayer.Prepare()
    - Assign the RenderTexture to VisualElement.BackgroundImage
    - Call VideoPlayer.Play() when the player opens the screen.

    But I'm noticing now that the videos seem to have reduced performance in full screen. The native videos are 1920x1080 webm files. When I run the game in editor with that resolution, it runs buttery smooth. But when I set the game mode aspect ratio to anything else, like 16:9 or free aspect ratio, I'm seeing degraded performance. It's not awful, but definitely choppier. I'm not so knowledgeable about video, my background is in engineering, and struggling to figure out how to resolve this.

    Would this maybe be a result of the VideoPlayer rendering to an RT? Or something about the webm file? Or just a result of degraded gpu performance?

    I have a 2023 Macbook Pro M2 with 32g ram, and I'm running Unity 2022.3.4f1. The smallest video it happens with is about 20mb.