Day 9 – Unity Modular Code Structure
Hi, Kavabun here!
Today I spent time refactoring my code, and although I’m not 100% satisfied yet… I’m definitely getting there.
Modular Code in Unity
I came across a really helpful YouTube video that explained the idea of modular code architecture in Unity. The key takeaway?
Keep your code separated with controllers, and have components communicate via Unity Events.
In Dungeon Merchant, the core gameplay revolves around the item placement and customer interaction system, which is similar to well-knwon [Supermarket Simulator] (https://store.steampowered.com/app/2670630/Supermarket_Simulator/). I had a feeling I’d need to modularize things eventually, but I hadn’t planned to start now. Turns out, it was the right time.
Tooltip System Refactor
I began with refactoring the tooltip system, and it actually went quite smoothly (thanks, AI).
One thing I struggled with was using managers. While I generally like them, they don’t work well with UnityEvents, since you can’t easily drag and drop references in the editor. So, I ended up creating a GameEvent
class — a ScriptableObject that can be referenced statically. That solved the problem.
Here’s how I broke the system down:
TooltipUIController
(+ITooltipProvider
)TooltipTrigger
TooltipEvent
(+TooltipEventListener
)
The TooltipTrigger
simply raises an event when the player hovers over something. That event is caught by a listener in the editor, which then tells the TooltipUIController
to show or hide the tooltip.
No need for a traditional manager here!
Each component that needs a tooltip just implements ITooltipProvider
, which keeps things clean and decoupled.
PlayerInteraction: Still in Progress
I also started working on the infamous PlayerInteraction
script. It’s still huge. Still scary. But it’s on my radar now, and I’ll slowly chip away at it.
What’s Next?
I’m planning to share my version of the refactored system soon — maybe with a quick tutorial if I can. I think it could help others like me figuring this out along the way.
Thanks again for reading. See you in the next post!
If you’re enjoying these devlogs or just curious about my journey:
- Subscribe via email here: Email Subscription
- Check out the main blog: BHB Studios Blog
Stay tuned!