Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lethael · Jul 06, 2018 at 12:56 PM · coroutinesthreadingglobal illuminationasynchronous

Multi-threading Alternative

Hello,
I would like to be able to call DynamicGI.UpdateEnvironment asynchronously somehow. Unfortunately this isn't possible in a new Thread, as I am given an error telling me that it can only be called in the main thread.

If anyone knows of any alternative ways to perform this task, please let me know!

I have heard you could possibly use a coroutine to do this, but I'm not quite sure how to go about using this method.


Any help or ideas would be appreciated.
Cheers.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sonky108 · Jul 06, 2018 at 02:01 PM 0
Share

I am not sure how Coroutines would help in the case since they are proceed using one thread. Check the link below: https://support.unity3d.com/hc/en-us/articles/208707516-Why-should-I-use-Threads-ins$$anonymous$$d-of-Coroutines-

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JVene · Jul 07, 2018 at 05:37 AM

This situation comes about in a number of paradigms, including typical GUI based applications. For GUI's, one usually posts a message for the main UI thread to act upon.

The main thread in Unity is that which calls the various Update or FixedUpdate functions, so you can work this as simply as setting a bool (or some other state value) indicating the thread has requested a function to be called in the next Update or FixedUpdate of some appropriate object. Then, of course, that Update must read that bool, and call the function which performs DynamicGI.UpdateEnvironment, and resetting the bool.

You'll need to consider declaring that bool volatile, though quite often you won't really notice why that is important. I'll leave you to search the keyword to dive into the general topic of threading if it is unfamiliar.

Of course, this is all too simple if there are many such functions to call. An alternative, more generalized approach might be to create a class representing a delegate and parameters, possibly using generics if required. In this way threads could post such an object to a queue owned by an appropriate object. The queue represents a list of functions to be called on the main thread, probably in the next update.

Such a queue would require the kind of protections typical of threaded programming, as it will be accessed by competing threads. The design is beyond the scope of a post, but the basic idea is to use an array, likely constructed as a circular queue, or some kind of deque container, to receive requests from various threads. The Update function would then consume this container, performing each function in the list, leaving it empty.

There is a class created by pdwitte (search for UnityMainThreadDispatcher) that provides a service for this kind of situation.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lethael · Jul 07, 2018 at 06:55 AM 2
Share

Thanks for the answer.
This seems alot more difficult than I thought it would be, to the point where I might try avoid it. I should've mentioned, the reason why I wanted to call DynamicGI.UpdateEnvironment from another thread was to prevent blocking the main thread, as it causes the game to 'freeze' due to the intense calculations it performs. So doing something like setting a bool would still call from the main thread, not achieving what I was hoping to achieve.

Since I was using DynamicGI.UpdateEnvironment for a day-night cycle, I decided to try write a script which caches the ambient probe at the different skybox states (Sunrise, mid-day, sunset, and night), lerps between them based on the time, then sets the RenderSettings.ambientProbe to it. The result of doing this was actually better than I expected, and there is barely any performance hit doing it this way (Apart from in the Start method, I had to call DynamicGI.UpdateEnvironment four times in total) So I may stick with this method ins$$anonymous$$d.
Thanks again for your input.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to update a progress bar for ANY asynchronous operation? 2 Answers

Multi threading problem ! 2 Answers

Optimizing the generation of a procedural world - 50ms lag spike to 1 Answer

Does UnityEngine.CustomeYieldInstruction works in a seperate thread? 2 Answers

Couroutines and Threading for Procedural Generation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges