Browse Source

Fixed null reference exceptions when deleting a command class

master
chrisgregan 10 years ago
parent
commit
c0384e6348
  1. 46
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs

46
Assets/Fungus/FungusScript/Editor/SequenceEditor.cs

@ -19,33 +19,14 @@ namespace Fungus
public int index; public int index;
} }
protected SerializedProperty sequenceNameProp;
public virtual void OnEnable()
{
sequenceNameProp = serializedObject.FindProperty("sequenceName");
/*
// Strip out any null commands from the command list
// This can happen if a command class is removed or renamed
serializedObject.Update();
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
for (int i = commandListProperty.arraySize - 1; i >= 0; --i)
{
SerializedProperty commandProperty = commandListProperty.GetArrayElementAtIndex(i);
if (commandProperty.objectReferenceValue == null)
{
commandListProperty.DeleteArrayElementAtIndex(i);
}
}
serializedObject.ApplyModifiedProperties();
*/
}
public virtual void DrawInspectorGUI(FungusScript fungusScript) public virtual void DrawInspectorGUI(FungusScript fungusScript)
{ {
serializedObject.Update(); serializedObject.Update();
// We acquire the serialized properties in the draw methods instead of in OnEnable as otherwise
// deleting or renaming a command class would generate a bunch of null reference exceptions.
SerializedProperty sequenceNameProp = serializedObject.FindProperty("sequenceName");
EditorGUILayout.PropertyField(sequenceNameProp); EditorGUILayout.PropertyField(sequenceNameProp);
EditorGUILayout.Separator(); EditorGUILayout.Separator();
@ -61,8 +42,9 @@ namespace Fungus
sequence.nodeRect.width = 240; sequence.nodeRect.width = 240;
ReorderableListGUI.Title(sequence.sequenceName);
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
ReorderableListGUI.Title(sequence.sequenceName);
CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0); CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0);
adaptor.nodeRect = sequence.nodeRect; adaptor.nodeRect = sequence.nodeRect;
@ -122,6 +104,17 @@ namespace Fungus
} }
} }
// Remove any null entries in the command list.
// This can happen when a command class is deleted or renamed.
for (int i = commandListProperty.arraySize - 1; i >= 0; --i)
{
SerializedProperty commandProperty = commandListProperty.GetArrayElementAtIndex(i);
if (commandProperty.objectReferenceValue == null)
{
commandListProperty.DeleteArrayElementAtIndex(i);
}
}
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
if (!Application.isPlaying) if (!Application.isPlaying)
@ -142,6 +135,11 @@ namespace Fungus
int indentLevel = 0; int indentLevel = 0;
foreach(Command command in sequence.commandList) foreach(Command command in sequence.commandList)
{ {
if (command == null)
{
continue;
}
indentLevel += command.GetPreIndent(); indentLevel += command.GetPreIndent();
command.indentLevel = Math.Max(indentLevel, 0); command.indentLevel = Math.Max(indentLevel, 0);
indentLevel += command.GetPostIndent(); indentLevel += command.GetPostIndent();

Loading…
Cancel
Save