[Open Source] Lurking Ninja's developer's tools series: Dependency

Dependency Package
On GitHUb

If you’re tired to drag and drop every bit of component references in the editor, this package is for you.
Simplified dependency injection for Unity utilizing source generation making it as performant as possible.
You can find game objects through names or tags, components, list or array of components, and have many other decorators to find the targeted component you need.

I’m looking forward any and all feedback or if you have any idea for useful additions to this package, feel free to discuss it below, I will consider every ideas.

You can install through GitHub here: Lurking Ninja’s Dependency

Basic usage is super simple, just decorate your fields and properties:

using LurkingNinja.Attributes;
using UnityEngine;

namespace DoTest
{
    [GenerateOnValidate]
    public partial class DiTest : MonoBehaviour
    {
        [Get][SerializeField]
        private BoxCollider[] get_BoxColliders;
      
        [Get][field: SerializeField]
        private SphereCollider GetOnProperty_SphereCollider { get; set; }
      
        [FindWithTag("MainCamera")][SerializeField]
        private Camera getByTag_MainCamera;
      
        [GetInChildren][IncludeInactive][SerializeField]
        private AudioSource getInChild_AudioSource;
      
        [GetInChildren][IncludeInactive][IgnoreSelf][SerializeField]
        private AudioSource[] getInChildren_AudioSource;
    }
}

And enjoy the inspector fill itself up with references:

The latest version on GitHub

Changelog
All notable changes to this project will be documented in this file.

[0.0.5] - 2024-01-10

  • [Add] attribute

TODO

  • Reintroduce asset referencing (GetInAssets)
  • Develop filtering
  • Add C# class ([Depends] - [Provides]) dependency injection
  • Check if we can have a [Require] attribute for entries (throwing warning or error in console).

[0.0.4] - 2024-01-09 - Release (Download)
Changed

  • Documentation updated

TODO

  • Reintroduce asset referencing (GetInAssets)
  • Develop filtering
  • Add C# class ([Depends] - [Provides]) dependency injection

[0.0.3] - 2024-01-09
Added

  • DiTestHelpers
  • StableSort
  • Tests added

TODO

  • Reintroduce asset referencing (GetInAssets)
  • Develop filtering
  • Update documentation

[0.0.2] - 2024-01-08
Added

  • Find
  • FindWithTag
  • GenerateAwake
  • GenerateInitializers
  • GenerateOnValidate
  • InjectInEditor
  • InjectInRuntime

Removed

  • GetByName - use the combination of Find and Get
  • GetByTag - use the combination of FindWithTag and Get
  • GetInAssets - will be introduced on another way in the future
  • InjectInPlay - replaced with InjectInRuntime
  • Placeholder files in package folder

TODO

  • Reintroduce asset referencing (GetInAssets)
  • Develop filtering
  • Add bool SortByInstanceId optional parameter to Find
  • Update documentation
  • Move test over to public

[0.0.1] - 2024-01-03 - Release (Download)
Added

  • Get attribute
  • GetByName attribute
  • GetByTag attribute
  • GetInAssets attribute
  • GetInChildren attribute
  • GetInParent attribute
  • IgnoreSelf attribute
  • IncludeInactive attribute
  • InjectInPlay attribute
  • SkipNullCheck attribute

TODO

  • FindWithTag attribute
6 Likes

As a person don’t use source generators before, I wonder how much these source generators effect on iteration time. Do generators work on only changed scripts or do they rebuild all marked scripts on compile?

It’s a bit more complex question what code the generators run on inside Unity.

I recommend that you read this thread , especially @HaraldNielsen 's posts. Those are partially on when generators are invoked. And also tells you that the results are cached.
In my experience so far, they aren’t heavy on the iteration time. But I’m always careful about my assembly-relations in my projects.

Also it is better to use rather the IIncrementalGenerator than the ISourceGenerator. And if you do any heavy computation, use the cancellation token, so you don’t run heavy calculations on code will be discarded anyway.

Also, in general the Roslyn docs page is great (Unity docs), except don’t believe in the version numbers.

This is absolutely not true, you can use 4.3.1 for sure in 2023.x versions and in 2022 LTS as well (basic tested).

Finally, please comment here as well. It’s a petition-thread to request separate SourceGenerator topic in these forums, because currently the info regarding source generation is all over the place, it would be great to have one topic where we can share info.

And ps: sorry that I answered this late, unfortunately the usual Unity drama consumed my patience again.

2 Likes

This plugin reminds me the Cecil attributes. Looks like it uses Mono Cecil instead of SG to achieve similar things.
This could little bit off-topic but I’m gonna ask anyway.
I wonder your thoughts about Cecil vs Source Generators. What their pros and cons in Unity context and why you choose SG for this project?

Well, I only thought about this very briefly, to be honest, I don’t particularly like Cecil, it’s too verbose. If you check the code of the Source Generator I wrote and this Cecil Attributes’ code base the gap is giant, it takes significantly more code to make changes. Especially when you’re weaving IL. It’s just not as comfortable as the Roslyn-solution is. I have no fundamental opposition to Cecil though.
Also I started to learn SG at some point and just fell into it. Meanwhile I never really started to use Cecil to change source output.

1 Like

I cant make it work on 22.3.9 but it there is no issue on 22.3.16 and 22.3.19. The partial class generated but not worked somehow.

Another issue is that I’m updated Roslyn of my editor and I’m using File-Scoped namespaces, but source generator couldn’t understand it, so gives compiler error. Do you think you can fix it?

Edit: I think there is the solution

I will look into it, but since you already found a solution, do not wait for me, feel free to fork it and apply the change. I’m not sure when I will have the time to dig into how to update Roslyn under Unity-thing myself.

Thanks to @icauroboros support for file-scoped namespaces added

1 Like