3

Im currently facing an issue where I am using the Facebook plugin v.7.1.0 for Unity. Im distributing through WebGL, but my app needs to be running both on Facebook, but also outside Facebook. When using FB.Init I get a successful callback, which is what I used to use for testing whether on Facebook canvas or not when deploying for WebPlayer.

So my question is, how do I detect whether the WebGL player is on the facebook canvas or not?

gman
  • 100,619
  • 31
  • 269
  • 393
HrLarsen
  • 105
  • 10

2 Answers2

3

This solution is just posted for anyone who would like the same solution as I used.

The solution was to use a jslib plugin as described here http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

var FacebookCanvasChecker = {
    GetSite: function()
    {
        var url = document.URL;
        var returnStr = "";
        if (url.indexOf("apps.facebook.com") > -1)
            returnStr = "facebook";
        else
        {
            // try with referer
            if(document.referrer)
            {
                url = document.referrer;
                if (url.indexOf("apps.facebook.com") > -1)
                    returnStr = "facebook";
                else
                    returnStr = url;
            }
            else
                returnStr = url;
        }

        var buffer = _malloc(returnStr.length + 1);
        writeStringToMemory(returnStr, buffer);
        return buffer;
    }
};

mergeInto(LibraryManager.library, FacebookCanvasChecker);
HrLarsen
  • 105
  • 10
2

In Unity's WebGL, you can communicate with javascript. link

So, I call a javascript function to check current url by

C#

Application.ExternalCall("GetCurrentUrlType");

JS

function GetCurrentUrlType(){
  var url = document.URL;
  if (url.indexOf("apps.facebook.com") > -1)
    SendMessage("GameObject", "CheckURL", "facebook");
}

Hope this help!!!

Ar Aui
  • 392
  • 1
  • 7
  • 17