Unity - Manual: Create a bindable custom control (unity3d.com)
The code below works just fine when I follow the link and mimic it.
public class ExampleField : BaseField<double>
{
new class UxmlFactory : UxmlFactory<ExampleField, UxmlTraits> { }
new class UxmlTraits : BaseField<double>.UxmlTraits { }
FloatField m_Input;
IntegerField m_Input2;
public ExampleField() : this(null)
{
}
public ExampleField(string label) : base(label, new Label() { })
{
// This is the input element instantiated for the base constructor.
m_Input = new FloatField();
m_Input2 = new IntegerField();
Add(m_Input);
Add(m_Input2);
m_Input.RegisterValueChangedCallback(OnValueChanged);
m_Input2.RegisterValueChangedCallback(OnValueChanged);
}
void OnValueChanged(ChangeEvent<int> evt)
{
value = evt.newValue;
}
void OnValueChanged(ChangeEvent<float> evt)
{
value = evt.newValue;
}
public override void SetValueWithoutNotify(double newValue)
{
base.SetValueWithoutNotify(newValue);
m_Input.value = (float)value;
m_Input2.value = (int)value;
}
}
But when I use Custom Data as the BaseField data format, the SetValueWithoutNotify part of the code below breaks. There is no way to get or modify the value of BaseField's value. How can I use Custom Data in BaseField? Thanks.
public class ExampleField : BaseField<MyStruct>
{
new class UxmlFactory : UxmlFactory<ExampleField, UxmlTraits> { }
new class UxmlTraits : BaseField<MyStruct>.UxmlTraits { }
FloatField m_Input;
IntegerField m_Input2;
public ExampleField() : this(null)
{
}
public ExampleField(string label) : base(label, new Label() { })
{
// This is the input element instantiated for the base constructor.
m_Input = new FloatField();
m_Input2 = new IntegerField();
Add(m_Input);
Add(m_Input2);
//m_Input.RegisterValueChangedCallback(OnValueChanged);
//m_Input2.RegisterValueChangedCallback(OnValueChanged);
}
void OnValueChanged(ChangeEvent<int> evt)
{
//value = evt.newValue;
value.intValue = evt.newValue;
}
void OnValueChanged(ChangeEvent<float> evt)
{
//value = evt.newValue;
value.floatValue = evt.newValue;
}
public override void SetValueWithoutNotify(MyStruct newValue)
{
base.SetValueWithoutNotify(newValue);
//m_Input.value = (float)value;
//m_Input2.value = (int)value;
m_Input.value = value.floatValue;
m_Input2.value = value.intValue;
}
}
[Serializable]
public class MyStruct
{
public float floatValue;
public int intValue;
}