- Home /
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.
Answer by DudeGuy · Jan 15, 2015 at 07:14 PM
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.
Answer by Kiwasi · Nov 14, 2014 at 08:42 PM
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.
Sorry, I ended up getting pulled off this project right after posting this. Unfortunately, I had mentioned I wanted to avoid this solution in the original post.
I feel the Facebook SD$$anonymous$$ should be responsible for reporting any errors back to the user ins$$anonymous$$d of failing silently. I'll give them some feedback on that very issue.
Thanks for trying to help, though. $$anonymous$$uch appreciated. If I can find a way to detect this, I'll post back here in the future.