Streaming Assets
Unity Streaming Assets are a great way to manage files, such as videos or asset bundles, allowing them to be loaded only when they are needed.
To ensure applications are secure when uploaded to the Immerse Platform, all files are accessed via signed URLs. This means that the usual method of accessing the StreamingAssets folder will not work as expected on WebGL builds.
The Immerse SDK provides a method that will provide a path to any asset in your StreamingAssets folder, on any of our supported platforms.
ImmerseSDK.StreamingAssets.GetAssetPath(string asset);
For WebGL, this will return a signed URL. For other platforms, (Android and Windows) it will use the default Unity approach of prepending Application.streamingAssetsPath
Loading a video from the StreamingAssets folder and playing using Unity’s VideoPlayer component
using UnityEngine;
using UnityEngine.Video;
/// <summary>
/// Video playback example using the ImmerseSDK streaming assets implementation
/// </summary>
[RequireComponent(typeof(VideoPlayer))]
public class VideoLoader : MonoBehaviour
{
[Tooltip("Path to video, relative to StreamingAssets folder")]
[SerializeField] private string _videoFilename = "video.mp4";
private void Awake()
{
// Get the video player component
var videoPlayer = GetComponent<VideoPlayer>();
// Set the url using the ImmerseSDK method and play
videoPlayer.url = ImmerseSDK.StreamingAssets.GetAssetPath(_videoFilename);
videoPlayer.source = VideoSource.Url;
videoPlayer.Play();
}
}
Loading an image from the StreamingAssets folder and applying it to a material
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// Image load example using the ImmerseSDK streaming assets implementation
/// </summary>
public class ImageLoader : MonoBehaviour
{
[Tooltip("Path to image, relative to StreamingAssets folder")]
[SerializeField] private string _imageFilename = "image.jpg";
[Tooltip("Material to apply the image to, using mainTexture")]
[SerializeField] private Material _targetMaterial;
private IEnumerator Start()
{
// Get the image URL using the ImmerseSDK method
var imageUrl = ImmerseSDK.StreamingAssets.GetAssetPath(_imageFilename);
// Download the texture
var www = UnityWebRequestTexture.GetTexture(imageUrl);
yield return www.SendWebRequest();
// Print error if one occurs
if (www.isHttpError || www.isNetworkError)
{
Debug.LogErrorFormat("Failed to load image. {0}", www.error);
yield break;
}
// Load texture and apply to material
var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
_targetMaterial.mainTexture = texture;
}
}
- Add an asset to your StreamingAssets folder within your project.
- Create a new object
- Next copy and paste one of the code examples above into a new script.
- Add the script onto your object
- Drag the asset into the filename field of the script
- Drag a material into the Target Material field of the script (don’t forget to apply it to the mesh renderer of the object too!)