x


How would I go about getting the results of a javascript variable to a string inside script?

Is it possible to get the URL that the web player object is embedded in?

From the "Unity web player and browser communication page" they have this example:

Application.ExternalEval(
 "if(document.location.host != 'unity3d.com') 
 { 
    document.location='http://unity3d.com'; 
 }" 
);

But is it possible to get what "document.location" is equal inside my scripts? Ideally it would be without modifying the page itself that the unity web player is embedded in.

more ▼

asked Nov 05 '09 at 10:47 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

To do that you would need to use both directions of browser communication. Here is one example of how it could be done:

using UnityEngine;
using System.Collections;

public class WebCheck : MonoBehaviour
{
    private string m_Address;

    public void ResolvePageAddress ()
    {
    	Application.ExternalEval ("GetUnity ().SendMessage ('WebCheckGameObject', 'SetPageAddress', document.location);");
    	Debug.Log (m_Address);
    }

    public void SetPageAddress (string address)
    {
    	m_Address = address;
    }
}

For this script example to be working, your script would need to be in the scene on a GameObject by the name "WebCheckGameObject".

more ▼

answered Nov 10 '09 at 01:19 PM

AngryAnt gravatar image

AngryAnt ♦♦
4k 14 19 52

(comments are locked)
10|3000 characters needed characters left

try Application.dataPath it doesn't return the exact html file, but it does return the path. If you're going for antipiracy this should be enough. For example:

if (Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
{
    datapath = Application.dataPath;
    if(datapath == "http://mypage.com/games/mygame")
    {
    	print("This is the correct page. No piracy detected.");
    }
    else
    {
    	print("Dr Jones: This game doesn't belong on this webpage. It BELONGS IN A MUSEUM!\nAuctioneer:SIT DOWN Dr Jones!");
    }
}
more ▼

answered Nov 06 '09 at 10:17 AM

MyraLoveless gravatar image

MyraLoveless
11 1

(comments are locked)
10|3000 characters needed characters left

Check out the Unity function Application.absoluteURL. The Scripting Reference for that function also has an anti-piracy code sample using that and Application.dataPath.

more ▼

answered Nov 07 '09 at 09:08 PM

technicat gravatar image

technicat
1.5k 10 11 34

That returns the path of the unity3d file, not the URL of the web site it is embedded in.

My use wasn't for anti-piracy measures, it was more of an intellectual exercise to see if I could get parameters out of a URL.

Nov 09 '09 at 10:33 PM Tetrad

Ah! Your initial title did not immediately reflect this, which is why you got answers like these. I took the liberty of editing your title to better match your goal.

Nov 10 '09 at 01:17 PM AngryAnt ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x810
x211
x126
x11

asked: Nov 05 '09 at 10:47 PM

Seen: 2832 times

Last Updated: Nov 20 '09 at 01:10 PM