Search Unity

How to open a webview when a target is recognized?

Discussion in 'AR/VR (XR) Discussion' started by dcostadc, Feb 11, 2019.

  1. dcostadc

    dcostadc

    Joined:
    Jul 23, 2018
    Posts:
    3
    Hi,

    I'm trying to open a webview inside unity app when an vumark is detected. I'm using gree's webview and my code is the following:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Vuforia;
    5.  
    6. public class TrackMarkNew : MonoBehaviour, ITrackableEventHandler
    7. {
    8.     TrackableBehaviour image_TrackableBehaviour;
    9.     VuMarkManager myVuMark;
    10.  
    11.     public string Url;
    12.     public GUIText status;
    13.     WebViewObject webViewObject;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         image_TrackableBehaviour = GetComponent<TrackableBehaviour>();
    18.         if (image_TrackableBehaviour != null)
    19.         {
    20.             image_TrackableBehaviour.RegisterTrackableEventHandler(this);
    21.         }
    22.         myVuMark = TrackerManager.Instance.GetStateManager().GetVuMarkManager();
    23.     }
    24.  
    25.     public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
    26.     {
    27.         Debug.Log("Outside found");
    28.         if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    29.         {
    30.             Debug.Log("inside found");
    31.             Webview();
    32.         }
    33.         else if (previousStatus == TrackableBehaviour.Status.TRACKED && newStatus == TrackableBehaviour.Status.NOT_FOUND)
    34.         {
    35.             //DO SOMETHING
    36.         }
    37.     }
    38.  
    39.     IEnumerator Webview()
    40.     {
    41.         foreach (var item in myVuMark.GetActiveBehaviours())
    42.         {
    43.             Debug.Log("inside function");
    44.             webViewObject.gameObject.SetActive(true);
    45.             webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
    46.             webViewObject.Init(
    47.                 cb: (msg) =>
    48.                 {
    49.                     Debug.Log(string.Format("CallFromJS[{0}]", msg));
    50.                     //status.text = msg;
    51.                     //status.GetComponent<Animation>().Play();
    52.                 },
    53.                 err: (msg) =>
    54.                 {
    55.                     Debug.Log(string.Format("CallOnError[{0}]", msg));
    56.                     //status.text = msg;
    57.                     //status.GetComponent<Animation>().Play();
    58.                 },
    59.                 started: (msg) =>
    60.                 {
    61.                     Debug.Log(string.Format("CallOnStarted[{0}]", msg));
    62.                 },
    63.                 ld: (msg) =>
    64.                 {
    65.                     Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
    66. #if UNITY_EDITOR_OSX || !UNITY_ANDROID
    67.                 // NOTE: depending on the situation, you might prefer
    68.                 // the 'iframe' approach.
    69.                 // cf. https://github.com/gree/unity-webview/issues/189
    70. #if true
    71.                 webViewObject.EvaluateJS(@"
    72.                  if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
    73.                    window.Unity = {
    74.                      call: function(msg) {
    75.                        window.webkit.messageHandlers.unityControl.postMessage(msg);
    76.                      }
    77.                    }
    78.                  } else {
    79.                    window.Unity = {
    80.                      call: function(msg) {
    81.                        window.location = 'unity:' + msg;
    82.                      }
    83.                    }
    84.                  }
    85.                ");
    86. #else
    87.                 webViewObject.EvaluateJS(@"
    88.                  if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
    89.                    window.Unity = {
    90.                      call: function(msg) {
    91.                        window.webkit.messageHandlers.unityControl.postMessage(msg);
    92.                      }
    93.                    }
    94.                  } else {
    95.                    window.Unity = {
    96.                      call: function(msg) {
    97.                        var iframe = document.createElement('IFRAME');
    98.                        iframe.setAttribute('src', 'unity:' + msg);
    99.                        document.documentElement.appendChild(iframe);
    100.                        iframe.parentNode.removeChild(iframe);
    101.                        iframe = null;
    102.                      }
    103.                    }
    104.                  }
    105.                ");
    106. #endif
    107. #endif
    108.                     webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
    109.                 },
    110.                 //ua: "custom user agent string",
    111.                 enableWKWebView: true);
    112. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
    113.         webViewObject.bitmapRefreshCycle = 1;
    114. #endif
    115.             webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
    116.             webViewObject.SetVisibility(true);
    117.  
    118. #if !UNITY_WEBPLAYER
    119.             if (Url.StartsWith("http"))
    120.             {
    121.                 webViewObject.LoadURL(Url.Replace(" ", "%20"));
    122.             }
    123.             else
    124.             {
    125.                 var exts = new string[]{
    126.                 ".jpg",
    127.                 ".js",
    128.                 ".html"  // should be last
    129.             };
    130.                 foreach (var ext in exts)
    131.                 {
    132.                     var url = Url.Replace(".html", ext);
    133.                     var src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
    134.                     var dst = System.IO.Path.Combine(Application.persistentDataPath, url);
    135.                     byte[] result = null;
    136.                     if (src.Contains("://"))
    137.                     {  // for Android
    138.                         var www = new WWW(src);
    139.                         yield return www;
    140.                         result = www.bytes;
    141.                     }
    142.                     else
    143.                     {
    144.                         result = System.IO.File.ReadAllBytes(src);
    145.                     }
    146.                     System.IO.File.WriteAllBytes(dst, result);
    147.                     if (ext == ".html")
    148.                     {
    149.                         webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
    150.                         break;
    151.                     }
    152.                 }
    153.             }
    154. #else
    155.         if (Url.StartsWith("http")) {
    156.             webViewObject.LoadURL(Url.Replace(" ", "%20"));
    157.         } else {
    158.             webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
    159.         }
    160.         webViewObject.EvaluateJS(
    161.             "parent.$(function() {" +
    162.             "   window.Unity = {" +
    163.             "       call:function(msg) {" +
    164.             "           parent.unityWebView.sendMessage('WebViewObject', msg)" +
    165.             "       }" +
    166.             "   };" +
    167.             "});");
    168. #endif
    169.             yield break;
    170.         }
    171.     }
    172.  
    173. #if !UNITY_WEBPLAYER
    174.     void OnGUI()
    175.     {
    176.         Debug.Log("inside gui");
    177.         GUI.enabled = webViewObject.CanGoBack();
    178.         if (GUI.Button(new Rect(10, 10, 80, 80), "<"))
    179.         {
    180.             webViewObject.GoBack();
    181.         }
    182.         GUI.enabled = true;
    183.  
    184.         GUI.enabled = webViewObject.CanGoForward();
    185.         if (GUI.Button(new Rect(100, 10, 80, 80), ">"))
    186.         {
    187.             webViewObject.GoForward();
    188.         }
    189.         GUI.enabled = true;
    190.  
    191.         GUI.TextField(new Rect(200, 10, 300, 80), "" + webViewObject.Progress());
    192.     }
    193. #endif
    194.  
    195. }
    I´ve put the webview in a function and then said to, when the vumark is recognized the webview should appear.

    The problem is that when the mark is recognized, nothing happens.



    Can anyone tell me what I'm doing wrong?

    Thanks.