From 8be8b378c26451375e0f903f4c8d6075eaabde62 Mon Sep 17 00:00:00 2001 From: Zach Vinless Date: Sat, 12 Nov 2016 14:08:58 -0800 Subject: [PATCH] Fixed compatibility with Unity 5.0 and 5.1 and handled reimporting resources asset --- .../Scripts/Editor/FungusEditorResources.cs | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/FungusEditorResources.cs b/Assets/Fungus/Scripts/Editor/FungusEditorResources.cs index 109c3dda..ca9c2c21 100644 --- a/Assets/Fungus/Scripts/Editor/FungusEditorResources.cs +++ b/Assets/Fungus/Scripts/Editor/FungusEditorResources.cs @@ -8,6 +8,9 @@ using System; using System.IO; using System.Linq; using System.Collections.Generic; +#if UNITY_5_0 || UNITY_5_1 +using System.Reflection; +#endif namespace Fungus.EditorUtils { @@ -32,6 +35,26 @@ namespace Fungus.EditorUtils } } + internal class EditorResourcesPostProcessor : AssetPostprocessor + { + private static void OnPostprocessAllAssets(string[] importedAssets, string[] _, string[] __, string[] ___) + { + foreach (var path in importedAssets) + { + if (path.EndsWith("FungusEditorResources.asset")) + { + var asset = AssetDatabase.LoadAssetAtPath(path, typeof(FungusEditorResources)) as FungusEditorResources; + if (asset != null) + { + FungusEditorResources.UpdateTextureReferences(asset); + AssetDatabase.SaveAssets(); + return; + } + } + } + } + } + internal partial class FungusEditorResources : ScriptableObject { [Serializable] @@ -68,8 +91,6 @@ namespace Fungus.EditorUtils { instance = ScriptableObject.CreateInstance(typeof(FungusEditorResources)) as FungusEditorResources; AssetDatabase.CreateAsset(instance, GetRootFolder() + "/FungusEditorResources.asset"); - UpdateTextureReferences(instance); - AssetDatabase.SaveAssets(); } else { @@ -153,9 +174,9 @@ namespace Fungus.EditorUtils } } - private static void UpdateTextureReferences(FungusEditorResources instance) + internal static void UpdateTextureReferences(FungusEditorResources instance) { - // Iterate through all fields in class and set texture references + // Iterate through all fields in instance and set texture references var serializedObject = new SerializedObject(instance); var prop = serializedObject.GetIterator(); var rootFolder = new [] { GetRootFolder() }; @@ -186,7 +207,15 @@ namespace Fungus.EditorUtils } instance.updateOnReloadScripts = false; + + // The ApplyModifiedPropertiesWithoutUndo() function wasn't documented until Unity 5.2 +#if UNITY_5_0 || UNITY_5_1 + var flags = BindingFlags.Instance | BindingFlags.NonPublic; + var applyMethod = typeof(SerializedObject).GetMethod("ApplyModifiedPropertiesWithoutUndo", flags); + applyMethod.Invoke(serializedObject, null); +#else serializedObject.ApplyModifiedPropertiesWithoutUndo(); +#endif } } }