Hello,
What are some best practices for Unity C# ?
Post those which are memory efficient & faster in execution, no matter how complex it is ? You can post even single line also.
Please keep posting on this thread, since this can be useful for beginners to learn & for experts to improve their programming.
Thanks.
so we can have yet another thread on this?
google: c# best practices site:forum.unity3d.com
2 Likes
Best practices in games are hard to nail down. They are very project and team specific.
There are a few obvious ones that you can find with a search as mentioned. Things like using SerialiseField and keeping the load in Update low.
1 Like
The best practices really depend on the game, but really just try to maintain good encapsulation with your classes using things like [SerializeField] instead of public, and try to make each class or component only do one thing.
As far as performance and optimization you should not waste time on that till you have enough of a game to profile, and learn where time put into optimization will actually matter.
3 Likes
Can you please explain this ?
As for the optimization note, I tend to agree with the “don’t prematurely optimize” suggestion. Mostly because you’re likely wasting your time - much of the code you write in the early stages will be thrown out or changed to the point where having done previous optimizations on it wasn’t for good reasons.
With that said, I also don’t necessarily like the idea that you should code in a horribly sloppy way and then profile at the end and see where the highest cost is, and then optimize that. Maybe you’ll reach a point where you’ve optimized the major bottlenecks, but further optimizing the highest cost item proves to be too complex or difficult. But you wouldn’t even be in that situation if you had written proper code to begin with - all the “little things” taking up time throughout your code end up adding up.
What I would agree with is that you shouldn’t optimize the prototype code that will likely be changed or removed. Experiment, prototype, find what you like. When you’re happy with it and ready to commit to it, optimize it. Do this on a per-system basis, not on a per-entire_game basis.
2 Likes
Thanks.
I never knew about anything like this.
In that case you might want to start with just googling coding best practices. Almost all of general coding good practices are good practices in Unity.
Some acronyms to start you off
2 Likes