Unity WebGL OnApplicationQuit()? - unity3d

I need to remove player from database when he closes tab with WebGL game. I tried using OnApplicationQuit(), but that does not fire. Is there something similar that can be used for WebGL to call some logic when player closes the tab?

For anyone else who might struggle with this I solved it in a following way:
Inside WebGL template index.html i added this code ( SendMessage - https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html )
window.onbeforeunload = () => {
unityInstance.SendMessage("CloseEvent", "CloseCallback");
return "Are you sure to leave this page?";
}
in my case I put this code inside <script> after this code ( that's where unityInstance is assigned from above )
script.onload = () => {
createUnityInstance
....
Important part for me was to have the return there, which shows alert window. That allows me to call database and make changes. Without the alert window the db call does not go through for some reason. Btw you can't change the text of the alert window unfortunately because of browser security changes.
Hope this helps someone else in the future. Spent full day on this.

Related

Facebook instant games api method canSubscribeBotAsync method is not a function

So my game wants the players to have the ability to subscribe to a game bot and I'm using FBInstant.player.canSubscribeBotAsync() method. But everytime, it will return an error that state
Uncaught TypeError: FBInstant.player.canSubscribeBotAsync is not a function
here is my code
FBInstant.player.canSubscribeBotAsync().then(function(yes){
if(yes){
FBInstant.player.subscribeBotAsync().then(function(){
console.log('sub');
}).catch(function(e)){}
}
});
I tried using this code from FB developer doc
FBInstant.player.canSubscribeBotAsync().then(
can_subscribe => console.log(can_subscribe)
);
but the result still the same.
It seems that I tried to let the player subscribe when they click the button and this error appears.
But when I switch it from letting the player click the subscribe button to asked when player finished loading the game, the error disappears. The program's flow look like this
window.onload = async () => {
FBInstant.initializeAsync().then(()=>{
FBInstant.startGameAsync().then((res)=>{
FBInstant.player.canSubscribeBotAsync().then((canSub)=>{
//Ask for sub or something
}).catch(()=>console.log("Error"))
})
})
But for me, this is a temporary fix since normally you should be able to call a function anywhere you want (working or not isn't my problem) and my problem is that I can only call FBInstant.player.canSubscribeBotAsync() inside FBInstant.initializeAsync() or FBInstant.startGameAsync() but not outside of them

Issue with setting AutomationElement value

I have an issue with setting value of AutomationElement by using method ValuePattern.SetValue().
Everything works just fine until some dialog appears. When the dialog appears the code execution got stuck. No exception is thrown. After the dialog is confirmed, the code exection continues. Bellow is a sample of the code:
BasePattern basePattern = null;
ValuePattern valuePattern = null;
AutomationElement elementA = Window.GetElement(SearchCriteria.ByText(propertyName));
object patternObjectA = null;
elementA.TryGetCurrentPattern(ValuePattern.Pattern, out patternObjectA);
basePattern = (BasePattern)patternObjectA;
valuePattern = (ValuePattern)patternObjectA;
valuePattern.SetValue(optionToSet);
// Window.GetElement() is a method from TestStack.White framework
// The code execution got stuck on the last line until the dialog is confirmed
Is there any other way to set AutomationElement value?
Is somehow possible to avoid of getting stuck by dialog?
I'll by grateful for any help.
Thanks advance.
It could be that this dialog is not supporting UI Automation correctly or that you simply target the wrong element.
To verify that you may use Inspect.exe from Microsoft or similiar tools.
If it works, check if you really target the correct component with your code again.
If it does not work and:
if you are able to change the application
you can change the so called AutomationPeer of the UI component - here is a link for more infos
Or simply use another UI component that supports UI Automation correctly.
if you are not able to change the application, and also do not need to run in background, parallel, etc.. you might just focus the component (call setFocus() onto the AutomationElement, or expand it (via IsExpandCollapsePatternAvailable or simulated MouseClick onto the components coordinates)) and then use the SendKeys.SendWait("test") method.
EDIT: There is one more thing you should have a look at, and I wonder why I didn't mentioned it in the first place: Register to UI Automation Events
For example you could register a callback for the Structure change event type, and check if the dialog you talk about appeared.
If so --> click the confirmed button of the dialog.
Probably you will have to synchronize your execution, so that every further action in the UI Automation script waits until the registered callback got executed and the confirmed button got clicked.

Ionic content not updating after callback from external plugin

As part of the functionality of the app we are developing, when an android alarm is fired, a dialog box is to appear with an "Accept" or "Reject" button. Selecting reject does nothing, but selecting "Accept" triggers a callback from the plugin, which I have passed a function into. This function causes the ionic app to navigate to the root page of the app.
The issue I am having is, when I then navigate to another page after that where the user selects a value, and this value is displayed back to them and a button is enabled, the value display is not updating, and the button is not becoming enabled. Nothing seems to be updating.
What I have found is that pressing the back button on my android device will cause the page to update, which is not ideal.
This functionality works without the callback from the plugin.
What is happening here? And how do I fix it?
Passing the function into the plugin.
alarms_plugin.onAlarmRecieved = (alarmId) =>{
this.events.publish('alarmRecieved', alarmId);
}
Plugin-side functions
alarmRecieved: function(alarmId){
alarms_plugin.onAlarmRecieved(alarmId);
}
onAlarmRecieved: null
Navigating to root page on alarmRecieved
this.events.subscribe('alarmRecieved', (alarmId) =>{
if(alarmId != 'TIMEOUT')
this.nav.setRoot(HomePage);
});
Fix found by surrounding the this.nav.setRoot(HomePage) in this.zone.run(..).
Supposedly calling navigation within a subscription event causes issues like I was having.

casperjs cant click popup

My code:
casper.waitForSelector('.single_like_button.btn3-wrap .btn3',
function success() {
this.click('.single_like_button.btn3-wrap .btn3');
}
);
Returned:
TypeError: 'undefined' is not an object (evaluating 'window.angular.version')
https://s.ytimg.com/yts/jsbin/www-en_US-vfl2odRpD/angular_base.js:167
https://s.ytimg.com/yts/jsbin/www-en_US-vfl2odRpD/angular_base.js:225
the button when clicked, will open a second tab window, like a popup,
when the second window finished its works, it will close automatically and transfer data back to 1st window.
I dont know if this process had used angular js?
and do i need to install angularjs and how?
beacuse the error seemed related to angularjs
the popup url is inside a javascript's function, i dont know how to scrape it
I dont get the popup url, so i dont know how to use "waitforpopup"
but even if i can get the url, i still have to click the button in order to the correct data transfer. I have used clickLabel("Subscribe", div) , also not working.
Any advices?
I've had issues with waitForSelector before.. Although I'm not familiar with your html/js, why not try the following in place of your code above to do a quick sanity check. This will give you a better idea of what's happening.
this.wait(5000,function(){ // use wait (5 secs) instead of waitForSelector
// log the el to see if it exists at this point
console.log($('.single_like_button.btn3-wrap .btn3').length + " els found");
// click dat
this.click('.single_like_button.btn3-wrap .btn3');
});

Touch events in Meteor

The first thing I started to do with Meteor was start writing a touch-based web application for mobile/tablets. Here's how you reproduce the problem:
First step: create a blank project
meteor create touch_example
cd touch_example
meteor
Second, add these things to the .js file This first bit spits out an alert for touch devices and because they have no console.
Meteor.log = function(input){
if (typeof console !== 'undefined' && typeof Touch !== "object")
console.log(input);
else
alert(input);
}
And here's the culprit.
Template.touchbox.events = {
'touchmove' : function (e){
e.preventDefault();
Meteor.log('touchy');
}
};
Last step, change the template around so there's at least one "touchbox" div on the page. IN theory, it should be taking the events. You'll notice that if you change 'touchmove' to 'click' that it works just fine. If you change it to dblclick it will also work fine. Touch events don't do anything.
This may help. I was looking for a long time to get the click working on mobile for my meteor app. I discovered you can add a comma after your click, and add touchstart
Template.red.events({
'click, touchstart .red': function(event){
// add function
}
Looks like jQuery as standard doesn't handle these, try using jquery-mobile: http://jquerymobile.com/demos/1.1.0/docs/api/events.html
To add it as a package you might want to look at my other answer here:
jQuery-Mobile Meteor sample integration and/or integration guidelines
I found some other help regarding binding touches the same as mouse events however this uses bind and I doubt that will hook up with the live-ui meteor stuff:
http://xavi.co/articles/trouble-with-touch-events-jquery