Skip to content

Commit 50c2ecd

Browse files
authored
Make a serialization upgrade path for maxStep. (#3424) (#3434)
- Test comment out as we need a package that is only verified for 2019.2.
1 parent d230591 commit 50c2ecd

File tree

11 files changed

+591
-3
lines changed

11 files changed

+591
-3
lines changed

Project/ProjectSettings/EditorBuildSettings.asset

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
EditorBuildSettings:
55
m_ObjectHideFlags: 0
66
serializedVersion: 2
7-
m_Scenes: []
7+
m_Scenes:
8+
- enabled: 1
9+
path: Packages/com.unity.ml-agents/Tests/Runtime/SerializeTestScene.unity
10+
guid: 60783bd849bd242eeb66243542762b23
811
m_configObjects: {}

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,25 @@ internal struct AgentAction
106106
"docs/Learning-Environment-Design-Agents.md")]
107107
[Serializable]
108108
[RequireComponent(typeof(BehaviorParameters))]
109-
public abstract class Agent : MonoBehaviour
109+
public abstract class Agent : MonoBehaviour, ISerializationCallbackReceiver
110110
{
111111
IPolicy m_Brain;
112112
BehaviorParameters m_PolicyFactory;
113113

114+
/// This code is here to make the upgrade path for users using maxStep
115+
/// easier. We will hook into the Serialization code and make sure that
116+
/// agentParameters.maxStep and this.maxStep are in sync.
117+
[Serializable]
118+
internal struct AgentParameters
119+
{
120+
public int maxStep;
121+
}
122+
123+
[SerializeField] [HideInInspector]
124+
internal AgentParameters agentParameters;
125+
[SerializeField] [HideInInspector]
126+
internal bool hasUpgradedFromAgentParameters;
127+
114128
/// <summary>
115129
/// The maximum number of steps the agent takes before being done.
116130
/// </summary>
@@ -187,6 +201,26 @@ void OnEnable()
187201
LazyInitialize();
188202
}
189203

204+
205+
public void OnBeforeSerialize()
206+
{
207+
if (maxStep == 0 && maxStep != agentParameters.maxStep && !hasUpgradedFromAgentParameters)
208+
{
209+
maxStep = agentParameters.maxStep;
210+
211+
}
212+
hasUpgradedFromAgentParameters = true;
213+
}
214+
215+
public void OnAfterDeserialize()
216+
{
217+
if (maxStep == 0 && maxStep != agentParameters.maxStep && !hasUpgradedFromAgentParameters)
218+
{
219+
maxStep = agentParameters.maxStep;
220+
}
221+
hasUpgradedFromAgentParameters = true;
222+
}
223+
190224
/// Helper method for the <see cref="OnEnable"/> event, created to
191225
/// facilitate testing.
192226
public void LazyInitialize()
@@ -215,7 +249,6 @@ public void LazyInitialize()
215249
ResetData();
216250
InitializeAgent();
217251
InitializeSensors();
218-
219252
}
220253

221254
/// Monobehavior function that is called when the attached GameObject

com.unity.ml-agents/Tests/Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// using System.Collections;
2+
// using NUnit.Framework;
3+
// #if UNITY_EDITOR
4+
// using UnityEditor.SceneManagement;
5+
// using UnityEngine.TestTools;
6+
// #endif
7+
// using UnityEngine;
8+
// using UnityEngine.SceneManagement;
9+
//
10+
// namespace Tests
11+
// {
12+
// [TestFixture]
13+
// public class SerializationTest
14+
// {
15+
//
16+
// [SetUp]
17+
// public void Setup()
18+
// {
19+
// SceneManager.LoadScene("Packages/com.unity.ml-agents/Tests/Runtime/SerializeTestScene");
20+
// }
21+
//
22+
// /// <summary>
23+
// /// Test that the serialized agent in the scene, which has its agent parameter value serialized,
24+
// /// properly deserializes it to Agent.maxStep.
25+
// /// </summary>
26+
// [UnityTest]
27+
// public IEnumerator SerializationTestSimplePasses()
28+
// {
29+
// // Use the Assert class to test conditions
30+
// var gameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
31+
// if (SceneManager.GetActiveScene().name != "SerializeTestScene")
32+
// {
33+
// yield return null;
34+
// }
35+
//
36+
// GameObject agent = null;
37+
// foreach (var go in gameObjects)
38+
// {
39+
// if (go.name == "Agent")
40+
// {
41+
// agent = go;
42+
// break;
43+
// }
44+
// }
45+
// Assert.NotNull(agent);
46+
// var agentComponent = agent.GetComponent<SerializeAgent>();
47+
// Assert.True(agentComponent.maxStep == 5000);
48+
//
49+
// }
50+
// }
51+
// }

com.unity.ml-agents/Tests/Runtime/SerializationTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using MLAgents;
5+
6+
public class SerializeAgent : Agent
7+
{
8+
public override float[] Heuristic()
9+
{
10+
return new[] {0f};
11+
}
12+
}

com.unity.ml-agents/Tests/Runtime/SerializeAgent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)