Detect if Web Player is running in Facebook Canvas or not.

I’m using Facebook’s official plugin for Unity, but it only works in Facebook Canvas or in the Editor. When running in a browser that is not in Facebook Canvas, it does not work. I’d like to be able to detect whether it is running in Facebook Canvas or not. The app depends on Facebook for a lot of things, but I’d like it to still be able to run outside of Facebook Canvas for test purposes.

I first looked into adding an initFailure callback to the plugin, but it appears the methods under the hood that the plugin calls don’t return failures back to Unity. I’d like to avoid dealing with a timeout or something like that.

I also messed around with Application.absoluteUrl, but that only references the domain that is hosting the unity3d file, not whether it’s running in Facebook Canvas or not.

Does anyone know of a way to detect this?

Thanks for the help.

Simply wait for FB.Init to call back. If you get no call back then assume you are not running on canvas.

How I typically manage it is not load any FaceBook functionality until the call back is received.

Just in case anyone else needs to do this, I ended up using ExternalEval to get some information I needed. I’m not fully happy with this implementation, but it’s the cleanest way I can find without waiting for an arbitrary amount of time from the Facebook plugin.

First I try this:

Application.ExternalEval( "UnityObject2.instances[0].getUnity().SendMessage('GameObject', '_OnReferrerUrl', document.referrer);" );

And check for “https://apps.facebook.com” in the returned url.

If that doesn’t contain the string, I then try:

Application.ExternalEval( "UnityObject2.instances[0].getUnity().SendMessage('GameObject', '_OnUrl', document.URL);" );

And check for “https://integrated-plugin-canvas” in the url.

Make sure your callback methods take in a string as a parameter.