Allows for automatic component assignments in Unity projects using C# and IL weaving.
In unity you will often need references to other components. A well known beginner approach is to do the following:
[SerializeField] private Rigidbody rb;This way, you can drag and drop the references in the inspector. However, more seasoned unity developers know that this way of working is very error prone.
A team member can for example forget to drag a field in, or a field might be impossible to drag because it needs a reference after a runtime instantiation.
Therefore an alternative is taught to intermediate unity developers.
private Rigidbody rb;
void Awake()
{
if(!rb) rb = GetComponent<Rigidbody>();
}Instead of letting unity serialize the reference, we let it find the component at runtime. For years this has been the default. However it causes a new problem. A big stutter happens when loading/instantiating new scenes/gameobjects. This is because all objects will search for a reference at the same time. Apart from that it also requires developers to write the little piece of boilerplate code every time.
This is where Auto Assigns come in. It extends on the begginer approach with the flexibilty of the intermediate approach.
[SerializeField, AutoAssign] private Rigidbody rb;Auto assigns uses an optimized non reflection based approach to assign serializeable fields. For performance it does this at compile time whereever possible, and at runtime whenever needed.
Thanks to a bit of black magic (IL Weaving) auto assigns have almost the same speed as the manual approach (when compared to a reflection based implementation).
Auto assigns are also flexible via different search areas and with support for different field types:
//Different kinds of fields
//Public fields
[AutoAssign] public Rigidbody rb;
//Compiler Generated Backing Fields
[field: SerializeField, AutoAssign] public Rigidbody Rb { get; private set; }
//Different search areas
[SerializeField, AutoAssign(AutoAssignSearchArea.Scene)] public Camera camera;
//The snippet above will look for any camera in the scene (FindAnyObjectByType<T>())
//Or mix multiple search areas
//For example, first search on this object, if not found, search in all children
[AutoAssign(AutoAssignSearchArea.CurrentObject | AutoAssignSearchArea.AllChildren)] public Collider collider; - 🔍 Multiple Search Areas (See table below)
- 🔗 IL Weaving based implementation (Faster, Default)
- 🔭️ Reflection based implementation (Slower, Backup)
- ⚙ Extra Compiler Checks (Via roslyn analyzers)
| Search Area | Unity Equivalent |
|---|---|
| CurrentObject | GetComponent() |
| Parent | GetComponentInParent() |
| DirectChildren | for child in direct-children -> GetComponent() |
| AllChildren | GetComponentInChildren() |
| Scene | FindAnyObjectByType() |
-
Add the Package to Unity
-
Open your Unity project.
-
Go to Window → Package Manager.
-
Click the ‘+’ button in the top-left corner and select “Add package from Git URL…”
-
Enter the following URL:
https://github.com/Wezzel-NL/AutoAssignsForUnity.git -
Click Add and wait for Unity to import and compile the package.
-
-
Try the Sample Scene
- Open the Sample Scene provided with the package.
- Select the ExampleObject game object
- All fields should automatically be assigned
- Reset the component to clear the fields, then revalidate it to see the fields get populated (for example by disable->enable toggling it or entering playmode)
-
Apply to your own scripts
The package includes a Script Usage Sample demonstrating how to use Auto Assigns in your code.
Planned and potential future enhancements include:
-
🔍 More search areas If you’d like to contribute or take on one of these features:
-
💡 Open a pull request for feature contributions.
-
🧾 Submit an issue for suggestions or bug reports.
Contributions are very welcome! Please fork the repository, create a feature branch, and submit a pull request with a clear description of your changes.
This project is licensed under the BSL license — see the LICENSE.md file for more details.
In short: For personal use you can use it for free, for commercial use ask me first. When in doubt, ask.
After 01/01/2029 the license changes to MIT.