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 newborne · Sep 21, 2012 at 09:52 AM · c#countdownactionprogress

Passing Func event as parametar

Hi this is more a C# question but might be useful to people using unity. I am writing the countdown script that is going to be used like this:

 //simple countdown duration:10s, when done call OnFinish
 this.gameObject.AddComponent<Countdown>().StartCountdown(10,OnFinished);
 //overloaded, countdown duration:10s when done call OnFinish, report in 1s call OnProgress
 this.gameObject.AddComponent<Countdown>().StartCountdown(10,OnFinished,1,OnProgress);

countdown script is attached to the GO that needs countdown and destroys itself when done. I managed to use simple Action event for OnFinish and OnProgress but i could not get the Func to work since i want to return some values to the script that uses the countdown.

here is the current countdown script, help me expand this and learn along the way! thanks!

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class Countdown : MonoBehaviour {
 
     float startTime;
     float elapsedTime;
     
     event Action Finished;
     event Action Progress;
     int progressCount;
     
     public void StartCountdown(int seconds, Action OnFinished)
     {
         Finished += OnFinished;
         StartCoroutine(Count(seconds));
     }
     
     public void StartCountdown(int seconds, Action OnFinished, int progress, Action OnProgress)
     {
         Finished += OnFinished;
         Progress += OnProgress;
         progressCount = (int)(seconds / progress);
         StartCoroutine(CountWithProgress(seconds,progress));
     }
     
     IEnumerator Count(int duration)
     {
         startTime = Time.time;
         
         while(elapsedTime < duration)
         {
             elapsedTime = startTime + Time.time;
             yield return null;
         }
         
         if(Finished != null)
             Finished();
         
         Destroy(this);
     }
     
     IEnumerator CountWithProgress(int duration, int progress)
     {
         int i = 1;
         startTime = Time.time;
         
         while(elapsedTime < duration)
         {
             elapsedTime = startTime + Time.time;
             
             if(elapsedTime > progress * i)
             {
                 if(Progress != null)
                     Progress();
                 
                 i++;
             }
             
             yield return null;
         }
         
         if(Finished != null)
             Finished();
         
         Destroy(this);
     }
 }
 
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 Bunny83 · Sep 21, 2012 at 10:22 AM

Just use a generic `Func<>` delegate instead of an Action. When you look at the left side of the MSDN page you will see there are a lot different Func and Action delegates. Those are to specify the parameters and the return type of your delegate.

Btw, i would expect the OnProgress delegate to get the current countdown value as parameter.

ps. your elapsedTime calculation is wrong ;) It should be:

 elapsedTime = Time.time - startTime ;


See this Stackoverflow question for more information about Action and Func delegates ;)

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 newborne · Sep 21, 2012 at 10:55 AM 0
Share

everything noted! thanks for your help!

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Does passing an Action as a parameter create garbage? 1 Answer

How to reset timer? (C#) 3 Answers

IEnumerator only called once 3 Answers


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