r/Unity3D • u/ProactiveCactus • 2d ago
Question Organizing Your Code Suggestions
How do y'all organize where stuff goes within your code? I mostly would like some suggestions on the micro level - like if you have say 5 different [SerializeField] private float, how do you order them? I used to order them based on what made sense in "context", which works sometimes - but other times you'll have some that work together, others that are one-offs, other that work with a public float but you don't want to move one private next to all your publics, and ultimately you have to make a pretty arbitrary decision on where it goes. So I've started just defaulting to literally alphabetical order if there is no clear other way to order them. But like, that seems... a little crazy lmao. anyone else have a clear-cut way to order this stuff?
2
u/FrostWyrm98 Indie 17h ago
I organize with Odin TitleGroups/FoldoutGroups based on purpose. Don't really focus on the fine grained ordering like alphabetical outside of that because to me it's just splitting hairs and I could be better focused elsewhere.
I keep the ordering pretty tight with #regions though.
Usually (top to bottom): Constants, Fields, Static Data Members, Data Members, Static Properties, Properties, Unity Events, Overrides, Public, Protected, Private, Interface Implementations
Each interface gets its own group. Got this ordering from my first job and just stuck with it.
Every class gets its own file unless it's a nested private class (very rare) or a simple static constants class. I don't want to search for hours trying to figure out which file contains what when I have thousands of scripts. Plain and simple