I’m curious, in general, how feasible it is to migrate a small part of my code over to ECS, while leaving most of my code using “legacy” MonoBehaviour.
Right now, my game is using the MonoBehaviour approach to everything. There is one aspect of my game that is a performance bottleneck: specifically, there are objects in my scene that exert force on all nearby objects, depending on line of sight. This happens every physics frame, and it’s expensive because I’m performing raycasts from the object to all other rigidbodies within a certain range.
I’ve optimized this in a couple of ways so far. I cache the Rigidbodies in a GameManager, so I don’t need to GetObjectsOfType every frame. And I’m performing the raycasts in a job, using RaycastCommand
to do the raycasting in the background. However, it’s still not great performance.
So I’m wondering if I can feasibly move this functionality into an ECS approach, while leaving the rest of my code alone. So, some (hopefully) simple questions:
- Can I access a rigidbody in an ECS OnUpdate? Can I call AddForce on it in the ECS update?
- Can my game objects contain mainly MonoBehaviour components, but also contain a some ECS Component objects?
- As I said, right now I’m caching Rigidbodies when their gameobject is instantiated, to speed up finding components every frame. It seems that using ECS this isn’t necessary, and the ECS filters efficiently find components in the scene without me having to creating my own caching system? Is that correct? (There’s Unity wisdom: Don’t call FindObjectsOfType in Update, and I just want to make sure the examples I’ve seen aren’t overly simplified.)
- What if I need to access some other objects in my Update? For example, when I call AddForce on the objects, currently I do one extra thing if the object I’m adding force to is the Player. That is, I call a method on my Player, which is a MonoBehaviour. Is there any way to handle this kind of thing in ECS, where everything is reduced to structs and primitives?
Sorry if these questions are a bit vague. ECS looks awesome from the demos I’ve seen. It’s just a bit more difficult to appreciate how feasible it is to use for arbitrary tasks.
Thanks.