Abort Frame?


It would be nice to have a code that aborts the frame or presses pause if a frame is taking too long…

for example, if i use print command 8000 times in a loop by mistake, it would be nice if unity didnt crash.

is there a way around it? can;t unity just print “this frame has just printed 100 items, do you want to continue ?”

I don’t think that is possible now.

Takes time to develop a framework that protect itself against bad programming. Even if you are able to create one, it will start creating bad habits from the programmers (example, they need to add #pragma strict). It is better to have the code clean and simple and expect that programer that use your framework is your friend. If he do something wrong let him know (or crash until he fix it).

For these types of problem I see 2 solutions.

  1. Change your algorithm to run into a Courotine and give time for app to check if the process take to much time. Here is some draft code. You need to test if works. System time in old times was very trick in performance and accuracy.

Change your algorithm by returning a yield from times to times.

IEnumerator DoFractal() {
	for ... {
	  yield return 0;
	while ...
	  yield return 0;

Then create a executor that check the time that takes during the frame, if it take to long, continue in next frame.

IEnumerator Execute(IEnumerator coroutine) {
	float maxMilliseconds = 10f;
	var start = System.DateTime.Now;
	while (true) {
		if (!coroutine.MoveNext()) {
		var now = System.DateTime.Now;
		if ((System.DateTime.Now - start).TotalMilliseconds < maxTime) {
			// continue next frame
			yield return 0;
			start = System.DateTime.Now;


Some references


  1. Simple run your code outside the main thread.