From 367f0527f4d8d962067b1851454d36f6ffa3480b Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 2 Jun 2017 16:27:27 +0100 Subject: [PATCH] Replaced slow calls to FindObjectOfType() and FindDerivedTypes() --- Assets/Fungus/Scripts/Editor/BlockEditor.cs | 22 +++++++++++++++---- .../Fungus/Scripts/Editor/FlowchartWindow.cs | 12 ++++++---- .../Fungus/Scripts/Editor/VariableEditor.cs | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/BlockEditor.cs b/Assets/Fungus/Scripts/Editor/BlockEditor.cs index db037c2c..77dd47d7 100644 --- a/Assets/Fungus/Scripts/Editor/BlockEditor.cs +++ b/Assets/Fungus/Scripts/Editor/BlockEditor.cs @@ -39,6 +39,21 @@ namespace Fungus.EditorUtils protected Texture2D duplicateIcon; protected Texture2D deleteIcon; + static List commandTypes; + static List eventHandlerTypes; + + static void CacheEventHandlerTypes() + { + eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); + commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); + } + + [UnityEditor.Callbacks.DidReloadScripts] + private static void OnScriptsReloaded() + { + CacheEventHandlerTypes(); + } + protected virtual void OnEnable() { upIcon = FungusEditorResources.Up; @@ -46,6 +61,8 @@ namespace Fungus.EditorUtils addIcon = FungusEditorResources.Add; duplicateIcon = FungusEditorResources.Duplicate; deleteIcon = FungusEditorResources.Delete; + + CacheEventHandlerTypes(); } public virtual void DrawBlockName(Flowchart flowchart) @@ -317,8 +334,6 @@ namespace Fungus.EditorUtils { // Show available Event Handlers in a drop down list with type of current // event handler selected. - List eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); - Block block = target as Block; System.Type currentType = null; if (block._EventHandler != null) @@ -667,8 +682,7 @@ namespace Fungus.EditorUtils GenericMenu commandMenu = new GenericMenu(); // Build menu list - List menuTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); - List> filteredAttributes = GetFilteredCommandInfoAttribute(menuTypes); + List> filteredAttributes = GetFilteredCommandInfoAttribute(commandTypes); filteredAttributes.Sort( CompareCommandAttributes ); diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index 110ea949..1840308b 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -133,6 +133,7 @@ namespace Fungus.EditorUtils protected Flowchart flowchart; protected Block[] blocks; protected Block dragBlock; + protected static FungusState fungusState; [MenuItem("Tools/Fungus/Flowchart Window")] static void Init() @@ -205,12 +206,15 @@ namespace Fungus.EditorUtils { // Using a temp hidden object to track the active Flowchart across // serialization / deserialization when playing the game in the editor. - FungusState fungusState = GameObject.FindObjectOfType(); if (fungusState == null) { - GameObject go = new GameObject("_FungusState"); - go.hideFlags = HideFlags.HideInHierarchy; - fungusState = go.AddComponent(); + fungusState = GameObject.FindObjectOfType(); + if (fungusState == null) + { + GameObject go = new GameObject("_FungusState"); + go.hideFlags = HideFlags.HideInHierarchy; + fungusState = go.AddComponent(); + } } if (Selection.activeGameObject != null) diff --git a/Assets/Fungus/Scripts/Editor/VariableEditor.cs b/Assets/Fungus/Scripts/Editor/VariableEditor.cs index 9b368fd7..588c78f0 100644 --- a/Assets/Fungus/Scripts/Editor/VariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableEditor.cs @@ -86,7 +86,7 @@ namespace Fungus.EditorUtils } } - Flowchart[] fsList = GameObject.FindObjectsOfType(); + List fsList = Flowchart.CachedFlowcharts; foreach (Flowchart fs in fsList) { if (fs == flowchart)