🌐 AI搜索 & 代理 主页
Skip to content

Init(args) Lite extends the MonoBehaviour class with the ability to receive arguments from the outside during their initialization, before the code in their Awake and OnEnable methods execute.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

timo-sisus/init-args-lite

Repository files navigation

init-args-lite

What is Init(args) Lite?

Init(args) Lite extends the MonoBehaviour class with the ability to receive arguments during initialization.

You'll be able to continue using all the same commands you're used to - AddComponent and Instantiate - just with the added ability to pass arguments as well.

The arguments will be received by components before the code in their Awake and OnEnable methods are executed, making sure that NullReferenceExceptions or bugs won't occur.

This makes it trivial to write unit tests for the components.

Reflection-Free

Init(args) Lite doesn't use any reflection; generic methods and interfaces are used to provide strongly typed pipelines through which the dependencies can be delivered with optimal efficiency and safety.

Features

  • Instantiate with upto twelve arguments.
  • AddComponent with upto twelve arguments.
  • x != Null - easily test if an interface type variable contains a null value, or a reference to a destroyed or unassigned Object.

Example Component

public class TextDisplayer : MonoBehaviour<StringEvent, Text> // <- list dependencies as generic type arguments
{
    [SerializeField] StringEvent stringEvent;
    [SerializeField] Text text;
 
    protected override void Init(StringEvent stringEvent, Text text) // <- dependencies received here
    {
        this.stringEvent = stringEvent;
        this.text = text;
    }
 
    void OnEnable() => stringEvent.AddListener(OnEventRaised); // <- it's safe to use dependencies during OnEnable!
    void OnDisable() => stringEvent.RemoveListener(OnEventRaised);
    void OnEventRaised(string value) => text.text = value;
}

Example Test

[Test]
public void Raising_StringEvent_Updates_Text_To_Match_Event_Argument()
{
    // Arrange
    var gameObject = new GameObject();
    var stringEvent = new StringEvent();
    var text = gameObject.AddComponent<Text>();
    gameObject.AddComponent(out textDisplayer, stringEvent, text);  // <- inject dependencies

    // Act
    stringEvent.Raise("Test");

    // Assert
    Assert.AreEqual("Test", text.text);
}

Links

About

Init(args) Lite extends the MonoBehaviour class with the ability to receive arguments from the outside during their initialization, before the code in their Awake and OnEnable methods execute.

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages