From c2fd41f25ab1ae40622f33f730c86ea802ea3326 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 13 Aug 2015 11:11:05 +0100 Subject: [PATCH] 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. --- .../Fungus/Flowchart/Editor/BlockInspector.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Assets/Fungus/Flowchart/Editor/BlockInspector.cs b/Assets/Fungus/Flowchart/Editor/BlockInspector.cs index 99d7b53e..88fd11a0 100644 --- a/Assets/Fungus/Flowchart/Editor/BlockInspector.cs +++ b/Assets/Fungus/Flowchart/Editor/BlockInspector.cs @@ -3,6 +3,7 @@ using UnityEngine.Serialization; using UnityEditor; using System.Collections; using System.Collections.Generic; +using System.Linq; namespace Fungus { @@ -34,7 +35,8 @@ namespace Fungus protected Command activeCommand; // Command currently being inspected // Cached command editors to avoid creating / destroying editors more than necessary - protected Dictionary cachedCommandEditors = new Dictionary(); + // This list is static so persists between + protected static List cachedCommandEditors = new List(); protected void OnDestroy() { @@ -53,10 +55,11 @@ namespace Fungus protected void ClearEditors() { - foreach (CommandEditor commandEditor in cachedCommandEditors.Values) + foreach (CommandEditor commandEditor in cachedCommandEditors) { DestroyImmediate(commandEditor); } + cachedCommandEditors.Clear(); activeCommandEditor = null; } @@ -130,15 +133,18 @@ namespace Fungus inspectCommand != activeCommandEditor.target) { // See if we have a cached version of the command editor already, - // if not then create a new one. - if (cachedCommandEditors.ContainsKey(inspectCommand)) + var editors = (from e in cachedCommandEditors where (e.target == inspectCommand) select e); + + if (editors.Count() > 0) { - activeCommandEditor = cachedCommandEditors[inspectCommand]; + // Use cached editor + activeCommandEditor = editors.First(); } else { + // No cached editor, so create a new one. activeCommandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor; - cachedCommandEditors[inspectCommand] = activeCommandEditor; + cachedCommandEditors.Add(activeCommandEditor); } } if (activeCommandEditor != null)