jeudi 30 mars 2023

Best way to extend Unity3d Rigidbody?

I am designing a physics class library for Unity3d and would love to be able to hook and inject functionality into Unity3d's Rigidbody class, but it's my understanding that there is no way to do this directly while still being able to target all platforms.

I thought the next best thing might be to create a new class that inherits directly from Rigidbody. However, it looks like unity has prevented this from occurring in anything but the UnityEngine namespace by requiring the internal sealed ExtensionOfNativeClassAttribute on anything that inherits native classes. Furthermore, Unity does not searialize inherited classes well.

So, in review, I'm not able to hook into Rigidbody directly (Please correct me if this is wrong!), and I'm not able to inhert from Rigidbody directly (Please correct me if this is wrong!). I think the next best option is to:

  1. Create a new Monobehavior, let's say it's called ModRigidbody, which makes a parellel implementation of all of Rigidbody's members. This class does not actually inherit from Rigidbody, but merely has members with all the same names / signatures as Rigidbody.
  2. On ModRigidbody add the attribute [RequireComponent(typeof(Rigidbody))]. Now I can add functionality to some ModRigidbody's members as I see fit, and have other members simply 'pass through' to transform.GetComponent<Rigidbody>().
  3. Have any end users (programmers) change all of their references to Rigidbody over to ModRigidbody.

To me this feels quite sloppy. Is there a way to mod the functionality of Rigidbody so that future users of this library will not have to change all of their Rigidbody over to ModRigidbody?

Aucun commentaire:

Enregistrer un commentaire