Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WebGL : How to alter Url

Discussion in 'WebGL' started by maxb12, Nov 6, 2019.

  1. maxb12

    maxb12

    Joined:
    Feb 2, 2019
    Posts:
    10
    Hello. I want to be able to alter the URL while the Unity WebGL App is running.
    The Url changes but unfortunatly the WebGL App restarts as soon as i do this.
    Is this a bug or am i doing something wrong?

    I do it via a *.jslib file in a "Plugins" folder with the content:
    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.     SetParam: function(param){
    3.         window.location.search = "/" + Pointer_stringify(param);
    4.     }
    5. });

    Then in C# i import the function and use it like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class changeUrl : MonoBehaviour
    7. {
    8.     //import javascript library functions
    9.  
    10.     [DllImport("__Internal")]
    11.     private static extern void SetParam(string param);
    12.  
    13.     public string nameOFUrl;
    14.  
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.      
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.      
    27.     }
    28.  
    29.     public void changeUrlOnClick()
    30.     {
    31.         Debug.Log("switching to: " + nameOFUrl);
    32.         SetParam(nameOFUrl);
    33.     }
    34. }
    35.  
     
  2. Akkadokk

    Akkadokk

    Joined:
    Mar 7, 2017
    Posts:
    3
    Hey ! did you solve your problem ?
     
  3. kou-yeung

    kou-yeung

    Joined:
    Sep 5, 2016
    Posts:
    30