'unity - how to update an object when a serialized field is changed?
I'm sure the question is already out there but I cannot find it, sorry.
I'm trying to sync a Serialized field of an object with other of its components.
let's say I have a field "size" which should impact the object transform scale:
[SerializeField]
int _size;
I'm looking for an event handler or something that allow me to do:
void onSerializedPropertyChange() {
transform.localScale = new Vector3(_size,_size,_size);
}
Does a method like that exists ?
In the end the idea would be to work with more than one property and to tweak object properties while preview the result.
Solution 1:[1]
public class UIAnimatorGroup:MonoBehaviour
{
public UIAnimatorTransform _animatorTransform;
}
[System.Serializable]
public class UIAnimatorTransform
{
[HideInInspector] public Matrix4x4 LocalMatrix4X4; // ??
[HideInInspector] public Matrix4x4 GobleMatrix4X4;
[HideInInspector] public UIAnimatorTransform parent;
[SerializeField]
[OnChangedCall("onSerializedPropertyChange","UIAnimatorTransform")]
private Vector3 position;
[SerializeField]
[OnChangedCall("onSerializedPropertyChange","UIAnimatorTransform")]
private Vector3 rotation;
[SerializeField]
[OnChangedCall("onSerializedPropertyChange","UIAnimatorTransform")]
private Vector3 scale;
public void onSerializedPropertyChange()
{
SetLocalMatrixByTransform();
CalculateGobleTransform();
}
}
Solution 2:[2]
How about [ExecuteInEditMode]?
Then you can use the normal Update() method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user17596091 |
| Solution 2 | ARZ |
