Accessing session cookie with WebGL build

Hi,

We have a game running on the unity web player, embedded in our website. The user logs in to the website, and the game then uses the session cookie created to authenticate itself. This is achieved by the javascript injecting the session cookie into the native plugin, as specified by http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html

Everything works great now, but now that NPAPI support is being deprecated, we want to switch over to using WebGL. The trouble is, the javascript generated by WebGL build does not have an interface to plug in the session cookie, and so we are unable to find a way to inject session cookies into WebGL builds. Has anyone else faced this issue? Thanks.

The setup should be very similar to the web player. See: Unity - Manual: Interaction with browser scripting

Great, thank you.

My javascript code to save and read cookies dont work on WebGL, anybody knows why? here is the code

    setCookie: function (cname, cvalue, exdays) {
       var d = new Date();
       d.setTime(d.getTime() + (exdays*24*60*60*1000));
       var expires = "expires="+ d.toUTCString();
       document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
       console.log('set cookie='+document.cookie);
    },

    getCookie: function (cname) {
       var ret="";
       var name = cname + "=";
       var decodedCookie = decodeURIComponent(document.cookie);
       console.log('get cookie='+decodedCookie);
       var ca = decodedCookie.split(';');
       for(var i = 0; i <ca.length; i++) {
           var c = ca[i];
           while (c.charAt(0) == ' ') {
               c = c.substring(1);
           }
           if (c.indexOf(name) == 0) {
               ret=c.substring(name.length, c.length);
               break;
           }
       }
       var buffer = _malloc(lengthBytesUTF8(ret) + 1);
       writeStringToMemory(ret, buffer);
       return buffer;
    },

Hello nsmith1024, your javascript cookie functions aren’t working because you need to cast your function input strings like so:

setCookie:
…
document.cookie = Pointer_stringify(cname) + β€œ=” + Pointer_stringify(cvalue) + expires + β€œ;path=/”;

and getCookie:
…
var name = Pointer_stringify(cname) + β€œ=”;

1 Like