In our current project, a large part of the functionality relies on a timer. When it boils down to it, the accuracy of the timer isn’t hugely important so long as it’s consistent, but I figured the most accurate method is likely to also be the most consistent.
There are three basic ways to implement a timer in Unity:
- +=deltaTime in Update()
I’ve seen quite a bit of discussion about these different methods, but there appears to be some disagreement about what’s best and why. None of the discussions I’ve found have included the results of any tests to compare them or demonstrate accuracy or efficiency.
I’ve been avoiding
Update() where practical – using Coroutines or
Invoke instead if something doesn’t need to be checked or done for every frame of an object’s life. But with two other methods available, I didn’t know whether a Coroutine with
WaitForSeconds or an
Invoke is a better way to create a simple delay. It doesn’t really matter too much for one off events, but for repeating events, or if you have lots of them, delays can add up fairly quickly.
Continue reading “Most Accurate Timer in Unity”
I’ve been using Unity for a while, but I’ve only been coding (for realz) for about a year. I’m currently refactoring some of the code I wrote a year ago, and seeing lots of things I can do better.
One thing I’m seeing way too much of is this:
public GameObject myButton;
myButtonImage = myButton.GetComponent<Image>();
I’m assigning a
GameObject in the inspector, and then getting the
Image component in
What’s wrong with this?
In many cases I don’t need the
GameObject beyond the
Start() method, so having it hanging around is pretty much pointless, and getting all those components at the start (or any other time) causes an unnecessary delay.
Continue reading “Get the Right Component!”
I’ve been spending a fair bit of time with Unity’s UGUI for my latest project. Having wrestled with it for a while, in some ways I wish I’d stuck with NGUI, but I’ve learned a lot along the way so it’s not all bad.
In my UI I have some buttons which open sub menus, but I wanted to be able to rename the menu item as well. Since the target is mobile, right click is out. I thought a long press would be an intuitive way for users to change the name, so I had a quick look to see if anyone had already done this.
I came across this Unity forum post which has a script/component that works quite nicely.
The thing is, I’ve been avoiding including any
Update() methods in my UI to keep it as speedy as possible, so I rewrote that long press script to avoid
Update() and it’s turned out to be a fair bit quicker as well:
Continue reading “Unity uGui Button Long Press”