Search Unity

WebView in WebGL

Discussion in 'Getting Started' started by aravind07, Mar 14, 2017.

  1. aravind07

    aravind07

    Joined:
    Mar 14, 2017
    Posts:
    1
    Hi ,
    In My new Project i would like to add a web view in a WEBGL build .. is it possible ? i am using unity version 5.5 ... if its possible what all plugins do i need ?
     
    timlg likes this.
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Wait, isn't the point of a web view to... view a web page? And a WebGL build runs on a web page, so... you want to have a web page inside an applet inside a web page?
     
    Joe-Censored likes this.
  3. BaiboZhang

    BaiboZhang

    Joined:
    Jul 10, 2017
    Posts:
    1
    Uniwebview can be used for mobile platforms. But how can I show a webview in WebGL ? I means to show some web page in a WebGL app.
     
    brennanyoung likes this.
  4. ammarvohra

    ammarvohra

    Joined:
    Jan 24, 2016
    Posts:
    10
    You can call the external jslib method to open iframe for your perticular website. For example
    I added this example.jslib in Plugins folder

    Code (JavaScript):
    1. var IframePlugin = {
    2.       iframeClick: function() {
    3.         if (!document.getElementById('webview')) {
    4.           var ifrm = document.createElement("iframe");
    5.           ifrm.setAttribute("src", "https://www.someexample.com");
    6.           ifrm.setAttribute("id", "webview");
    7.           ifrm.style.width = "640px";
    8.           ifrm.style.height = "480px";
    9.           document.body.appendChild(ifrm);
    10.         }
    11.       }
    12.     };
    13.     mergeInto(LibraryManager.library, IframePlugin);
    and called it from C# script using:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class tempscript : MonoBehaviour
    7. {
    8.  
    9.     [DllImport("__Internal")]
    10.     private static extern void iframeClick();
    11.  
    12.  
    13.     public void OpenWebView()
    14.     {
    15.         iframeClick();
    16.      }
    17.  
    18. }
    19.  
    NOTE: You need to add the UI button with the component EventTrigger and adding OnPointerDown method to call the OpenWebView() method. Also you cannot open some sites which have limitations of access control like google, facebook, yahoo, etc.
     
    timlg likes this.
  5. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    anybody tried this, does it actually show a webpage inside WebGL?
     
  6. Parixit_Jadeja

    Parixit_Jadeja

    Joined:
    Jul 27, 2017
    Posts:
    8
    I have tested this and it is working. A web-view window opens behind the WebGL window. But, I think we can work on looks by changing the unity template and example.jslib.
     
  7. Parixit_Jadeja

    Parixit_Jadeja

    Joined:
    Jul 27, 2017
    Posts:
    8
    This is how it looks:
    upload_2020-8-25_22-47-14.png

    You can also pass link from unity like this:


    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.   LoadWebView: function (str) {
    3.     window.alert(Pointer_stringify(str));
    4.     if (!document.getElementById('webview')) {
    5.           var ifrm = document.createElement("iframe");
    6.           ifrm.setAttribute("src",Pointer_stringify(str));
    7.           ifrm.setAttribute("id", "webview");
    8.           ifrm.style.width = "640px";
    9.           ifrm.style.height = "480px";
    10.           document.body.appendChild(ifrm);
    11.     }
    12.   },
    13. });
    Unity Code:

    Code (CSharp):
    1.     string url = "https://www.sample.com/";
    2.  
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         LoadWebView(url);
    7.     }
    8.  
    9.     [DllImport("__Internal")]
    10.     private static extern void LoadWebView(string url);
    You can see this link for more information:- https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
     
    timlg likes this.