Binder

A binding library for Unity 3D. You can easily binding Unity 3D component or resource to field(method) by using this library, make programming more efficient.

Flow

Usage

Import package

Import Binder package, purchase from Asset Store.

ViewBinder

Add [BindView] attribute with view name to fields. Binder will find and automatically cast the corresponding component into the field.

public class BindViewSample : MonoBehaviour
{
    [BindView("HelloWorldText")]
    private Text _text;

    [BindView("ButtonText")]
    private Text _buttonText;
}

Add [BindViews] attribute with multiple views name to a List<T> field(T is the specified component type).

public class BindViewSample : MonoBehaviour
{
    [BindViews("HelloWorldText", "ButtonText")]
    private List<Text> _texts;

    [BindViews("Button1", "Button2", "Button3")]
    private List<Button> _buttons;
}

Add [OnClick] or [OnClickAttribute] attribute with single(multiple) view(s) name to a method(The view referred to by the name(s) must be a Button component, and the method should have empty arguments).

public class BindViewSample : MonoBehaviour
{
    [OnClick("Button1", "Button2")]
    void OnClick()
    {
        // Multiple buttons click listener...
    }

    [OnClick("Button3")]
    void OnClick()
    {
        // One button click listener...
    }
}

Add [OnToggle] or [OnToggleAttribute] attribute with single(multiple) view(s) name to a method(The view referred to by the name(s) must be a Toggle component, and the method should have only one bool arguments).

public class BindViewSample : MonoBehaviour
{
    [OnToggle("Toggle1", "Toggle2")]
    void OnToggle1(bool on)
    {
        // Multiple toggles value changed listener...
    }

    [OnToggle("Toggle1")]
    void OnToggle1(bool on)
    {
        // One toggle value changed listener...
    }
}

Add [OnSlide] or [OnSlideAttribute] attribute with single(multiple) view(s) name to a method(The view referred to by the name(s) must be a Slider component, and the method should have only one float arguments).

public class BindViewSample : MonoBehaviour
{
    [OnSlide("Slider1", "Slider2")]
    void OnSlider1(float value)
    {
        // Multiple sliders value changed listener...
    }

    [OnSlide("Slider1")]
    void OnSlider2(float value)
    {
        // One slider value changed listener...
    }
}

Add [OnDropdown] or [OnDropdownAttribute] attribute with single(multiple) view(s) name to a method(The view referred to by the name(s) must be a Dropdown component, and the method should have only one int arguments).

public class BindViewSample : MonoBehaviour
{
    [OnDropdown("Dropdown1", "Dropdown2")]
    void OnDropDown1(int index)
    {
        // Multiple dropdown value changed listener...
    }

    [OnToggle("Dropdown1")]
    void OnDropDown2(int index)
    {
        // One dropdown value changed listener...
    }
}

Note: Make sure each view has a unique name in the same script(If there are multiple views with the same name, Binder will bind the first view found).

Inject

Add [Inject] attribute to the method where the view fields or listener will be initialized.

[Inject]
void Start()
{
    // After inject, you can access the bound views or receive callback from bound views ...
    string textStr = _text.text;
    int textsListCount = _texts.Count;
}

Note: You can't access the fields until you specify a method to initialize the fields with Inject attribute(In Unity 3D, it's recommended to add this attribute on Awake or Start method).

Now, you have successfully bound the view or listener. The next, we will introduce the resource binding.

ResourceBinder

Add [BindResource] attribute with resource path to fields. Binder will load the resource with specify path and automatically cast the corresponding resource into the field.

public class BindResourceSample : MonoBehaviour
{
    [BindResource("test/go0")]
    private GameObject _gameObject0;
    
    [BindResource("test/textAsset")]
    private TextAsset _textAsset;
}

Note: This binding method will block the thread, just like you execute Resources.Load<T>(string path).

Add [BindResourceAsync] attribute with resource path and type to fields. Binder will load the resource asynchronously with specify path and automatically cast the corresponding ResourceRequest into the field.

public class BindResourceSample : MonoBehaviour
{
    [BindResourceAsync("test/goAsync", typeof(GameObject))]
    private ResourceRequest _resourceRequest;
    
    [Load]
    void Start()
    {
        StartCoroutine(BindResourceAsync());
    }

    IEnumerator BindResourceAsync()
    {
        if (!_resourceRequest.isDone)
        {
            yield return null;
        }
        Debug.Log(((GameObject)_resourceRequest.asset).name);
    }
}

Note: In this binding method, binding is an asynchronous process, just like execute Resources.LoadAsync<T>(string path). Only field with type ResourceRequest are supported by [BindResourceAsync] attribute, in addition to specifying the path of the resource, you also need to specify the type of resource.

Add [BindResources] attribute with multiple resources path to a List<T> field(T is the specified resource type). Binder will load this resources with specify path and automatically add the corresponding resource to the List<T> field in order.

public class BindResourceSample : MonoBehaviour
{
    [BindResources("test/go0", "test/go1")]
    private List<GameObject> _goList;
    
    [BindResources("test/textAsset0", "test/textAsset1")]
    private List<TextAsset> _textAssetList;
}

Note: This binding is also synchronous.

Add [BindAllResource] attribute with a folder path to fields. Binder will load all the resources in a folder or file at path and automatically add the corresponding resource(the resource has been filter by array field element type) to the Array field.

public class BindResourceSample : MonoBehaviour
{
    [BindAllResource("test/")]
    private GameObject[] _gameObjectArray;
    
    [BindAllResource("test/")]
    private TextAsset[] _textAssetArray;
}

If path refers to a folder, all resources in the folder will be returned. If path refers to a file, only that resource will be returned.

Note: This binding is also synchronous. When you defined an array element type, only the specify type resources will be bound.

Load

Add [Load] attribute to the method where the resources will be load and the resource fields will be bound.

[Load]
void Start()
{
    // After load, you can access the bound resource fields ...
    // ...
    Debug.Log(_textAsset.text);
    StartCoroutine(BindResourceAsync());
    // ...
    // ...
}

IEnumerator BindResourceAsync()
{
    if (!_resourceRequest.isDone)
    {
        yield return null;
    }
    
    Debug.Log(((GameObject)_resourceRequest.asset).name);
}

Note: You can't access the resource fields until you specify a method with Load attribute.

Now, you have successfully bound the resource. Enjoy it!

Custom assembly definition binding

The assembly define with name 'Binder.dll' is the Binder assembly definition. To use Binder in your custom assembly definition class, add the Binder assembly definition reference for custom assembly definition.

Menu Tool

After successfully importing this package, the Unity menu bar will have a 'Binder' menu:

Use the menu tool, you can do something like below:

Menu item Description
Bind All Bind view manually.
Auto Bind Enable or disable automatic binding. Once enabled, Binder will automatically bind after reload scripts(Default enable).
Settings Open Binder setting window.

Binder setting window

In setting window, you can choose default assembly or add custom assembly for binding, only specified assemblies will participate in binding. If there are multiple custom assembly definitions, separate the names with ';'.

Note: If you want to run the sample scene of the Binder, make sure the 'Binder.dll' option is selected.

Change logs

1.1.1(2020-04-11)

1.1.0(2020-04-06)

1.0.3(2020-04-02)

1.0.2(2020-03-30)

1.0.1(2020-03-28)

1.0.0(2020-03-11)

0.9.1(2020-03-04)

0.9(2020-03-03)

About

If you have any questions, contact me: lujun.byte#gmail.com.

Copyright

© 2020 lujun