Browse Source

Fix for intermittent null reference error from custom editors

Made cache into a static list so it persists between serialization /
deserialization. This should guarantee that every editor that gets
created gets destroyed later.
master
chrisgregan 10 years ago
parent
commit
c2fd41f25a
  1. 18
      Assets/Fungus/Flowchart/Editor/BlockInspector.cs

18
Assets/Fungus/Flowchart/Editor/BlockInspector.cs

@ -3,6 +3,7 @@ using UnityEngine.Serialization;
using UnityEditor; using UnityEditor;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Fungus namespace Fungus
{ {
@ -34,7 +35,8 @@ namespace Fungus
protected Command activeCommand; // Command currently being inspected protected Command activeCommand; // Command currently being inspected
// Cached command editors to avoid creating / destroying editors more than necessary // Cached command editors to avoid creating / destroying editors more than necessary
protected Dictionary<Command, CommandEditor> cachedCommandEditors = new Dictionary<Command, CommandEditor>(); // This list is static so persists between
protected static List<CommandEditor> cachedCommandEditors = new List<CommandEditor>();
protected void OnDestroy() protected void OnDestroy()
{ {
@ -53,10 +55,11 @@ namespace Fungus
protected void ClearEditors() protected void ClearEditors()
{ {
foreach (CommandEditor commandEditor in cachedCommandEditors.Values) foreach (CommandEditor commandEditor in cachedCommandEditors)
{ {
DestroyImmediate(commandEditor); DestroyImmediate(commandEditor);
} }
cachedCommandEditors.Clear(); cachedCommandEditors.Clear();
activeCommandEditor = null; activeCommandEditor = null;
} }
@ -130,15 +133,18 @@ namespace Fungus
inspectCommand != activeCommandEditor.target) inspectCommand != activeCommandEditor.target)
{ {
// See if we have a cached version of the command editor already, // See if we have a cached version of the command editor already,
// if not then create a new one. var editors = (from e in cachedCommandEditors where (e.target == inspectCommand) select e);
if (cachedCommandEditors.ContainsKey(inspectCommand))
if (editors.Count() > 0)
{ {
activeCommandEditor = cachedCommandEditors[inspectCommand]; // Use cached editor
activeCommandEditor = editors.First();
} }
else else
{ {
// No cached editor, so create a new one.
activeCommandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor; activeCommandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor;
cachedCommandEditors[inspectCommand] = activeCommandEditor; cachedCommandEditors.Add(activeCommandEditor);
} }
} }
if (activeCommandEditor != null) if (activeCommandEditor != null)

Loading…
Cancel
Save