Skip to content

Commit

Permalink
Added Enable/Disable support for multiple components
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipAlg committed Nov 11, 2024
1 parent 1f7b799 commit fa62560
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 17 deletions.
51 changes: 34 additions & 17 deletions AGXUnity/AerodynamicsParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,47 @@ public bool AerodynamicsEnabled
set
{
m_aerodynamicsEnabled = value;
if ( m_objects != null && WindAndWaterManager.HasInstance ) {
var manager = WindAndWaterManager.Instance.GetInitialized<WindAndWaterManager>().Native;
foreach ( var shape in m_objects.Shapes ) {
if ( shape.GetInitialized<Collide.Shape>() == null )
continue;
SetEnableAerodynamics( m_aerodynamicsEnabled );
}
}

private void SetEnableAerodynamics( bool enable )
{
if ( m_objects != null && WindAndWaterManager.HasInstance ) {
var manager = WindAndWaterManager.Instance.GetInitialized<WindAndWaterManager>().Native;
foreach ( var shape in m_objects.Shapes ) {
if ( shape.GetInitialized<Collide.Shape>() == null )
continue;

manager.setEnableAerodynamics( shape.NativeGeometry, m_aerodynamicsEnabled );
}
manager.setEnableAerodynamics( shape.NativeGeometry, enable );
}

foreach ( var wire in m_objects.Wires ) {
if ( wire.GetInitialized<Wire>() == null )
continue;
foreach ( var wire in m_objects.Wires ) {
if ( wire.GetInitialized<Wire>() == null )
continue;

manager.setEnableAerodynamics( wire.Native, m_aerodynamicsEnabled );
}
manager.setEnableAerodynamics( wire.Native, enable );
}

foreach ( var cable in m_objects.Cables ) {
if ( cable.GetInitialized<Cable>() == null )
continue;
foreach ( var cable in m_objects.Cables ) {
if ( cable.GetInitialized<Cable>() == 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();
}
}
}
5 changes: 5 additions & 0 deletions AGXUnity/CableDamage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ protected override bool Initialize()

void Update()
{
if ( !enabled )
return;

m_currentDamages = Native.getCurrentDamages();
m_accumulatedDamages = Native.getAccumulatedDamages();

Expand Down Expand Up @@ -153,10 +156,12 @@ protected override void OnDestroy()

protected override void OnEnable()
{
RenderCableDamage = RenderCableDamage;
}

protected override void OnDisable()
{
CableRenderer.SetRenderDamages( false );
}

private void Reset()
Expand Down
16 changes: 16 additions & 0 deletions AGXUnity/CollisionGroupsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
6 changes: 6 additions & 0 deletions AGXUnity/HydrodynamicsParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ namespace AGXUnity
[HelpURL( "https://us.download.algoryx.se/AGXUnity/documentation/current/editor_interface.html#hydrodynamics-parameters" )]
public class HydrodynamicsParameters : WindAndWaterParameters<agxModel.HydrodynamicsParameters>
{
protected override void OnDisable()
{
if ( State == States.INITIALIZED && !enabled )
Debug.LogWarning( "Disabling an initialized HydrodynamicsParameters at runtime is not supported" );
base.OnDisable();
}
}
}
22 changes: 22 additions & 0 deletions AGXUnity/Model/ConveyorBelt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Track>();
Expand Down Expand Up @@ -764,6 +784,8 @@ private void OnDrawGizmosSelected()
{
if ( m_nodeGizmoMesh == null )
m_nodeGizmoMesh = Resources.GetBuiltinResource<Mesh>( "Cube.fbx" );
if ( !enabled )
return;
var initialized = m_tracks != null && m_tracks.Length > 0;
var tracks = initialized ? m_tracks : FindTracks();
var color = Color.black;
Expand Down
14 changes: 14 additions & 0 deletions AGXUnity/Model/OneBodyTire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
14 changes: 14 additions & 0 deletions AGXUnity/Model/TrackWheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions AGXUnity/Model/TwoBodyTire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions AGXUnity/Rendering/TerrainPatchRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Terrain>().terrainData;
Expand Down
14 changes: 14 additions & 0 deletions AGXUnity/WindAndWaterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit fa62560

Please sign in to comment.