Need a custom collider

I think I may have, for the first time, completely hit a wall on what is possible in the Unity engine. I need very large cube terrain, stored very compactly, and I need it to interact properly with the physics engine. This means I need a collider, or set of colliders, that represent the shape of the terrain, which basically requires me to duplicate my map data into a much more verbose format, either turning it into a big polygon mesh, or into a large number of BoxColliders. This would totally destroy any hope of cramming my game into a reasonable amount of memory. (I think I have ways of drawing the terrain efficiently, but that doesn’t help with the physics.)

If I could just subclass the base Collider class, and use that, it would be no problem. It’s trivial to detect collisions against cube terrain. Unfortunately, Unity doesn’t allow that. If you try to add a component to a GameObject, it seems it either has to be one of the set of Component subclasses that they specifically authorize, or a subclass of MonoBehavior.

Is there any other way of doing custom collision detection? Some kind of callback? A way to edit a collision, or decide whether it counts, in OnCollisionEnter or OnCollisionStay?

The best I can think of is to do the check myself every frame on every rigid body, and when they’re colliding with something, create a BoxCollider for the block they’re colliding with, which I keep around only as long as they’re actively colliding with it. This seems way over-complicated, and thus really not an ideal solution, but it may the only thing that will work.

As I’ve put more thought into it, I realized the solution I proposed in the last paragraph actually isn’t so bad. I can get the axis-aligned bounding box of every collider, and checking for a collision between an AABB and cube terrain is extremely trivial. I’ll get some false positives, but that’s okay.

I’m a little worried that things will penetrate the terrain a bit too much. It’ll update the physics, and some object with move to a position where it’s in collision with the terrain. Then, my algorithm runs in the next FixedUpdate, detects this collision, and places a BoxCollider. Then the next time the physics updates, it finds the object colliding with the BoxCollider and pushes it out. The fact that it has one update where it’s penetrating the terrain without an actual collider there may make things seem a bit squishy.

Only way to find out is to try, though.