How to use streaming property in Unity

by:

Web Development

An asset can be any useful resource in Unity those means can be images, audio, video, scripts, text, etcetera.

When constructing a Unity challenge to any system, all the assets in your game will be “packaged” in a file (or a lot more depending on your platform), and the ensuing dimensions of the develop will rely on the size of the belongings you determined to offer within just your activity.

It is not the exact to make a activity with several Hd textures somewhat than applying a Map Atlas, or employing movie on 240p rather than applying 1080p. It is anticipated that the larger the dimension of your assets, the bigger the sizing of your remaining construct.

In typical, it is a excellent follow to preserve your construct as lightweight as you can, so the participant will not be discouraged from downloading your recreation owing to the dimensions, and your scenes will run considerably faster.

There are multiple tips and methods to attain this, and in this write-up we will be speaking about some of them, beginning with streaming assets.

What are streaming property?

Let’s say you have a good scene in your activity in which all the motion happens. The participant is the enemy base, and in a particular place, you choose to place a video taking part in on a laptop as an easter egg.

The video is actually big, like extra than 300MB, and the player may possibly or might not enter that distinct space to essentially see it. Would it be ok to load it in your scene by default? That may well lead to unwanted slowness in your fantastic scene. So it could be good to just load it when we basically want it.

A streaming asset it just that: an asset positioned in a certain folder that would be loaded by the Unity Player when required. That asset will be positioned in an effortless to come across address inside the target system.

You should observe that any asset positioned on a streaming asset folder (named StreamingAssets in just Unity) will be copied into the target platform, and if a user queries inside the project folder it will be able to see them.

This is how some of the mods of some game titles are manufactured.

Changing the texture of a character? Go into the information folder and find the utilised texture, then modify it. Shifting the voice strains of a tricky manager? Just go to the details folder and obtain the audio track and change it with a different with the similar identify.

Streaming assets permit the game operate efficiently without the need of loading all the assets from memory and provide the even larger kinds in an simple to attain path.

Let us make an straightforward instance of streaming assets

We are likely to demonstrate how streaming belongings function with an simple example by showing a online video in your activity.

I downloaded a preview of a movie from iStockPhoto for this example.

Make guaranteed the video clip you want to use has a single of the next extensions: .mov, .mpeg, .mp4, .avi, .asf.

For our original instance, we are going to generate a new folder called Video and area our video within that folder:

Video Inside Folder


Much more wonderful articles or blog posts from LogRocket:


Then, on our Hierarchy panel, we are going to set up our online video player.

Right-click on your Hierarchy and add an Vacant Activity Object. I known as mine VideoObject.

Within of it, I made a cube exactly where we are likely to perform the video clip it can be any condition you want, but I selected a cube for comfort. I get in touch with it Movie_Canvas.

And at the identical amount of Online video_Canvas, within VideoObject, I included a new Video clip Player object by correct-clicking VideoObject and deciding upon Video clip, then Video Player.

Video Object Folder Structure

Below is how I set up our Online video Participant item:

Video Player Setup

We set the Source as a Video Clip, and then we picked the movie clip from our Video clip Folder that we created, then we picked Loop so the video would retain enjoying infinitely.

Then, pick out Render Mode as Content Override and drag and drop the Item you want to perform the online video on — in our scenario, the Online video_Canvas dice. Let Content Home be _MainTex.

If you click on Participate in in the Unity Editor, you will see some thing like this: a movie getting played on each and every aspect of the dice.

Video on Each Cube Side

You could also generate a aircraft or cut down the scale of the dice to the form you want if you want a flatter surface, like a monitor.

This demo video clip is 3.5MB.

If we build this challenge for a standalone platform (Windows, Linux, MacOs) we will see that a blank task with a movie it is 87.6MB.

Black Project Video

Now, let us see how it appears to be like applying the similar video clip as a streaming asset.

To do so, we are heading to create a new folder identified as StreamingAssets in the Belongings folder this is a specific named folder that will treat our video clip file video clip_streamingAssets as a standard file with no selections in the qualities window of the editor.

StreamingAssets Folder

Then in our Online video Participant object, we could alter the Resource of the Video Player Game Component to URL and leave the real URL on your own. We will make a new Script named LoadVideo and connect it to our Online video Player GameObject like in the impression.

This script requires as a parameter a Online video Player, so we will drag and drop our identical Video Player that this script it’s attached to.

Video Player Details

Here’s the script of LoadVideo:

making use of UnityEngine
employing UnityEngine.Video

community class LoadVideo : MonoBehaviour

   general public VideoPlayer myVideoPlayer
   void Start out()
   
       string videoUrl= Application.streamingAssetsPath +"https://website.logrocket.com/"+ "video clip_streamingAssets" + ".mp4"
       myVideoPlayer.url = videoUrl
   

In essence, at the commence of the application it will use Software.streamingAssetsPath to get the route of the StreamingAssets folder in any concentrate on platform that has been created into. And then it will reference the identify of our video in that folder and its extension.

Then, it will choose myVideoPlayer (that now has the reference of our scene video clip participant) and it will write its url property with the path of our video clip.

This will end result in the similar dice with the actively playing video.

Video on Each Cube Side

Now, if we develop this project, we will see that it has the identical sizing as our former instance, but with 1 important variation.

Blank Project with One Difference

If we see the Deal contents of this build, we can see that on Contents/Assets/Data there is our StreamingAssets folder with our video.

StreamingAssets Folder with Video

And if we switch the video clip in our StreamingAssets folder with yet another of the similar name, we would not will need to develop our task once again in buy to see the changes reflected in our recreation.

Cube with Differences

Now that we know how we can detach our property/written content from our construct. We could also call distant documents from the URL on our Video clip Participant. For performing so, we are heading to use this video as illustration. It’s virtually 10 minutes extended and it’s about 151MB.

If we alter our code to:

making use of UnityEngine
employing UnityEngine.Movie

public class LoadVideo : MonoBehaviour

   community VideoPlayer myVideoPlayer
   void Begin()
   
       string videoUrl= "http://commondatastorage.googleapis.com/gtv-movies-bucket/sample/BigBuckBunny.mp4"
       myVideoPlayer.url = videoUrl
   

…and delete our primary video clip of the StreamingAssets folder, we conclusion up with some thing like this:

New Video Cube

The exact features, but now the online video belongings arrive from an exterior resource.

Now if we develop our project without the need of the regional online video in our StreamingAssets folder, we would see a decrease in the dimensions of our construct.

Streaming Assets Decreased Size

Make sure you continue to keep in head that this could also be finished with an async connect with or a UnityWebRequest phone and a coroutine, but for simplicity, we just increase the specific URL of a movie to display the ability of streaming assets.

Employing a coroutine to fetch a video from an external supply

We can modify our LoadingScript like this in buy to use a coroutine as an alternative of passing the immediate hyperlink to our online video participant:

using UnityEngine
making use of UnityEngine.Video
applying UnityEngine.Networking
working with Technique.Collections

public course LoadVideo : MonoBehaviour

   general public VideoPlayer myVideoPlayer
   void Begin()
   
       StartCoroutine(LoadExternalVideo("http://commondatastorage.googleapis.com/gtv-movies-bucket/sample/ElephantsDream.mp4"))
   

     IEnumerator LoadExternalVideo(string url)
      working with (UnityWebRequest request = UnityWebRequest.Get(url))
      
          //Fetches a webpage and shows the selection of people of the reaction.
          yield return request.SendWebRequest()
          myVideoPlayer.url = request.url
      
  

With this case in point, we could use a coroutine to take in an API endpoint and retrieve the URL of the online video, but for simplicity, this is a simple case in point on how to do it with an external backlink.

The outcome will be quite related we just need to have to make guaranteed that our UnityWebRequest end result is retrieved from the eaten endpoint and make positive that the URL of a video clip is present just before environment it up to the URL residence of the video clip participant.

Closing feelings

Streaming property are just just one of the various methods Unity handles belongings for your activity. It functions wonderful when you are hoping to get your player base associated by letting them to mod your game, or if you want to have a lightweight last establish of your recreation, and then on initial launch, download the relaxation of the belongings of your video game in advance of the the participant can take detect.

There are numerous on-line online games that do this strategy: their game titles are quite light-weight, you can complete up a small tutorial of the 1st level, and then you will get a loading screen that downloads the rest of the assets.

You have to hold in head the greatest strategy for your game. You cannot have all your assets in a remote server, mainly because what would it occur if the initially time the player plays your match their device doesn’t have a very good net link? What would your recreation glance like with out property?

A person of the very best approaches is getting a incredibly light-weight edition of your belongings as default (like products, textures, illustrations or photos) as a placeholder, and when you are guaranteed that your participant will delight in that written content and the unit is ready to download that facts, do so as seamlessly as probable.

I hope you have preferred this posting and find it handy! Joyful gamedev!

LogRocket: Entire visibility into your internet and cellular applications

LogRocket Dashboard Free Trial Banner

LogRocket is a frontend application checking answer that lets you replay problems as if they occurred in your individual browser. Instead of guessing why faults come about, or inquiring consumers for screenshots and log dumps, LogRocket lets you replay the session to swiftly recognize what went improper. It is effective completely with any application, irrespective of framework, and has plugins to log extra context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket information console logs, JavaScript glitches, stacktraces, network requests/responses with headers + bodies, browser metadata, and tailor made logs. It also instruments the DOM to file the HTML and CSS on the site, recreating pixel-ideal video clips of even the most complex one-webpage website and cell applications.

Attempt it for absolutely free.

Leave a Reply

Your email address will not be published.