- Gain prototyping superpowers. Easily find, cache and validate components your script depends on.
- Skip the boilerplate. Implement singletons and track active components with one line of code.
- Have your cake, and eat it too. Emits highly optimized code, with no performance penalty for features you aren't using.
- Write a simple assignment, and Medicine will automatically generate backing fields and null checks for your components.
- Simply tag a method with
[Inject]
and make the classpartial
:
partial class MyComponent : MonoBehaviour
{
// nothing here!
[Inject]
void Awake()
{
// simply write some assignments - backing fields and
// missing component checks will be generated for you
Colliders = GetComponentsInChildren<Collider>();
Rigidbody = GetComponent<Rigidbody>().Optional();
MainCamera = Camera.main;
}
}
- Simply tag a component with the
[Singleton]
attribute - Access your singleton from anywhere with
T.Instance
orFind.Singleton<T>()
- Also works for
ScriptableObject
[Singleton]
class GameController : MonoBehaviour { }
var mySingleton = GameController.Instance;
- Put
[Track]
on your component and you're done! - Access the active instances using
T.Instances
orFind.Instances<T>()
- Fast and efficient under the hood
- Also works for
ScriptableObject
[Track]
class Enemy : MonoBehaviour { }
foreach (var instance in Enemy.Instances)
{
// iterate over enabled instances
}
- Non-alloc equivalents of the
GetComponents...<T>
family of functions. - Non-alloc enumerator for finding all components of type in a scene.
- Slightly faster variants of common functions, such as
FindObjectsByType<T>
.
foreach (var collider in this.EnumerateComponentsInChildren<Collider>())
{
// (do something with the child colliders)
}
foreach (var rigidbody in gameObject.scene.EnumerateComponentsInScene<Rigidbody>())
{
// (do something with each rigidbody in the scene)
}
// this version of FindObjectsByType omits an array allocation/copy,
// but it still allocates an array and is pretty slow.
// (you should use [Track] instead whenever possible)
GameObject[] allObjects = Find.ObjectsByType<GameObject>();
Compatibility: Unity 2022.3 or newer
Open "Add package from git URL" in the Unity Package Manager and paste the repository URL:
Optional dependencies:
- ZLinq
- Optimized, GC-free LINQ-like queries - highly recommended!
- Medicine data structures implement
AsValueEnumerable()
, allowing you to easily query components with ZLinq.
- PolySharp
- Enables the use of various modern C# features in Unity projects.
- Unlocks C# features such as:
init
setters![CallerArgumentExpression]
![InterpolatedStringHandler]
! - Medicine relies on some of these, but will generate its own fallback replacements when PolySharp is not detected.
- NuGetForUnity
- Easiest way to install the above two packages.