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

Read your own url posts in Webplayer

Discussion in 'Scripting' started by ki_ha1984, Nov 28, 2014.

  1. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Hi,

    I would like to ask you how i can read the posts of my own url in a web player game.
    Exactly, i mean:
    i have and "index.php?game=mygame" where is attached the game that i built and runs when i call that url.
    I need to read the value of the variable "game" of the url in every load of the game.

    Is there any csharp code example or how i can make it ?

    thank you in advance
     
  2. Ian-Dundore

    Ian-Dundore

    Unity Technologies

    Joined:
    Dec 14, 2012
    Posts:
    7
    Hi,

    There's no way to do this directly, but the value is visible to Javascript running in the webpage containing your Webplayer game. You can use Application.ExternalCall to request it from Javascript, and use the browser-to-Unity communication path to send the full URL back into Unity.

    I've attached a small example, which uses the following C# script and Javascript snippet to accomplish this, and show the URL via a Unity 4.6 UI.Text component. The built webplayer app can be found in the /build/ subfolder.

    If you're using Unity's generated HTML file to view your webplayer game, you'll have to either create a custom webplayer page template or manually add the Javascript snippet to the Unity-generated HTML file each time you build.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Text))]
    6. public class URLChecker : MonoBehaviour {
    7.  
    8.     private Text text;
    9.     private string fmt = "full url: {0}";
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         text = GetComponent<Text>();
    14.         text.text = "Loading...";
    15.         Application.ExternalCall ("GetFullUrl", gameObject.name);
    16.     }
    17.  
    18.     public void SetFullUrl(string fullUrl) {
    19.         text.text = string.Format (fmt, fullUrl);
    20.     }
    21. }
    22.  
    Code (JavaScript):
    1. function GetFullUrl(gameObjectName) {
    2.     u.getUnity().SendMessage(gameObjectName, "SetFullUrl", window.location.href);
    3. }
     

    Attached Files: