Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Assets and Asset Store' started by Lurking-Ninja, Jan 11, 2024.

  1. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    556
    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:
    Code (CSharp):
    1. using LurkingNinja.Attributes;
    2. using UnityEngine;
    3.  
    4. namespace DoTest
    5. {
    6.     [GenerateOnValidate]
    7.     public partial class DiTest : MonoBehaviour
    8.     {
    9.         [Get][SerializeField]
    10.         private BoxCollider[] get_BoxColliders;
    11.      
    12.         [Get][field: SerializeField]
    13.         private SphereCollider GetOnProperty_SphereCollider { get; set; }
    14.      
    15.         [FindWithTag("MainCamera")][SerializeField]
    16.         private Camera getByTag_MainCamera;
    17.      
    18.         [GetInChildren][IncludeInactive][SerializeField]
    19.         private AudioSource getInChild_AudioSource;
    20.      
    21.         [GetInChildren][IncludeInactive][IgnoreSelf][SerializeField]
    22.         private AudioSource[] getInChildren_AudioSource;
    23.     }
    24. }
    And enjoy the inspector fill itself up with references:
    inspector.png


    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
     
    Last edited: Jan 13, 2024
  2. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    177
    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?
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    556
    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.
     
    HaraldNielsen and icauroboros like this.
  4. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    177
    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?
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    556
    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.
     
    icauroboros likes this.
  6. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    177
    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
     
    Last edited: Feb 9, 2024
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    556
    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.
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    556
    Thanks to @icauroboros support for file-scoped namespaces added
     
    icauroboros likes this.