Immerse SDK
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Streaming Assets

Due to the security features of the Immerse Platform, the standard method for accessing Unity’s Streaming Assets in WebGL is not supported. Instead, a signed URL must be requested from the platform to access these assets securely.

The Immerse SDK provides a built-in feature to streamline this process. Refer to the example script below for guidance on how to implement it.

using System.Collections;
using ImmerseSDK.Platform.WebGL;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class StreamingAssetsExample : MonoBehaviour
{
    [SerializeField, Tooltip("Component to display the loaded texture")]
    private RawImage _display;
    
    /// <summary>
    /// Load a texture from streaming assets and apply it to the display
    /// </summary>
    /// <param name="relativePath">Path to asset relative to StreamingAssets folder</param>
    public void SetTexture(string relativePath)
    {
        // Get the full path to the asset and start the loading coroutine
        var path = StreamingAssetsPath.Get(relativePath);
        StartCoroutine(LoadAndApply(path));
    }
    
    private IEnumerator LoadAndApply(string path)
    {
        // Streaming assets must be retrieved via web request on WebGL and Android
        using var request = UnityWebRequestTexture.GetTexture(path);
        yield return request.SendWebRequest();
        
        // Handle error
        if (!string.IsNullOrWhiteSpace(request.error))
        {
            Debug.LogErrorFormat("Failed to load asset. {0}", request.error);
            yield break;
        }

        // Retrieve and apply texture
        var texture = DownloadHandlerTexture.GetContent(request);
        _display.texture = texture;
    }
}