Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Uzquiano · Mar 17, 2011 at 01:15 PM · coroutineefficiencythreadsblinking

Blinking & resource consumption

Hi,

I am coding some "blinking" stuff, which I can find many threads here, but any refering to the efficiency of those methods. So the too main options are:

i) Use yield WaitForSeconds() ii) Use a code I found in this thread, checking if it has passed enough time in the Update() function

var rate = .5; // seconds
var lastCheck = 0.0;
Update(){
if ((Time.time - lastCheck) >= rate)
  {
    Do Something
    lastCheck = Time.time;
  }
}

Even I saw an infinite loop placed in the Start() function, which I don't know how effiencient is that.

So, my question is, what is more efficient?

Other question I want to ask is about the 'yield' instruction. If i call yield WaitForSeconds() the execution thread is paused, isn't it?

And to make all my picture about 'threads' and 'yields' clear, there is one thread (per script) executing its script, isn't it?

Thanks in advance

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Mike 3 · Mar 17, 2011 at 02:24 PM

There is one thread total (when it comes to scripts) - each script is run sequentially.

Yielding is done by giving back execution after each yield to unity, and then the engine queues it up for calling back again - it is completely non blocking.

I would tend to use InvokeRepeating for your above example, it's called in a way which keeps the function calls equal length, doing it with WaitForSeconds in a loop will slowly go out of sync (as it'll usually be called after seconds + <= frametime)

There wouldn't be much difference between your update example and a coroutine, though coroutines have the benefit of being able to be stopped, which will be a benefit over Update which has to keep checking conditions

Comment
Add comment · Show 3 · 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 Uzquiano · Mar 17, 2011 at 03:00 PM 0
Share

Thank you very much $$anonymous$$ike for your fast, accuarate and professional answer. Tremendous help, I tried to vote you up more than once but it didn't work ;)

avatar image Uzquiano · Mar 18, 2011 at 12:34 PM 0
Share

Hi, Unfortunately for GUI functions InvokeRepeating is not an option since GUI functions must be called inside OnGUI()

avatar image Mike 3 · Mar 18, 2011 at 01:29 PM 0
Share

Set a bool in the invoked function, check if that bool is true or false in OnGUI

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

No one has followed this question yet.

Related Questions

Threads? Or just Coroutines? 0 Answers

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

Why does my EventHandler listener method not finish execution? 1 Answer

Cost of StopCoroutine() 0 Answers

Animation Curve or Coroutine? Performance question. 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