Skip to content

Commit 5e4a239

Browse files
committed
Fix serialization of RigidBodySensorComponents
The settings of `RigidBodySensorComponent`s were not getting serialized and saved properly before. Now they are. Fixes Unity-Technologies#5774
1 parent 71b121c commit 5e4a239

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using UnityEditor;
21
using Unity.MLAgents.Editor;
32
using Unity.MLAgents.Extensions.Sensors;
3+
using UnityEditor;
44

55
namespace Unity.MLAgents.Extensions.Editor
66
{
77
[CustomEditor(typeof(RigidBodySensorComponent))]
8-
[CanEditMultipleObjects]
98
internal class RigidBodySensorComponentEditor : UnityEditor.Editor
109
{
1110
bool ShowHierarchy = true;
@@ -26,8 +25,6 @@ public override void OnInspectorGUI()
2625
);
2726
}
2827

29-
bool requireExtractorUpdate;
30-
3128
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());
3229
{
3330
// All the fields affect the sensor order or observation size,
@@ -36,8 +33,11 @@ public override void OnInspectorGUI()
3633
EditorGUILayout.PropertyField(so.FindProperty("RootBody"), true);
3734
EditorGUILayout.PropertyField(so.FindProperty("VirtualRoot"), true);
3835

39-
// Changing the root body or virtual root changes the hierarchy, so we need to reset later.
40-
requireExtractorUpdate = EditorGUI.EndChangeCheck();
36+
// Changing the root body or virtual root changes the hierarchy, so we need to reset.
37+
if (EditorGUI.EndChangeCheck())
38+
{
39+
rbSensorComp.ResetPoseExtractor();
40+
}
4141

4242
EditorGUILayout.PropertyField(so.FindProperty("Settings"), true);
4343

@@ -47,13 +47,13 @@ public override void OnInspectorGUI()
4747
{
4848
var treeNodes = rbSensorComp.GetDisplayNodes();
4949
var originalIndent = EditorGUI.indentLevel;
50+
var poseEnabled = so.FindProperty("m_PoseExtractor").FindPropertyRelative("m_PoseEnabled");
5051
foreach (var node in treeNodes)
5152
{
5253
var obj = node.NodeObject;
5354
var objContents = EditorGUIUtility.ObjectContent(obj, obj.GetType());
5455
EditorGUI.indentLevel = originalIndent + node.Depth;
55-
var enabled = EditorGUILayout.Toggle(objContents, node.Enabled);
56-
rbSensorComp.SetPoseEnabled(node.OriginalIndex, enabled);
56+
EditorGUILayout.PropertyField(poseEnabled.GetArrayElementAtIndex(node.OriginalIndex), objContents);
5757
}
5858

5959
EditorGUI.indentLevel = originalIndent;
@@ -64,12 +64,6 @@ public override void OnInspectorGUI()
6464
EditorGUI.EndDisabledGroup();
6565

6666
so.ApplyModifiedProperties();
67-
if (requireExtractorUpdate)
68-
{
69-
rbSensorComp.ResetPoseExtractor();
70-
}
7167
}
72-
73-
7468
}
7569
}

com.unity.ml-agents.extensions/Runtime/Sensors/PoseExtractor.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ namespace Unity.MLAgents.Extensions.Sensors
1414
/// Poses are either considered in model space, which is relative to a root body,
1515
/// or in local space, which is relative to their parent.
1616
/// </summary>
17-
public abstract class PoseExtractor
17+
[Serializable]
18+
public abstract class PoseExtractor : ISerializationCallbackReceiver
1819
{
19-
int[] m_ParentIndices;
20+
[SerializeField] int[] m_ParentIndices;
2021
Pose[] m_ModelSpacePoses;
2122
Pose[] m_LocalSpacePoses;
2223

2324
Vector3[] m_ModelSpaceLinearVelocities;
2425
Vector3[] m_LocalSpaceLinearVelocities;
2526

26-
bool[] m_PoseEnabled;
27+
[SerializeField] bool[] m_PoseEnabled;
2728

2829

2930
/// <summary>
@@ -177,11 +178,8 @@ protected void Setup(int[] parentIndices)
177178
#endif
178179
m_ParentIndices = parentIndices;
179180
var numPoses = parentIndices.Length;
180-
m_ModelSpacePoses = new Pose[numPoses];
181-
m_LocalSpacePoses = new Pose[numPoses];
182181

183-
m_ModelSpaceLinearVelocities = new Vector3[numPoses];
184-
m_LocalSpaceLinearVelocities = new Vector3[numPoses];
182+
CreateSensorArrays(numPoses);
185183

186184
m_PoseEnabled = new bool[numPoses];
187185
// All poses are enabled by default. Generally we'll want to disable the root though.
@@ -191,6 +189,25 @@ protected void Setup(int[] parentIndices)
191189
}
192190
}
193191

192+
public void OnBeforeSerialize()
193+
{
194+
// no-op
195+
}
196+
197+
public void OnAfterDeserialize()
198+
{
199+
CreateSensorArrays(m_ParentIndices.Length);
200+
}
201+
202+
private void CreateSensorArrays(int numPoses)
203+
{
204+
m_ModelSpacePoses = new Pose[numPoses];
205+
m_LocalSpacePoses = new Pose[numPoses];
206+
207+
m_ModelSpaceLinearVelocities = new Vector3[numPoses];
208+
m_LocalSpaceLinearVelocities = new Vector3[numPoses];
209+
}
210+
194211
/// <summary>
195212
/// Return the world space Pose of the i'th object.
196213
/// </summary>

com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyPoseExtractor.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Collections.Generic;
23
using UnityEngine;
4+
using Object = UnityEngine.Object;
35

46
namespace Unity.MLAgents.Extensions.Sensors
57
{
@@ -8,15 +10,16 @@ namespace Unity.MLAgents.Extensions.Sensors
810
/// Utility class to track a hierarchy of RigidBodies. These are assumed to have a root node,
911
/// and child nodes are connect to their parents via Joints.
1012
/// </summary>
13+
[Serializable]
1114
public class RigidBodyPoseExtractor : PoseExtractor
1215
{
13-
Rigidbody[] m_Bodies;
16+
[SerializeField] Rigidbody[] m_Bodies;
1417

1518
/// <summary>
1619
/// Optional game object used to determine the root of the poses, separate from the actual Rigidbodies
1720
/// in the hierarchy. For locomotion
1821
/// </summary>
19-
GameObject m_VirtualRoot;
22+
[SerializeField] GameObject m_VirtualRoot;
2023

2124
/// <summary>
2225
/// Initialize given a root RigidBody.

0 commit comments

Comments
 (0)