Application.ExternalEval - valid js calls -> web page

Using the Application.ExternalEval cammand I am able to open-up, re-size, and kind-of locate the position of where the new page will appear in my game.

I have two (2) issues that I have been fighting with and I am starting to wonder is solutions are even possible.

(1) in locating the page, I can left=100, top=100 or some integer… but I really want to make sure the page is located properly in everyone’s web viewer so I am trying to use screen.height screen.width in the string that I pass to the Application.ExternalEval command but they don’t work.

(2) secondly, I have been trying to send a command to close the open web page page trying variations with both the Application.ExternalEval command Application.ExternalCall and I can’t come up with anything to close the opened web page.

My script:

Code:

if (hudMode == "peripherals") 
   { 
      GUI.skin = customSkinStandard;    
       
      GUILayout.BeginArea (Rect (60,Screen.height - 40,1900,128)); 
      GUILayout.BeginHorizontal (); 
      if (GUILayout.Button ("Turn On Device")) 
      { 
      Application.ExternalEval("window.open('http://www.tech-ufo.com/','microscope','width=640,height=480,left=(screen.width/2)-320,top=(screen.height/2)-240')");  // does NOT work 
      Application.ExternalEval("window.open('http://www.tech-ufo.com/,'microscope','width=640,height=480,left=100,top=100')"); // works but not is exact 
      } 
      if (GUILayout.Button ("Turn Off Device")) 
      { 
      Application.ExternalCall("microscope.close()"); 
      } 
      if (GUILayout.Button ("Return to Classroom")) 
      { 
      camera1.enabled = true; 
      camera2.enabled = false; 
      hudMode = "classroom"; 
      } 
      GUILayout.EndHorizontal ();    
      GUILayout.EndArea(); 
    
}

Screen.width and Screen.height will return the size of the webplayer itself, not the dimensions of the screen (although in fullscreen mode, they will be the same).

Can the ExternalEval return data passed by the Javascript function?

I am trying to print the variable helloWorld, but I am getting nothing returned, just the Error: Cannot convert void to string, pls help

dt = Application.ExternalEval("getQueryVariable('helloWorld')");
print(dt);

Neither ExternalEval nor ExternalCall return anything. The primary reason for this (likely) is because ExternalCall is non-blocking. That is, when you execute it, your program continues running instead of waiting for the browser’s JavaScript function to complete and return a value. It doesn’t explicitely mention this on ExternalEval’s documentation, but I suspect it’s the same (plus as an “eval”, there wouldn’t be a concept of a “return value”)

If you really need to, then perhaps you can have your JavaScript in turn make a SendMessage call back to the Unity player as described here: http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html

Something like:

//in Unity, script on an object named "MyObject":

function Start()
{
	Application.ExternalCall("ReportQueryVariable", "helloWorld");
}

function OnQueryVariableReceived(var queryData)
{
	var queryTokens : string[] = queryData.Split("|"[0]);
	var variableName : string = queryTokens[0];
	var variableValue : string = queryTokens[1];
	//do stuff
}


//in the web browser

function ReportQueryVariable(var queryVariableName)
{
	var value = GetQueryVariable(queryVariableName);

	var unity = unityObject.getObjectById("UnityContent");
	unity.SendMessage("MyObject", "OnQueryVariableReceived", queryVariableName + "|" + value);
}

The only reason I combine/split the query variable’s name and value with a pipe “|”, is because (if I recall correctly) you can only pass one value back to Unity. And I’m assuming by what you had so far, you wanted it to be a more general-use method. Of course, all the cavaets: wrote it in notepad, no testing, etc.