How to play a YouTube video in Unity

I want to play a YouTube video inside my Unity project, but I don’t know how to do it. Would anyone know how to achieve this, and if so, could anyone point me in the direction to achieve this goal.

So far I’ve tried two methods only to be met with failure. The first one was to parse through the https link of the video to get an mp4 link and play the video in the video player by adding the URL. The second one was to input a vimeo URL, but that doesn’t work either.

Would anyone know how to achieve this?

You would need to integrate the official Youtube API and last I checked it does not let you play the videos.

They let you upload, modify, delete, annotate, etc., basically everything except playback.

Any other way of playing Youtube videos with your own code and data fetching is likely going to be a violation of their Terms of Service.

Or just use Application.OpenURL() on the video link itself in a browser, all within Youtube’s normal Terms Of Service.

you mentioned “Application.OpenURL()” would you provide more context into achieving something similar to this?

Like, how would one go about opening the browser inside the game?

I wouldn’t bother trying to embed a browser into a game. The API method just opens the link in the user’s default browser. Alternatively, I noticed when opening up Space Engineers recently, that it first opened the webpage in the Steam Overlay, likely using the Steamworks API.

Try it yourself!

Aren‘t there assets on the store, or github repos, or youtube tutorials that show how to do this? I tend to think that question is one for google, or Unity Muse (if you have access, otherwise ChatGPT).

I wasn‘t working on it but I know for sure we had live Youtube video playback in some projects.Perhaps violating TOS though, I mean who would think that‘s a violation when you can embed videos in your own webpages.

Well, you can embed YT videos in an iframe. So you effectively loading the actual youtube player (a part of their website) inside your website. That means YT has full control over analytics and “injecting” advertisement and all that important stuff ^^. Keep in mind, Youtube is not a free community service. They don’t spend millions on hosting huge datacenters which eat up tons of energy and space to please the users. Their goal is to make money. So they are not interested in just acting as a transparent hosting service which pays for storage and traffic and get nothing in reward. I’m not sure if YT actually has paid plans which allow you to use it like that. Though that’s probably not worth it.

Indeed, with YT you can’t fully control your content.
I just started playing with the HISPlayer example using HIS hosting for DASH and HLS streams. It is pretty straightforward. I guess you should just subsistute YT for the HISPlayer hosted m3u8 file and take full control over your content delivery.