Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up AcademyFixedUpdateStepper when playmode changed #4532

Merged
merged 18 commits into from
Oct 7, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions com.unity.ml-agents/Runtime/Academy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ namespace Unity.MLAgents
/// </summary>
internal class AcademyFixedUpdateStepper : MonoBehaviour
{
void OnEnable()
{
// Check if this stepper belongs to current Academy singleton
// if (!Academy.Instance.IsStepperOwner(this))
dongruoping marked this conversation as resolved.
Show resolved Hide resolved
// {
// Destroy(this);
// }
}

void FixedUpdate()
{
Academy.Instance.EnvironmentStep();
Expand Down Expand Up @@ -222,7 +231,25 @@ public int InferenceSeed
Application.quitting += Dispose;

LazyInitialize();

#if UNITY_EDITOR
EditorApplication.playModeStateChanged += HandleOnPlayModeChanged;
#endif
}

#if UNITY_EDITOR
/// <summary>
/// Clean up the Academy when switching from edit mode to play mode
/// </summary>
/// <param name="state">State.</param>
void HandleOnPlayModeChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingEditMode)
{
Dispose();
}
}
#endif

/// <summary>
/// Initialize the Academy if it hasn't already been initialized.
Expand Down Expand Up @@ -631,5 +658,13 @@ public void Dispose()
// Reset the Lazy instance
s_Lazy = new Lazy<Academy>(() => new Academy());
}

/// <summary>
/// Check if the input AcademyFixedUpdateStepper belongs to this Academy.
/// </summary>
internal bool IsStepperOwner(AcademyFixedUpdateStepper stepper)
{
return stepper.gameObject.Equals(Academy.Instance.m_StepperObject);
dongruoping marked this conversation as resolved.
Show resolved Hide resolved
}
}
}