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

[UniWebView 5] Render as Texture crashed on iOS

Discussion in 'Scripting' started by trieujd, Apr 13, 2024.

  1. trieujd

    trieujd

    Joined:
    Oct 26, 2023
    Posts:
    1
    Hi guys,

    I experienced game get crashed on iOS device when using feature Render as Texture of UniWebView 5.
    ```
    var texture = webView.CreateRenderedTexture(webView.Frame);
    ```

    Any help?

    Here is some information:
    - Unity Editor version 2022.3.20f1
    - UniWebView 5 version 5.9.2
    - iOS: 17.4.1

    Here is my code:

    using System;
    using UnityEngine;
    using UnityEngine.UI;

    public class MapController : MonoBehaviour
    {
    [SerializeField] private UniWebView webView;

    [SerializeField] private RawImage rawImage;

    public bool IsLoading { get; private set; } = true;
    public bool AllowMediaCapture { get; private set; } = false;


    // Start is called before the first frame update
    void Start()
    {
    UniWebViewLogger.Instance.LogLevel = UniWebViewLogger.Level.Verbose;
    UniWebView.SetForwardWebConsoleToNativeOutput(true);
    webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
    webView.OnPageFinished += OnPageFinished;
    webView.OnMessageReceived += OnMessageReceived; // window.location.href = "uniwebview://game-over?score=" + myGameArea.frameNo;

    var accessURL = UniWebViewHelper.StreamingAssetURLForPath("www/");
    // var url = UniWebViewHelper.StreamingAssetURLForPath("www/index.html");
    var url = "https://classroom-dev.servegame.com/";
    webView.Load(url);
    Debug.LogWarning($"Load................ {url}");

    webView.RegisterOnRequestMediaCapturePermission(RegisterOnRequestMediaCapturePermissionHandler);
    webView.RegisterShouldHandleRequest(RegisterShouldHandleRequestHandler);

    // Start the rendering process.
    webView.StartSnapshotForRendering();
    }

    private bool RegisterShouldHandleRequestHandler(UniWebViewChannelMethodHandleRequest request)
    {
    return true;
    }

    private UniWebViewMediaCapturePermissionDecision RegisterOnRequestMediaCapturePermissionHandler(
    UniWebViewChannelMethodMediaCapturePermission permission)
    {
    AllowMediaCapture = true;
    Debug.LogWarning("RegisterOnRequestMediaCapturePermissionHandler");
    return UniWebViewMediaCapturePermissionDecision.Grant;
    }

    private void OnMessageReceived(UniWebView webView, UniWebViewMessage message)
    {
    // if (message.Path.Equals("game-over")) {
    // var score = message.Args["score"];
    // Debug.Log("Your final score is: " + score);

    // // Restart the game after 3 second
    // Invoke("Restart", 3.0f);
    // }
    }

    private void OnPageFinished(UniWebView webView, int statusCode, string url)
    {
    IsLoading = true;

    // Start the rendering process.
    webView.StartSnapshotForRendering(onStarted: texture =>
    {
    rawImage.texture = texture;
    });
    }

    private void RenderWebView()
    {
    if (webView != null)
    {
    // webView.Stop();
    var texture = webView.CreateRenderedTexture(webView.Frame);
    // Destroy(rawImage.texture);
    // rawImage.texture = texture;
    // Debug.Log("Update texture...");
    }
    }

    void OnDestroy()
    {
    // clean up webview footprint
    webView.StopSnapshotForRendering();
    Destroy(rawImage.texture);
    webView = null;
    }

    float cooldownTime = 3;

    // Update is called once per frame
    void Update()
    {
    if (IsLoading)
    {
    cooldownTime -= Time.deltaTime;
    if (cooldownTime <= 0)
    {
    RenderWebView();
    cooldownTime = 3f;
    }
    }
    }
    }
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,274
    Ask UniWebView support for help, not us.