npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

com.knot.bindings

v0.4.1

Published

Smart property bindings solution for Unity

Downloads

68

Readme

Unity version Dependencies Platforms

ezgif-4-cafed11d6c

Installation

Install via Package Manager

Screenshot_3

https://github.com/V0odo0/KNOT-Bindings.git

or

Add dependency to your /YourProjectName/Packages/manifest.json

"com.knot.bindings": "https://github.com/V0odo0/KNOT-Bindings.git",

Usage example #1: Property override

//Create or get bindings container
var bindingsContainer = new KnotBindingsContainer();

//Add property with default value
bindingsContainer.Set("EnablePlayerControls", true);

//Read property value
Debug.Log(bindingsContainer.Get("EnablePlayerControls", false)); //true

//Override property value by setting higher priority 
bindingsContainer.Set("EnablePlayerControls", false, 100);

//Read property value after override
Debug.Log(bindingsContainer.Get("EnablePlayerControls", false)); //false

//Delete previously set override value
bindingsContainer.Delete<bool>("EnablePlayerControls", 100);

//Read property value after deleting higher priority override
Debug.Log(bindingsContainer.Get("EnablePlayerControls", true)); //true

Usage example #2: Two-Way Databinding

//Class that changes CameraZoom property by player input and applies it to Camera
public class CameraController : MonoBehaviour
{
    public float CameraZoom;

    void OnEnable()
    {
        //Register property changed callback
        KnotBindings.Global.RegisterPropertyChanged<float>("CameraZoom", OnCameraZoomPropertyChanged);
    }

    void OnDisable()
    {
        //UnRegister callbacks
        KnotBindings.Global.UnRegisterPropertyChanged<float>("CameraZoom", OnCameraZoomPropertyChanged);
    }

    void OnCameraZoomPropertyChanged(float oldvalue, float newvalue, object setter)
    {
        //Return if the property has been changed by this CameraController
        if (setter as Object == this)
            return;

        //Apply zoom from binding property value
        CameraZoom = newvalue;
        ApplyCameraZoom();
    }

    void Update()
    {
        //Read mouse scroll input
        if (Input.GetMouseButtonDown(2))
        {
            //Modify CameraZoom
            CameraZoom += Input.mouseScrollDelta.y;
            CameraZoom = Mathf.Clamp(CameraZoom, 1, 10);

            //Set property value and provide the setter (this CameraController)
            KnotBindings.Global.Set("CameraZoom", CameraZoom, setter: this);

            ApplyCameraZoom();
        }
    }

    void ApplyCameraZoom()
    {
        var cameraLocalPos = Camera.main.transform.localPosition;
        cameraLocalPos.z = CameraZoom;
        Camera.main.transform.localPosition = cameraLocalPos;
    }
}
//Class that changes CameraZoom property by Slider
public class UICameraSettingsPanel : MonoBehaviour
{
    public Slider CameraZoomSlider;

    void Awake()
    {
        CameraZoomSlider.minValue = 1;
        CameraZoomSlider.maxValue = 10;
    }

    void OnEnable()
    {
        //Register slider value changed callback
        CameraZoomSlider.onValueChanged.AddListener(OnCameraZoomChanged);

        //Register property changed callback
        KnotBindings.Global.RegisterPropertyChanged<float>("CameraZoom", OnCameraZoomPropertyChanged);
    }

    void OnDisable()
    {
        //UnRegister callbacks
        CameraZoomSlider.onValueChanged.RemoveListener(OnCameraZoomChanged);
        KnotBindings.Global.UnRegisterPropertyChanged<float>("CameraZoom", OnCameraZoomPropertyChanged);
    }

    void OnCameraZoomChanged(float cameraZoom)
    {
        //Set property value and provide the setter (this UICameraController)
        KnotBindings.Global.Set("CameraZoom", cameraZoom, setter: this);
    }

    void OnCameraZoomPropertyChanged(float oldvalue, float newvalue, object setter)
    {
        //Return if the property has been changed by this UICameraController
        if (setter as Object == this)
            return;

        //Apply zoom from binding property value
        CameraZoomSlider.SetValueWithoutNotify(newvalue);
    }
}

Usage example #3: Global Bindings

public class MyGlobalBindings
{
    public static MyGlobalBindings Instance = new();

    public UIBindings UI { get; } = new();


    public class UIBindings : KnotBindingsContainer
    {
        public KnotBindingsProperty<bool> Interactable { get; } = new(true);
    }
}
public class MyUIManager : MonoBehaviour
{
    [SerializeField] private CanvasGroup _canvasGroup;

    void OnEnable()
    {
        MyGlobalBindings.Instance.UI.AnyPropertyChanged += OnAnyPropertyChanged;
        //or
        MyGlobalBindings.Instance.UI.Interactable.Changed += OnInteractableChanged;
    }

    void OnDisable()
    {
        MyGlobalBindings.Instance.UI.AnyPropertyChanged -= OnAnyPropertyChanged;
        //or
        MyGlobalBindings.Instance.UI.Interactable.Changed -= OnInteractableChanged;
    }

    void OnAnyPropertyChanged(string propertyName)
    {
        switch (propertyName)
        {
            case nameof(MyGlobalBindings.UI.Interactable):
                _canvasGroup.interactable = MyGlobalBindings.Instance.UI.Interactable;
                break;
        }
    }

    void OnInteractableChanged(bool oldvalue, bool newvalue, object setter)
    {
        _canvasGroup.interactable = newvalue;
    }
}