Printing from the Unity Web Player Doesn't Work?

In a project script I have:

Application.ExternalCall ("PrintPage");

In my web page I have:

<script language="javascript" type="text/javascript">
 function PrintPage() {
  window.print();
 }
</script>

This causes the browser window to be partially redrawn before crashing the browser (Safari FF on MacOS). Anything else I put in this function, like Alert, works fine when called from Unity.

If I call the PrintPage function from a button on the web page it works, sort of – it displays the print dialog and prints, but the Unity content on the page does not get printed.

Am I doing something wrong? Is it not possible to print the Unity portion of a web page?

No you can not print the unity portion as its not part of the website at all.
it runs outside and just respects the rect within which it is embedded but its never a physical part of it (thats also the reason why putting DIV in front does not work)

So is there any way to print anything from within the Unity Web Player? The client really wants to be able to print data generated by this simulation, but I told them I didn’t think that was possible. But I was hoping to at least be able to print the Unity window. Now I see that even that is not possible.

Yes that should be possible. but you would need intermediate steps:

  1. Take screenshot and encode it to png
  2. upload png to WWW somewhere
  3. load a page with it in
  4. call the print there

Have Unity player call methods in the web page, perhaps? When on a real computer, I will post a link to the doc on that.

Thats what he did as you can see in the original posting but that won’t work as unity isn’t part of it. You need to give the webpage the currently rendered content first in an integrateable form to print it.

Right, so you have it dump the data from Unity into a web page via a Javascript method in the page, which may open in a new Window which is printed. If the point is to print the data, then print the data instead of having a fixation on printing the presentation of the data as displayed by the Unity Web Player.

I’m thinking the best alternative (for printing data) is to have Unity call a JS function that opens a new web page and populates it with data. Maybe create a table to plug the data into. Only limitation would be the amount of data that can be passed to a new page (without using something on the server side).

That would solve most of the problem. Although there is a graphic element in Unity that I would like to be able to print (it’s modified by the simulation), but I can live with just outputting the data.

Now I can’t get the external call to work.

In Unity I have:

function OnPrintButton () {
	Application.ExternalCall ("PrintSetupScreen", "Hello");
}

And in the web page I have:

function PrintSetupScreen(arg) {
	alert(arg);
	var thepage = "http://www.polyhedronlearning.com/newfrontiers/task6/setup_print.html";
	var myname = "Setup";
	var w = 500;
	var h = 700;
	var tscroll = true;
	var winl = ((screen.width - w) / 2);
	var wint = 0;
	var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+tscroll+',resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no';
	var win = window.open(thepage,myname,winprops);
	if (window.focus) {win.focus()}
}
</script>

If I call this function from Unity, the alert is displayed, but then nothing. It looks like the window.open line is being ignored. If I call this function from a button on the web page, the alert is displayed and a new window is opened. Why would this function work when called from the web page but not when called from Unity?

I just discovered that the problem is that I had Block Pop-Up Windows turned on in Safari. If I turn it off, the window is opened by Unity as well as the button on the web page. Is there any way around this, since I’d guess most users will have a pop-up blocker?

Just a suggestion- you could use an overlay modal such as LightBox http://www.huddletogether.com/projects/lightbox2/ or create an iframe instead of a popup window to display the data.

If you want graphical elements, like Dreamora said, try taking a screenshot instead, then print that.

http://stackoverflow.com/questions/3316193/take-screenshot-of-browser-via-javascript

With something like Lightbox I can only display images (as far as I can tell). I have an HTML page with a table that I need to populate with data from the Unity simulation.

I’ve never used an iframe, but that would have to be in the same window as the Unity sim. The problem with that is the Unity content takes up the entire window on a smaller monitor. Users on a laptop might not even realize they need to scroll down. And if it’s in the same window, I wouldn’t need an iframe, I could just pass the data from Unity via a JS function.

I was just thinking that you could create an overlay over the webplayer. I’m not sure if the data would show up on top of the webplayer but that’s where the iframe comes in handy (flash trick to get data to show up on top of the player)

I did find a Lightbox-type framework (Floatbox) that lets me open a web page in an iFrame. Works great in Safari only. In FF or IE, the Unity Web Player interferes with the iFrame. In FF, the window goes black when the iFrame is displayed rather than dimming. I can live with that. But in IE, the Unity page goes away and must be reloaded when the iFrame is dismissed. This I cannot live with.

Does anybody know of anything like Lightbox that can display either an HTML page or content from a hidden div, and that works with Unity in all popular browsers?