I have a custom element that has some properties being exposed. Using code similar to here, I’m using the public properties to inline my element into the UMXL and then set the properties there.
In the editor this is working great - I can play with the values and see the preview update live, which is neat.
Once I’m actually running the game though, I find that changing my properties doesn’t trigger any notifications to the custom element to update the view and call Init() again. I see some usages in the docs of things like “INotifyValueChanged”, but I’m not sure how to connect this to the properties.
Is there a way at runtime to have the custom element call Init() again when it’s public properties change? That being said, it’s a bit strange to be calling Init() constantly anyway, so is there a way to subscribe to changes to the individual properties? Ie, setup some kind of delegate that gets called with the property changes?
Thanks.
In my understanding “Init” in the UxmlTraits class should only be called once in conjunction with the creation of the element. The UIBuilder seems to reset/reload the entire UXML many times, so init is called frequently there. But in runtime it is only called once as someone could expected according to the name “Init”.
So you want to simply be notified when properties of your control change at runtime. Here you can use the plain C# property feature. Instead of using auto-properties, you can use properties with backing field. Then you can react on the change and do the stuff needed within the control. Example:
private string example;
public string Example
{
get => this.example;
set
{
this.example = value;
// Do your stuff here to change the status/behaviour appropriate to the changed value
}
}
Note: As stated in the Unity docs, the property has to be named the same as the name of the Uxml???AttributeDescription without Dashes. You can still use UpperCamelCase.