Quantcast
Channel: Answers by "gfoot"
Viewing all articles
Browse latest Browse all 95

Answer by gfoot

$
0
0
You can make a coroutine that waits arbitrary lengths of time between processing: private IEnumerator Coro() { while (true) { DoStuff(); yield return new WaitForSeconds(HowLongShouldIWait()); } } public void Start() { StartCoroutine(Coro()); } Not sure if it helps. It is equivalent to, but somewhat tidier than, summing up a float in your Update method and using that to decide whether it's time to "do stuff" yet or not. I prefer it a lot, though, especially compared to InvokeRepeating. If you want more accurate callbacks than once per Update then you need to start your own thread. There is still always a limit to the extent to which you can get called at specific times under a multitasking operating system, let alone within C# code hosted by Unity, but I believe using a separate thread is the best you can do. There are also ways to ask Unity to call your coroutine at different points in the frame, e.g. during the next FixedUpdate or just before presenting a frame to the viewport, but these won't really help you either. Using a separate thread, then, you just loop forever as above but call "Thread.sleep(XXX)" instead of "yield return ..." to wait for a while before continuing processing. It is not entirely accurate, so you probably want to combine it with a Stopwatch so that although you can't *choose* exactly when your code runs, you do *know* exactly when it runs. Also beware of caveats using threads with Unity. I don't know for sure the status now, but in the past it has caused instability, especially if you don't proactively kill the threads when leaving play mode.

Viewing all articles
Browse latest Browse all 95

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>