Skip to content

Commit 6dc4763

Browse files
authored
[bugfix] Fix MLA-793 Make Unity lifecycle methods protected. Added tests for changes (#3590)
1 parent ec490f7 commit 6dc4763

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

Project/Project.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dont/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4242
- `AgentInfo.actionMasks` has been renamed to `AgentInfo.discreteActionMasks`.
4343
- `DecisionRequester` has been made internal (you can still use the DecisionRequesterComponent from the inspector). `RepeatAction` was renamed `TakeActionsBetweenDecisions` for clarity. (#3555)
4444
- The `IFloatProperties` interface has been removed.
45+
- Fix #3579.
4546

4647
## [0.14.1-preview] - 2020-02-25
4748

com.unity.ml-agents/Runtime/Agent.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ public string BehaviorName
219219
/// </summary>
220220
internal VectorSensor collectObservationsSensor;
221221

222-
void OnEnable()
222+
/// <summary>
223+
/// Called when the attached <see cref="GameObject"/> becomes enabled and active.
224+
/// </summary>
225+
protected virtual void OnEnable()
223226
{
224227
LazyInitialize();
225228
}
@@ -303,7 +306,10 @@ enum DoneReason
303306
Disabled,
304307
}
305308

306-
void OnDisable()
309+
/// <summary>
310+
/// Called when the attached <see cref="GameObject"/> becomes disabled and inactive.
311+
/// </summary>
312+
protected virtual void OnDisable()
307313
{
308314
DemonstrationWriters.Clear();
309315

@@ -573,6 +579,13 @@ internal void InitializeSensors()
573579
/// </summary>
574580
void SendInfoToBrain()
575581
{
582+
if (!m_Initialized)
583+
{
584+
throw new UnityAgentsException("Call to SendInfoToBrain when Agent hasn't been initialized." +
585+
"Please ensure that you are calling 'base.OnEnable()' if you have overridden OnEnable.");
586+
587+
}
588+
576589
if (m_Brain == null)
577590
{
578591
return;

com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.CodeDom;
12
using UnityEngine;
23
using NUnit.Framework;
34
using System.Reflection;
@@ -602,4 +603,61 @@ public void TestHeuristicPolicyStepsSensors()
602603
Assert.AreEqual(numSteps, agent1.sensor2.numCompressedCalls);
603604
}
604605
}
606+
607+
[TestFixture]
608+
public class TestOnEnableOverride
609+
{
610+
public class OnEnableAgent : Agent
611+
{
612+
public bool callBase;
613+
614+
protected override void OnEnable()
615+
{
616+
if (callBase)
617+
base.OnEnable();
618+
}
619+
}
620+
621+
static void _InnerAgentTestOnEnableOverride(bool callBase = false)
622+
{
623+
var go = new GameObject();
624+
var agent = go.AddComponent<OnEnableAgent>();
625+
agent.callBase = callBase;
626+
var onEnable = typeof(OnEnableAgent).GetMethod("OnEnable", BindingFlags.NonPublic | BindingFlags.Instance);
627+
var sendInfo = typeof(Agent).GetMethod("SendInfoToBrain", BindingFlags.NonPublic | BindingFlags.Instance);
628+
Assert.NotNull(onEnable);
629+
onEnable.Invoke(agent, null);
630+
Assert.NotNull(sendInfo);
631+
if (agent.callBase)
632+
{
633+
Assert.DoesNotThrow(() => sendInfo.Invoke(agent, null));
634+
}
635+
else
636+
{
637+
Assert.Throws<UnityAgentsException>(() =>
638+
{
639+
try
640+
{
641+
sendInfo.Invoke(agent, null);
642+
}
643+
catch (TargetInvocationException e)
644+
{
645+
throw e.GetBaseException();
646+
}
647+
});
648+
}
649+
}
650+
651+
[Test]
652+
public void TestAgentCallBaseOnEnable()
653+
{
654+
_InnerAgentTestOnEnableOverride(true);
655+
}
656+
657+
[Test]
658+
public void TestAgentDontCallBaseOnEnable()
659+
{
660+
_InnerAgentTestOnEnableOverride();
661+
}
662+
}
605663
}

0 commit comments

Comments
 (0)