Skip to content

Repository files navigation

What is it?

The Trackable is a generic property wrapper for Unity projects that tracks changes of nested property and invokes custom UnityEventwhen changes occurred.

Getting started

For example, you have some code like this:

publicboolisGrounded;privatebool_previousIsGrounded;privatevoidUpdate(){isGrounded=CheckIsGrounded();if(isGrounded!=_previousIsGrounded){isGrounded=_previousIsGrounded;OnIsGroundedChanged();// OnIsGroundedChanged - is your's handler of isGrounded change event}}

You can simplify it to this:

usingMadeYellow.Trackables;
...
public Trackable<bool>isGrounded=newTrackable<bool>();privatevoidStart(){isGrounded.onChanged.AddListener(OnIsGroundedChanged);// OnIsGroundedChanged - is your's handler of isGrounded change event}privatevoidUpdate(){isGrounded.value=CheckIsGrounded();}

Both will work the same, but with Trackable you will have the power of UnityEvent and code readability. Whenever you set new value to .value - change tracking will happen automatically.

How to install Trackable to my Unity project?

Use the Unity Package Manager (in Unity’s top menu: Window > Package Manager), click "+" icon, select "Add package from git URL" and type URL of this repository.

Can i check changes not right after value is changed?

Yep! There is built-it TrackingStrategy.Manual that can be provided to Trackable constructor OR can be assigned right from Unity editor. You'll have to call .Commit(); at the desired moment in order to change tracking happen. It will be a proper strategy if your script changes variable several times during complex method execution and you want to perform single change tracking at the very end of the method (when you have a final value calculated) instead of multiple checks.

Here's how you may use it:

usingMadeYellow.Trackables;
...
public Trackable<bool>isGrounded=newTrackable<bool>(TrackingStrategy.Manual);// Here default auto detection strategy changed to manualprivatevoidUpdate(){isGrounded.value=false;isGrounded.value=CheckIsGroundedOne();isGrounded.value=CheckIsGroundedTwo();isGrounded.Commit();// Only last assigned value will be compared to previously commited value}

About

Property change tracking for Unity

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages