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
Related
I'm working on a Unity project that uses the device camera for AR purposes (I'm using AR Foundation). I currently have it set so that the device won't go to sleep (because I want it to operate for long-periods without user-interaction). Specifically, Screen.sleepTImeout = SleepTimeout.NeverSleep. If I didn't have this, then the device would go to sleep, and the program would stop functioning. Does anyone know if there is a way to keep the program working in the background after the device goes to sleep...similar to how music apps keep playing music? I've come up empty handed after a few hours of searching for a solution. Any help would be greatly appreciated!!!
Some people aren't finding this in more recent versions of Unity. But it's still there: In Player settings, under the section "Resolution and Presentation", you find "Resolution" and below that, there's "Run In Background".
This is only available for Web players and standalones (Anything Except IOS And Android Pretty Much), though (you won't find it when you have the iOS, Android or Flash tab selected.
I am search for a way to detect if video is playing on Windows OS (7,8,10).
SetThreadExecutionState api function does not help I have tryied with different players (VLC, BS player etc.) but it seems they don't use flag ES_DISPLAY_REQUIRED.
Checking for disabled screensaver is not good solution, because it should be allowed at first place and almost nobody use screensavers nowdays.
My app is a break timer, I am using LASTINPUTINFO() function but I want to know
when the user is watching video because there is not input (keyboard or mouse) during this time.
A dirty and partial solution would be if the app does snapshot of region in the center of the screen and comparing hashes, but it will be 90% accurate.
Any better ideas?
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);
}
}
}
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
I am a C# developer working on conversions to Mono Android. These conversions work perfectly, except for when the phone times out or blacks out and the user touches the screen again.... The game is lost.
What is the simplest way of dealing with this? I have heard it referred to as the life cycle, and found many brief descriptions of this cycle without explanations of how to implement it with real examples, and especially, how to implement it with XNA conversions.
My first solution would be to have a 'Pause' method in the XNA game, and run that method for the 'onPause' of the Android. However there may be simpler ways of dealing with this, as I have heard there are simple Mono Android settings to pick that will deal with time outs and phone calls automatically.
What I ask is.. How do I make my XnA conversion continue to run through a phone call, screen timeouts, etc?
Might I ask if you are using MonoGame or simply the AndroidGameView that comes with Mono for Android?
If you are using AndroidGameView, I suggest you follow the convention of reloading the textures manually in OnLoad() as per the textured cube sample.
When using MonoGame, if you load textures via ContentManager.Load<Texture2D>(), reloading is handled for you. Textures loaded using Texture2D.FromStream must be manually reloaded in the GraphicsDevice.DeviceReset event.
As Andrew Russell pointed out, ExEn has the advantage that it doesn't need to reload the textures on every resume. However, it is still necessary (or good practice at least) to support reloading graphics resources on Android. On many devices your game will not be able to gracefully recover from switching away and back without it. ExEn currently has no support for reloading textures.
Xamarin are currently working on fixing a bug in AndroidGameView that will allow it to correctly resume where the device supports it. Once released, this should flow through to MonoGame.
Aside from reloading textures and other graphics resources, handling of the life cycle should be fairly trivial. MonoGame has not yet implemented the complete Windows Phone life cycle (ie the tombstone and rehydrate), but in my experience, it's not necessary. You can use the Game.Activated and Game.Deactivated events for things like showing the pause screen when resuming in-game.