Might not be answering your question so far but what I meant with the comment is
currently you 1. use the wait coroutine wrong and 2. it doesn't do anything
To 1.:
public void Save()
{
ScreenCapture.CaptureScreenshot(@".\" + counter + ".png");
CoroutineAction();
Vector3 location = Camera.main.WorldToScreenPoint(gameObject.transform.position);
System.IO.File.WriteAllText(@".\" + counter + ".json", toJson(location));
counter++;
}
What happens actually is:
ScreenCapture.CaptureScreenshot(@".\" + counter + ".png");
Vector3 location = Camera.main.WorldToScreenPoint(gameObject.transform.position);
System.IO.File.WriteAllText(@".\" + counter + ".json", toJson(location));
counter++;
is executed immediately. Than "parallel" (not really ofcourse) the coroutine would run if you used StartCoroutine(CoroutineAction());
But now to 2.:
the routine
public IEnumerator CoroutineAction()
{
yield return StartCoroutine(WaitFor.Frames(3));
}
does absolutetly nothing than waiting 3 frames ...
I guess what you wanted to do instead is something like
// actually start a coroutine
public void Save()
{
StartCoroutine(SaveRoutine());
}
// optional parameter superSize => if not provided uses 1
private IEnumerator SaveRoutine(int superSize = 1)
{
// makes sure the factor is always >= 1
var sizeFactor = Mathf.Max(1,superSize);
ScreenCapture.CaptureScreenshot(@".\" + counter + ".png", sizeFactor);
//Wait for 3 frames
// I think this makes more sense than your dedicated class with the static method
for (int i = 0; i < 4; i++)
{
yield return null;
}
Vector3 location = Camera.main.WorldToScreenPoint(gameObject.transform.position) * sizeFactor;
System.IO.File.WriteAllText(@".\" + counter + ".json", toJson(location));
counter++;
}
Than as also said already I'm not sure because I can't tell if you should get the location in the frame where you start the capture or in the frame where you end. But I guess you rather should get the location beforehand and than start the capture:
// optional parameter superSize => if not provided uses 1
private IEnumerator SaveRoutine(int superSize = 1)
{
// make sure the factor is always at least 1
var sizeFactor = Mathf.Max(1,superSize);
var location = Camera.main.WorldToScreenPoint(gameObject.transform.position) * sizeFactor;
ScreenCapture.CaptureScreenshot(@".\" + counter + ".png", sizeFactor);
//Wait for 3 frames
// I think this makes more sense than your dedicated class with the static method
for (int i = 0; i < 4; i++)
{
yield return null;
}
System.IO.File.WriteAllText(@".\" + counter + ".json", toJson(location));
counter++;
}
Also note that as mentioned in the linked duplicate
The CaptureScreenshot returns immediately on Android. The screen capture continues in the background. The resulting screen shot is saved in the file system after a few seconds.
You are waiting 3 Frames .. that's usully not even a 1/10 of a second (depending on the frame rate obviously).
You should rather wait for actual seconds using WaitForSeconds
e.g.
yield return new WaitForSeconds(3);
to wait e.g. 3 seconds. BUT also note that this will not work in combination with suggested Time.timeScale
since WaitForSecodns
depends on the timeScale as well.
I think you also should not / don't need to use
@".\" + counter + ".png"
in the filename but simply
counter + ".png"
since
On mobile platforms the filename is appended to the persistent data path. See Application.persistentDataPath for more information.