Hello. i have a working javascript sharing function (port from another project) that i’d like to use in my webgl project but i dont think it is suitable with unity’s webgl.
i have a Pugins folder in the Assets with the .jslib file. in the file :
mergeInto(LibraryManager.library, {
Sharing: function () {
if (navigator.share)
{
navigator.share({
title: 'Street Food Festival',
text: 'Check out it out',
url: 'ur',
}).then(() => console.log('Successful share')).catch((error) => console.log('Error sharing', error));
}
else
{
alert('Sorry. Sharing not available on your device')
}
}
});
and a button click is suppose to trigger it. but when i try to build and run to test, unity throws errors. im pretty sure its the Sharing function but im not sure how to fix it to make it work with unity webgl.
One thing that catches my eye is that .jslib files do not currently support ES6 syntax. Try reverting back to ES5 constructs instead. In particular the anynymous => lambda construct is not there for ES5.
i have corrected my mistake in the hopes that it would build and run but the error is basically just that it failed to build. no reference to any code or anything like that.
Oh hmm, there is an error with BuildPostProcessor.cs executing. That certainly means the ES5 vs ES6 issue, since BuildPostProcessor.cs will call out to a ES5 based JS parser.
Try double checking the contents of the .jslib file and reduce it to a minimum that would not exhibit the issue, then try to build up the code again to find the offending construct?
I have the same problem, so I append the sharing script on the Build’s index.html (You can also append it in your WebglTemplates index.html to have this code automatically generated for future builds)
function TriggerShareDialogue() {
if(navigator.share)
{
navigator.share({
title: "Your web title",
text: "Your web description",
url: "Your web url",
}).then(() => console.log("share success")).catch((error) => console.log(error));
}
else
{
alert('Sorry. Sharing not available on your device');
}
}
Next, in your jslib file inside Plugins folder, add this function
mergeInto(LibraryManager.library, {
Share: function () {
document.documentElement.addEventListener('pointerup', function () {
TriggerShareDialogue();
}, { once: true });
},
});
which will call the TriggerShareDialogue() function defined in the index.html
Finally to call the Share function in your c# script, you can write