Make a unity game look like it has crashed C# - 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);
}
}
}

Related

FPS drop after few minutes in the Android game

I have made a very simple hypercasual game everything works fine but after some few minutes of gameplay, the fps goes from 60 to 50 even the phone gets heated up. Similar to this question. I tried profiling but just can't see anything off. Tried even removing some UI elements but still no luck. Tried various vsync settings. Also, I had used this to display the fps. Even without it, the lag can be seen. Even if I just open the game and do nothing then after 5 minutes the fps will become 50. If go back using the home button and re-enter the game then the fps becomes 60 again. Using unity 2018.2.6f1. Never experienced this behavior in my other Android games.
Basically it was a faulty custom vertex shader which was applied to a plane to change the background color which changed color over time. I had not used the mobile vertex color because I was not getting the desired output. But now I'll stick to the mobile one.
The two symptoms you observed are very much likely to be connected.
The phone might heat up, as you are using its full power, which in turn makes the throttling kick in, reducing the perform
I've had the EXACTLY same problem. I was trying to fix it for a very long time. You said something about faulty shaders you use. And this is the key to solve our problem.
I use a 2-color gradient as a BG, so I have to use a shader too. Due to the fact that I'm a total noob in the writing "shader-code", I have to find something in the Internet. And it was my biggest fail)
To fix the problem and remove this fps drop you should remove your gradient and shader attached to it from the scene. And try to find a more optimized shader for 2D-game (or you can always write your own one c:)

Monogame SoundEffect Crash

I am trying to re-program some old Arcade games in C# with the Monogame engine. However, I have encountered a certain problem.
Since my code tends to be a bit messed up, I am often not taking the effort to reset everything when the player successfully completes the game. Instead, I am simply closing the current Game-instance and opening a new one, like this: (in Program.cs)
if (startgame)
{
do
{
using (var game = new Game1(level, points, soundOn))
game.Run();
} while (continueGame == true);
}
Now the problem. In Game1, I am declaring and playing various SoundEffects. The first run everything works fine, but in all following Game1-instances, my program will always throw an System.AccessViolationException related to SharpDX.XAudio2.dll at the moment I am calling the .Play()-Method of a SoundEffect.
I tried playing SoundEffectInstances instead of the actual SoundEffects. Now it doesn't crash anymore, but is being completely silent from level 2 on instead.
Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Thank you in advance.
(I am using Win 7 64 Bit, VS Express 2015 and Monogame 3.6)
Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Yes. Do not do it.
You are destroying the whole game object just to reset the game. This is a very bad style, because it destroys the whole instance and everything that is connected to it. If you use static variables within it, they will remain in memory which may be the problem you have.
If you want to reset your game, simply write a Reset-method for everything and call these instead. Cleanup and nice code is something you should use instead of terrible hacks.
Can you show more code, so it is possible to understand, what went wrong?
PS: Never write something like while (continueGame == true) always go for while (continueGame).
There is a chance that the issue is due to SharpDX's reaction to the media file itself. For instance, an MP3 file that would work perfectly fine under XNA 4.0 loaded as a SoundEffect (with associated SoundEffectInstance) may crash the game under MonoGame UWP. (This happened to me with one file, but I was already using SoundEffectInstance in the first place when the crashing started.)
As such, have you tried re-encoding your relevant audio files, such as through a utility like Audacity? Although it may not solve the internal issue per se (some element of the audio file breaking SharpDX), if a re-encoded file doesn't have the problem element in the first place, that should be a valid solution for you.

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

Unity coroutines stop while in background

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

How to refresh my grid? and How to calculate the amount of time?

I am trying to create a simple rigid body 2D physics engine.
I was able to create a rectangle image by using four lines and able to manipulate the image according to its angle and position; I can move and rotate it (though I made it possible to rotate I do not use the rotating function since I am unable to comprehend angular momentum theory yet).
The image will fall and bounce back based on simple formula:
v(velocity) += a(acceleration)
x += v(velocity)
But I have to click a button every time I want execute a movement.
I want it to execute itself automatically and update automatically, I tried to use loop but for some reason program seems to stop during the time it is in loop section. And because I use an infinite loop that will start over and over again, my program just freezes.
Not only this is the problem but also my good friend, who has better knowledge in physics, told me that I should be able to calculate the amount of time if I want to make this engine work properly.
I like DJ KRAZE's comment - use a System.Windows.Threading.DispatcherTimer, and set the interval to however often you want the picture to redraw. On the timer's Tick event, do your redraw.
If you want to know why the simple loop doesn't work, you have to know a little bit about the Windows message loop. For a brief and somewhat inaccurate overview, all of stuff running in your application, it sounds like, is happening on one thread. Windows uses a message WM_PAINT, which goes through the message loop, to make the control paint itself. It only processes on message at a time.
Where you have your infinite loop, that is happening in the processing of some message. So this keeps the message loop from processing any messages, including messages for the controls to paint. So you don't see anything updating in your infinite loop.
Perhaps you could look into XNA, it's a Microsoft platform that facilitates games development, you should go to the App Hub for information on how to get started.
As for your specific question on refreshing the screen, you can take a look at the Update and Draw methods. Here's a tutorial that, although may not address your specific question, should give you the know-to on how to refresh the screen.

Categories