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 Serellyn · Jun 04, 2018 at 12:09 PM · programmingtimers

Large amount of timers

Hey guys,

I want to create a mobile application that can use a variable amount of "timers" but should at least be able to time at least 100 items at a time without any problems, this is what I want, but, is it also possible? There are for example 120 items, each item has it's own 'time to completion'. For each item I want to show a timer.

For above example, I think the creation of 120 timers will be the death of any device, but what would be a good way to approach this?

Comment
Add comment · Show 3
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 Harinezumi · Jun 04, 2018 at 01:14 PM 1
Share

Not really, a timer is just a float variable, and displaying it is just the visual representation. A 120 visual items is really not much (depending on the complexity), many games display thousands of items at the same time.
Anyway, best is to try it, and if you find that it doesn't work, then profile it and check what causes the performance problem. As the old program$$anonymous$$g saying goes "premature optimization is the root of all evil".

avatar image Serellyn Harinezumi · Jun 04, 2018 at 01:22 PM 0
Share

Haha alright, but what makes more sense. Using unity's deltaTime in Update or using C#'s Timer function?

avatar image Harinezumi Serellyn · Jun 04, 2018 at 01:44 PM 1
Share

Depends what level of precision you want. If it is for a game, Time.deltaTime should be fine, even though repeatedly sum$$anonymous$$g floats accumulates precision error over time. C#'s Timer is more precise, but it is also more performance heavy (as far as I know), and it measures real time, so it might not be the best if you also need to pause the game (for example, you enter an in-game menu).

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Summit_Peak · Jun 04, 2018 at 01:42 PM

Instead of using timers, try storing the start time for each item:

 DateTime startTime = DateTime.Now;
 item.startTime = startTime;
 
 ...
 
 float elapsedSeconds = (float)((DateTime.Now - item.startTime).TotalSeconds);
 
 if(elapsedSeconds >= item.timeToCompletion)
   Kill(item);
 
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 Serellyn · Jun 04, 2018 at 02:54 PM 1
Share

Ah, yes, this could work also. Great tip!

avatar image
0

Answer by ShadyProductions · Jun 04, 2018 at 02:10 PM

Very easy actually, just cache all timers by item in a dictionary and in update count them up all up every second.

Here is an example:

 using System.Collections.Generic;
 using UnityEngine;
 
 namespace Assets.Scripts
 {
     public struct Item
     {
         // Unique item id
         public int Id;
     }
 
     public class TimeClass : MonoBehaviour
     {
         private float _gameTime;
         private Dictionary<int, int> _timerCache;
 
         private void Start()
         {
             _timerCache = new Dictionary<int, int>();
         }
 
         public int? GetTimerValue(Item item)
         {
             int time;
             if (_timerCache.TryGetValue(item.Id, out time))
                 return time;
             return null;
         }
 
         public void AddTimer(Item item)
         {
             int time;
             if (!_timerCache.TryGetValue(item.Id, out time))
             {
                 _timerCache.Add(item.Id, 0);
             }
         }
 
         public void RemoveTimer(Item item)
         {
             if (_timerCache.ContainsKey(item.Id))
             {
                 _timerCache.Remove(item.Id);
             }
         }
 
         private void Update()
         {
             _gameTime += Time.deltaTime;
 
             // Calculate every second
             if (_gameTime >= 1f)
             {
                 _gameTime = 0f;
 
                 // Add to all timers
                 foreach (var item in _timerCache.Keys)
                 {
                     _timerCache[item] = _timerCache[item]++;
                 }
             }
         }
     }
 }
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 Harinezumi · Jun 04, 2018 at 02:17 PM 0
Share

Why a Dictionary? Quick lookup is not essential, and using Item as key does not provide any advantage, not to mention that using foreach in Unity is not recommended (due to the GC). A List would make a lot more sense in this case.

avatar image ShadyProductions Harinezumi · Jun 04, 2018 at 08:11 PM 0
Share

Where did you get the information that 'foreach' in unity is not recommended, yes foreach over a dictionary makes garbage but only the first loop, the once thereafter create 0 garbage. A list creates no garbage in unity anymore and would techniqually be a better choice if the Item pool would be small. A dictionary can be useful in cases if there are a lot of items in game, and yes the key could have rather been an int, possibly the Item Id.


I chose the dictionary because it was the easier way of storing the timer, without having to directly add the timer to the item class/struct.

avatar image Harinezumi ShadyProductions · Jun 04, 2018 at 08:50 PM 0
Share

I read it in this Unity blog post: https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games
Apparently it has been fixed in Unity 5.5.

I find this solution very unusual... but I guess it must be a program$$anonymous$$g style thing.

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

95 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Animator equivalent of animation.isPlaying? 1 Answer

Rotate sun during set of time 2 Answers

I'm already an experienced programmer, is there anywhere great to go to get familiar with the libraries available in unity? 2 Answers

programming help/teacher? c# 0 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