- Home /
StartCoroutine doesn't work for me to remove momentary unresponsive when loading OBJ file at runtime. Multithreading is a good way to go? any help?
my app. https://www.dropbox.com/s/a6evzf2hw0jzjy1/Backup%20RunTime%20OBJcomplete.rar?dl=0
is a simple runtime obj importer that could also edit it at runtime the problem is it takes. a period of time to load and has a momentary unresponsiveness when loading. the reason of this momentary unresponsive is because of the import script. it gathers data, set it, change it, loops it again and many more in just a short amount of time. the script is from
https://www.assetstore.unity3d.com/en/#!/content/49547
this is how I emplemented corountine:
public IEnumerator ForLoadOBJ()
{
LoadOBJFile(PlayerPrefs.GetString("PathString"));
//the use of PlayerPrefs is just temporary until I could make this unresponsiveness and slow loading go away
yield return null;
}
and the code I use for when I click the import button:
StartCoroutine(obj.ForLoadOBJ());
using the codes above doesn't help.. its just feels like I'm not using Coroutine. unresponsive is still there.
any help regarding to using corountine or multithreading would very much apreciated. any questions ask would be answered as well. please and thank u.
Answer by MrEastwood · Oct 03, 2016 at 02:10 PM
Coroutines still run on the main thread. The only way a coroutine can make your app more responsive is if you can break down the work and then not do all of it at once. So for instance you can put yield return inside of a loop, and then you would do only one iteration of the loop each update instead of all of the iterations in one update.
However this is not the case here. You have a single instruction that is taking up all your time. Without a good point to yield, this really isn't any different from doing that one big instruction in Update.
So if you want to dive into the implementation of LoadOBJFile you could modify it to be coroutine friendly. But i would go with threading here. It will solve your problem but it will give you some new ones too if you are inexperienced with threads. Things to look out for:
Make sure you don't start your thread more than once. Your call will now be asynchronous meaning that the result will arrive at a different time from the call.
Use locking! Don't do things like if (!alreadyLoading) loadTheThing(), because the bool could change in between the test and the call, causing you to load twice even though you checked for it.
Only the main thread is allowed to touch gameobjects and components. So you could load your object asynchronously, but only if it gives you the vertex data. Then, when the data is loaded you take the data on the main thread and only then can you assign it to your meshfilter for example.