using UnityEngine; using UnityEngine.Serialization; namespace Flexalon { /// /// Base type for many Flexalon components. Deals with FlexalonNode lifecycle, /// and provides the ForceUpdate and MarkDirty methods to trigger a Flexalon update. /// [ExecuteAlways, RequireComponent(typeof(FlexalonResult))] public abstract class FlexalonComponent : MonoBehaviour { protected FlexalonNode _node; /// The FlexalonNode associated with this gameObject. public FlexalonNode Node => _node; [SerializeField, HideInInspector, FormerlySerializedAs("_initialized")] private int _version; private static readonly int _currentVersion = 4; void Update() { DoUpdate(); } void OnEnable() { _node = Flexalon.GetOrCreateNode(gameObject); DoOnEnable(); if (_version == 0) { Initialize(); } else if (_version < _currentVersion) { Upgrade(_version); } if (!_node.HasResult || _version == 0) { MarkDirty(); } else { UpdateProperties(); } _version = _currentVersion; } void OnDisable() { DoOnDisable(); } void OnDestroy() { if (_node != null) { ResetProperties(); Flexalon.RecordFrameChanges = true; _node.MarkDirty(); _node = null; } } /// Marks this component needing an update. The Flexalon singleton /// will visit it in dependency order on LateUpdate. public void MarkDirty() { if (_node != null) { UpdateProperties(); _node.MarkDirty(); } } /// Forces this component, its parent nodes, and its children nodes to update immediately. public void ForceUpdate() { _node = Flexalon.GetOrCreateNode(gameObject); MarkDirty(); _node.ForceUpdate(); } void OnDidApplyAnimationProperties() { MarkDirty(); } /// Called when the component is enabled to apply properties to the FlexalonNode. protected virtual void UpdateProperties() {} /// Called when the component is destroyed to reset properties on the FlexalonNode. protected virtual void ResetProperties() {} /// Called when the component is enabled. protected virtual void DoOnEnable() {} /// Called when the component is disabled. protected virtual void DoOnDisable() {} /// Called when the component is updated. public virtual void DoUpdate() {} /// Called when the component is first created. protected virtual void Initialize() {} /// Called when the component is upgraded to a new version of Flexalon. protected virtual void Upgrade(int fromVersion) {} } }