From fa625601428483c9f83f9d0a5746902d5c49435a Mon Sep 17 00:00:00 2001 From: Filip Henningsson Date: Mon, 11 Nov 2024 13:17:45 +0100 Subject: [PATCH] Added Enable/Disable support for multiple components --- AGXUnity/AerodynamicsParameters.cs | 51 ++++++++++++++-------- AGXUnity/CableDamage.cs | 5 +++ AGXUnity/CollisionGroupsManager.cs | 16 +++++++ AGXUnity/HydrodynamicsParameters.cs | 6 +++ AGXUnity/Model/ConveyorBelt.cs | 22 ++++++++++ AGXUnity/Model/OneBodyTire.cs | 14 ++++++ AGXUnity/Model/TrackWheel.cs | 14 ++++++ AGXUnity/Model/TwoBodyTire.cs | 14 ++++++ AGXUnity/Rendering/TerrainPatchRenderer.cs | 19 ++++++++ AGXUnity/WindAndWaterManager.cs | 14 ++++++ 10 files changed, 158 insertions(+), 17 deletions(-) diff --git a/AGXUnity/AerodynamicsParameters.cs b/AGXUnity/AerodynamicsParameters.cs index d390acaf..c09247d8 100644 --- a/AGXUnity/AerodynamicsParameters.cs +++ b/AGXUnity/AerodynamicsParameters.cs @@ -15,30 +15,47 @@ public bool AerodynamicsEnabled set { m_aerodynamicsEnabled = value; - if ( m_objects != null && WindAndWaterManager.HasInstance ) { - var manager = WindAndWaterManager.Instance.GetInitialized().Native; - foreach ( var shape in m_objects.Shapes ) { - if ( shape.GetInitialized() == null ) - continue; + SetEnableAerodynamics( m_aerodynamicsEnabled ); + } + } + + private void SetEnableAerodynamics( bool enable ) + { + if ( m_objects != null && WindAndWaterManager.HasInstance ) { + var manager = WindAndWaterManager.Instance.GetInitialized().Native; + foreach ( var shape in m_objects.Shapes ) { + if ( shape.GetInitialized() == null ) + continue; - manager.setEnableAerodynamics( shape.NativeGeometry, m_aerodynamicsEnabled ); - } + manager.setEnableAerodynamics( shape.NativeGeometry, enable ); + } - foreach ( var wire in m_objects.Wires ) { - if ( wire.GetInitialized() == null ) - continue; + foreach ( var wire in m_objects.Wires ) { + if ( wire.GetInitialized() == null ) + continue; - manager.setEnableAerodynamics( wire.Native, m_aerodynamicsEnabled ); - } + manager.setEnableAerodynamics( wire.Native, enable ); + } - foreach ( var cable in m_objects.Cables ) { - if ( cable.GetInitialized() == null ) - continue; + foreach ( var cable in m_objects.Cables ) { + if ( cable.GetInitialized() == null ) + continue; - manager.setEnableAerodynamics( cable.Native, m_aerodynamicsEnabled ); - } + manager.setEnableAerodynamics( cable.Native, enable ); } } } + + protected override void OnDisable() + { + SetEnableAerodynamics( false ); + base.OnDisable(); + } + + protected override void OnEnable() + { + SetEnableAerodynamics( m_aerodynamicsEnabled ); + base.OnEnable(); + } } } diff --git a/AGXUnity/CableDamage.cs b/AGXUnity/CableDamage.cs index 64e679a7..664e4026 100644 --- a/AGXUnity/CableDamage.cs +++ b/AGXUnity/CableDamage.cs @@ -118,6 +118,9 @@ protected override bool Initialize() void Update() { + if ( !enabled ) + return; + m_currentDamages = Native.getCurrentDamages(); m_accumulatedDamages = Native.getAccumulatedDamages(); @@ -153,10 +156,12 @@ protected override void OnDestroy() protected override void OnEnable() { + RenderCableDamage = RenderCableDamage; } protected override void OnDisable() { + CableRenderer.SetRenderDamages( false ); } private void Reset() diff --git a/AGXUnity/CollisionGroupsManager.cs b/AGXUnity/CollisionGroupsManager.cs index 3e9b7bc1..c1a2c1e9 100644 --- a/AGXUnity/CollisionGroupsManager.cs +++ b/AGXUnity/CollisionGroupsManager.cs @@ -96,5 +96,21 @@ private void SetEnablePair( CollisionGroupEntryPair pair, bool enable, agxCollid { space.setEnablePair( pair.First.Tag.To32BitFnv1aHash(), pair.Second.Tag.To32BitFnv1aHash(), enable ); } + + protected override void OnEnable() + { + var space = GetSimulation().getSpace(); + foreach ( var pair in m_disabledPairs ) + SetEnablePair( pair, false, space ); + base.OnEnable(); + } + + protected override void OnDisable() + { + var space = GetSimulation().getSpace(); + foreach ( var pair in m_disabledPairs ) + SetEnablePair( pair, true, space ); + base.OnDisable(); + } } } diff --git a/AGXUnity/HydrodynamicsParameters.cs b/AGXUnity/HydrodynamicsParameters.cs index b031d905..f6b77d06 100644 --- a/AGXUnity/HydrodynamicsParameters.cs +++ b/AGXUnity/HydrodynamicsParameters.cs @@ -6,5 +6,11 @@ namespace AGXUnity [HelpURL( "https://us.download.algoryx.se/AGXUnity/documentation/current/editor_interface.html#hydrodynamics-parameters" )] public class HydrodynamicsParameters : WindAndWaterParameters { + protected override void OnDisable() + { + if ( State == States.INITIALIZED && !enabled ) + Debug.LogWarning( "Disabling an initialized HydrodynamicsParameters at runtime is not supported" ); + base.OnDisable(); + } } } diff --git a/AGXUnity/Model/ConveyorBelt.cs b/AGXUnity/Model/ConveyorBelt.cs index 7f86aa93..b61ba117 100644 --- a/AGXUnity/Model/ConveyorBelt.cs +++ b/AGXUnity/Model/ConveyorBelt.cs @@ -616,6 +616,26 @@ protected override void OnDestroy() base.OnDestroy(); } + protected override void OnEnable() + { + foreach ( var track in Tracks ) + track.enabled = true; + base.OnEnable(); + } + + protected override void OnDisable() + { + foreach ( var track in Tracks ) + track.enabled = false; + base.OnDisable(); + } + + public override void EditorUpdate() + { + foreach ( var track in Tracks ) + track.enabled = enabled; + } + private void SynchronizeNumberOfTracks() { var tracks = GetComponents(); @@ -764,6 +784,8 @@ private void OnDrawGizmosSelected() { if ( m_nodeGizmoMesh == null ) m_nodeGizmoMesh = Resources.GetBuiltinResource( "Cube.fbx" ); + if ( !enabled ) + return; var initialized = m_tracks != null && m_tracks.Length > 0; var tracks = initialized ? m_tracks : FindTracks(); var color = Color.black; diff --git a/AGXUnity/Model/OneBodyTire.cs b/AGXUnity/Model/OneBodyTire.cs index 1b9c2ed3..4b05f3b8 100644 --- a/AGXUnity/Model/OneBodyTire.cs +++ b/AGXUnity/Model/OneBodyTire.cs @@ -105,5 +105,19 @@ protected override void OnDestroy() Native = null; base.OnDestroy(); } + + protected override void OnDisable() + { + if ( Simulation.HasInstance ) + GetSimulation().remove( Native ); + base.OnDisable(); + } + + protected override void OnEnable() + { + if ( Simulation.HasInstance && Native != null ) + GetSimulation().add( Native ); + base.OnEnable(); + } } } diff --git a/AGXUnity/Model/TrackWheel.cs b/AGXUnity/Model/TrackWheel.cs index c0e8d9c8..249d3775 100644 --- a/AGXUnity/Model/TrackWheel.cs +++ b/AGXUnity/Model/TrackWheel.cs @@ -261,6 +261,20 @@ protected override void OnDestroy() base.OnDestroy(); } + protected override void OnDisable() + { + if ( Native != null && !enabled ) + Debug.LogWarning( "Disabling an initialized TrackWheel at runtime is not supported" ); + base.OnDisable(); + } + + protected override void OnEnable() + { + if ( Native != null ) + Debug.LogWarning( "Enabling an initialized TrackWheel at runtime is not supported" ); + base.OnEnable(); + } + private void Reset() { // We cannot use this.RigidBody when it used frame parent and diff --git a/AGXUnity/Model/TwoBodyTire.cs b/AGXUnity/Model/TwoBodyTire.cs index 638f4943..4653eeb2 100644 --- a/AGXUnity/Model/TwoBodyTire.cs +++ b/AGXUnity/Model/TwoBodyTire.cs @@ -285,6 +285,20 @@ protected override void OnDestroy() base.OnDestroy(); } + protected override void OnDisable() + { + if ( Simulation.HasInstance ) + GetSimulation().remove( Native ); + base.OnDisable(); + } + + protected override void OnEnable() + { + if ( Simulation.HasInstance && Native != null ) + GetSimulation().add( Native ); + base.OnEnable(); + } + private bool m_tireRimConstraintInitialState = true; } } diff --git a/AGXUnity/Rendering/TerrainPatchRenderer.cs b/AGXUnity/Rendering/TerrainPatchRenderer.cs index 8e762846..dcf0f994 100644 --- a/AGXUnity/Rendering/TerrainPatchRenderer.cs +++ b/AGXUnity/Rendering/TerrainPatchRenderer.cs @@ -180,6 +180,25 @@ protected override void OnDestroy() base.OnDestroy(); } + protected override void OnDisable() + { + if ( Simulation.HasInstance ) { + Simulation.Instance.StepCallbacks.SimulationPost-= PostStep; + terrain.OnModification -= UpdateTextureAt; + } + + base.OnDisable(); + } + + protected override void OnEnable() + { + if ( Simulation.HasInstance ) { + Simulation.Instance.StepCallbacks.SimulationPost += PostStep; + terrain.OnModification += UpdateTextureAt; + } + base.OnEnable(); + } + private void PostStep() { var td = GetComponent().terrainData; diff --git a/AGXUnity/WindAndWaterManager.cs b/AGXUnity/WindAndWaterManager.cs index ba41a69c..d9670dfc 100644 --- a/AGXUnity/WindAndWaterManager.cs +++ b/AGXUnity/WindAndWaterManager.cs @@ -111,5 +111,19 @@ protected override void OnDestroy() base.OnDestroy(); } + + protected override void OnDisable() + { + if ( Simulation.HasInstance ) + GetSimulation().remove( Native ); + base.OnDisable(); + } + + protected override void OnEnable() + { + if ( Simulation.HasInstance ) + GetSimulation().add( Native ); + base.OnEnable(); + } } }