Unity coroutines stop while in background - c#

my question is the following: currently i have several coroutines running in my game for android/iOS but when i send the game to background in order to try other things with the phone those coroutines stop and only resume after i get back to the game; is there any way to make the coroutines continue running while the game is in background?

Android will suspend your application by design. Co-routines run in the same thread as your Updates so making a distinction between the two in terms of running in the foreground is not too likely. Having said that there are ways around this. You could build a plugin that talks to the android platform or use OnApplicationPause and calculate the time passed in the application and do whatever it is you were wanting to do in the time frame between. The first requiring you do something along the lines of building a plugin
and the second using the following
public void OnApplicationPause(bool paused) {
if(paused) {
// Game is paused, remember the time
} else {
// Game is unpaused, calculate the time passed since the game was paused and use this time to calculate build times of your buildings or how much money the player has gained in the meantime.
}
}
for more info see
this or this.
Also note that building a plugin requires a Pro Licence
Additional Co-Routine resources
Co-Routine Execution Order
Co-Routine Scheduler
Deep Explaination of Co-Routines

You can't make something permanently run in the background. The OS will event eventually pause it. Only VOIP, Media Player or GPS are allowed. Everything else only get an extended time frame until pause.
For iOS you can change "Application does not run in background" in info.plist

Use Application.runInBackground = true;
See Unity Docs Application.runInBackground

Related

how to run code in the background on ios unity

I have a waiting room in unity with photon and when there are enough players a countodown starts, only problem is if the master client is using IOS and they check notification or are not in the app then the game will never starts when it hits 0 until they come back in the game. Is there a way to run the code and force it to start while in the background?
If this is not possible can I use OnApplicationPause() to run code if the OnApplicationPause is more than a certain time(say 5 seconds). So basically if the user leaves for 5 seconds then I would kick them out of the scene and send them to another scene.
Thanks.
I don't know about IOS Unity but in android, you can use a Service to run script in the background
Start android service from Unity3D code
I think this is another way to solve your problem:
When OnApplicationPause() is called, you can send a signal to your
server to run a timer.
When this timer reaching elapse time, your server will kick the pause
guy out of room.
It would better if you run a timer in server instead of client (user's device not work well, there is some case that you can't handle, etc)

Make a unity game look like it has crashed C#

I want to make my Unity game look like it has crashed (Unresponsive Application Crash, Not a code exception crash).
There probably is some function in the Windows API you can call to emulate this.
I have also seen a game called "Pony Island" do this before, so it certainly is possible. However I don't own a copy nor can I decompile a steam game.
If there is a way to actually hang the program while still being able to bring it back at anytime that solution is preferable, this seems to be unlikely.
To be clear, I want it to look legitimate, so I would prefer the windows API be used to change the window color and generate a pop-up, instead of having them in unity as GUI objects or something similar. I also don't really care about cross-compatibility because the Mac API appears to be hard to implement and even harder to test (on a Windows machine), and most likely won't be compatible with as many hacky features as the Windows API is.
Best way to do this safely would be to run some script setting time.timeScale = 0 - effectively 'freezing' the game, while simultaneously playing the windows 'error' sound effect.
(Note: You may have to also pause audio sources and particle emitters from this script, I've not used timescale before an am unsure if it affects these)
I know you suggested against using UGUI but an image with no source placed over the entire screen, with it's colour and alpha tweaked could very easily emulate the 'frozen screen' too.
You could also look into ways to forcibly 'restore'/change fullscreen mode of the window (if it is played in full screen)
I would try to achieve that by using Thread.Sleep(int)
so while in the main thread, i would call that to freeze the entire unity loop for an amount of seconds eg 5 seconds = 5000ms
and check whether you want to continue the crash or not. for example, but not tested, (i don't have unity at my office environment)
Be carefull with this, as this will freeze it for real, you can't do any operation while it still freezing.
public class Crasher : MonoBehaviour
{
public bool isCrash = false;
//The crash in seconds
public float crashDuration = 5;
void Update ()
{
if (isCrash)
{
//Freeze the applicaion for amount of milliSeconds
System.Threading.Thread.Sleep(crashDuration * 1000);
}
}
}

What happens when an app gets supsended?

I'm building an app in Unity3D for iOS. I have huge problems with lag, but for some reason the lag disappears for like 5 seconds after I suspend the app by pulling down the iOS top menu or pressing the home button and then get back to the app again.
What happens when I suspend the app? How does Unity freeze the app? What functions are called? What happens memory-wise etc?
When user press home button or leave Application/Game in any way. It will pause whatever is happening in your game. All of the Update() FixedUpdate() and LateUpdate() methods would pause as well. It will call OnApplicationFocus and OnApplicationPause methods at two times (leaving app and coming back to app). You can implement these methods to do Application state specific behaviour. Having said that, lag in game is related to your scripting logic. I would suggest you to Optimise your app for IOS devices. Here are some useful links for you:
iOS Specific Optimizations
Practical Guide to Optimization for Mobiles
Optimizing graphics performance
IPhone Optimization Tips

Tic Tac Toe how to disable other's move in a period time

I'm just C# newbie C#, and I'm trying to build a Tic Tac Toe game in Windows Forms in which player will play with the computer. Each one will have to make their move in a certain amount of time. If they don't, the turn will skip to the opponent. Here is the tricky part. When it comes to the computer's turn, I wanna disable the player action, so he cannot interfere with the computers. Is it possible to do that ? and how would I implement in my code ?
Edit: I didn't realize that the computer move is almost instantly, so it does not matter if I disable the player or not. Thanks anyone for your inputs.
You can include the play area inside a Container eg: GroupBox and disable/ enable it depending on the requirement. This has to be on the UI thread. Just a suggestion.
With no code its hard to see where you are at.
Just use a Boolean and an if statement to disable player interaction if it is the computers turn.
A Boolean to detect if its the computers turn.
A if statement to disable player interaction if it is the computers turn.
if(computersTurn==false){
Allow UI/Clicking
}
You can do a pause according to voddy's answer, but any computer pause would be purely for effect as Derek mentioned. One simple thing you could do is to just create a timer, and when the timer event fires regularly, switch to the opponents turn and/or disable the UI as required.

Timer to appear on Game Screen

I have made a countdown timer using C# on basic form window and i want it to appear on game screen i tried to set form properties but it wont work on game screen.
how would it be done? Any Idea?
If you are looking to draw a timer into an existing DirectX or OpenGL backed game then I suggest you take a look at the following question which offers information about hooking and the ability to intercept calls along with working with the applications draw calls. However I believe this is above the level of complexity you would wish to encounter for adding a timer.
LINK : OpenGL/DirectX Hook - Similar to FRAPS
As you don't indicate if you are using OpenGL, XNA or what I will give you a generic response.
If it is your game
Your splash screen should be part of the game loop. This means that will have an update() and a draw().
In the update method you have to calculate the remaining time and store it in a variable or property of an object.
In the draw method you should paint the remaining time.
My recommendation is that you create a TimeCounter class because seems you are also going to reuse it during game.
If it is not your game
Most games will switch to full-screen direct-render mode. It is not impossible but pretty hard.
If the game is web, then you can wrap it in IFrame or similar and put the counter

Categories