Auto Hook is a property drawer that automagically assigns a reference to your field.
- The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.autohook
- Installing through a Unity Package created by the Package Installer Creator from Needle
Using Auto Hook is easy. You just have to add the [AutoHook]
attribute to any field that wants a component and it'll try to assign it.
public class Foo : MonoBehaviour
{
[SerializeField, AutoHook]
private Rigidbody rigidbody;
[...]
}
There's some extra options that you can configure
Option | Description |
---|---|
SearchArea | Default Matches GetComponent Parent Matches GetComponentInParent Children Matches GetComponentInChildren DirectChildrenOnly Iterates over the direct children of the current object only, ignoring itself and any children below that AllChildrenOnly Iterates over all children of the current object recursively, ignoring only itself |
HideWhenFound | Reduces the size of the property to 0 when a matching component has been found |
ReadOnlyWhenFound | Marks the property as read-only when a matching component has been found |
StopSearchWhenFound | Stops calling GetComponent (or other variants) when a matching component has been found. This is useful if you have many [AutoHook] usages in your file |
Auto Hook is originally made by LotteMakesStuff and can be found here.
You can support LotteMakesStuff through her ko-fi or by becoming her Patreon
If you're feeling generous and you like my version then you can support me here
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.