Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting a URL/link directly to a scene/level (or other portion) of a WebGL published "game"?

Discussion in 'Web' started by GrantSRobertson, Oct 7, 2018.

  1. GrantSRobertson

    GrantSRobertson

    Joined:
    Oct 7, 2018
    Posts:
    2
    I am thinking of using Unity to create interactive eLearning lessons. Part of those lessons will be questions and answers (and/or flashcards). I would like outside websites or programs to be able to link directly to those individual questions/flashcards (which I will likely design as a separate scene/level within Unity), and skip over the lesson (the primary "game") itself.

    Once a game has been published to WebGL, is it possible to get the URLs to link directly to those individual, specific scenes/levels within my "game"?

    As a followup question: Is it possible to generate a URL that starts the main "game"/lesson but specifies a particular state within the game? Sort of like a saved game.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,367
    Your web page hosting the WebGL Unity content can include your own JavaScript content, which can call some methods into the WebGL content. https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    So with this, your web page could surely launch the WebGL, wait for it to get initialized, and then call a SendMessage() to some class you include in the Unity build, which could jump to the appropriate in-game state.
     
  3. GrantSRobertson

    GrantSRobertson

    Joined:
    Oct 7, 2018
    Posts:
    2
    OK. So, I would have to publish to WebGL, then add JavaSript within that result to call methods in the Unity-generated JavaScript... Then I would have to fashion URLs that activated my JavaScript.

    However, no direct links into the levels are provided by the Unity WebGL generator. OK.

    Don't know enough JavaScript to do this yet, but A) I assume it is doable and B) That is a question for a different forum (or I'll just learn that part from a book).

    I assume, if I used consistent level and method naming, then I could write my add-on JavaScript once and then just insert the same JavaScript into each lesson/"game" I make.

    Thanks.
     
  4. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    you can try something like this:

    1. Your Link:
    Code (CSharp):
    1. https://www.myurl.com/myapp/?mycode
    2. In Unity:
    Code (CSharp):
    1. //Get URL CODE
    2. bool loadWithCode = false;
    3. int pm = Application.absoluteURL.IndexOf("?");
    4. if (pm != -1)
    5. {
    6.     string myCode = Application.absoluteURL.Split("?"[0])[1];
    7.     if (myCode != "")
    8.     {
    9.         loadWithCode = true;
    10.     }
    11.     else
    12.     {
    13.         loadWithCode = false;
    14.     }
    15. }