Derived
Use Derived.From to transform one or more Bindables into another. All of the following are equivalent:
var healthString = Derived.From(Health, health => $"Health: {health}");
MyText.BindText(healthString);
// Convenient method on Bindable, internally uses Derived.From
var healthString = Health.Derive(health => $"Health: {health}");
MyText.BindText(healthString);
// Convenient generated extensions method on TextMeshProUGUI
MyText.BindText(Health, health => $"Health: {health}");
You can combine multiple bindables, pass all of them to Derived.From
public Bindable<string> Name = new("Superman");
public Bindable<int> Health = new(100);
void Start()
{
var healthString = Derived.From(Name, Health, (n, h) => $"{n}'s Health: {health}");
MyText.BindText(healthString);
}
// MyText will now display "Superman's Health: 100"
Extracting Bindables from Bindables
You can also use Derived to extract a Bindable property from another Bindable. Suppose we have a Player class with Health and Name bindable properties, and a PlayerNameTag with a Bindable<Player> property:
class Player
{
public Bindable<string> Name;
public Bindable<float> Health;
}
public class PlayerNameTag : MonoBehaviour
{
public Bindable<Player> Player;
public TextMeshProUGUI MyText;
void Start()
{
var name = Player.Derive(p => p.Name);
var health = Player.Derive(p => p.Health);
var healthString = Derived.From(name, health, (n, h) => $"{n}'s Health: {h}");
MyText.BindText(healthString)
}
}
MyText will update any time Player, Player.Health, or Player.Name are changed.