From 0003e88698efbd7449d64afb5c2ecbca9fe7953c Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 14 Jan 2019 16:36:27 +0100 Subject: [PATCH] [unity] Fixed errors upon "Reimport All" and related import problems, closes #1248. --- .../Editor/SpineEditorUtilities.cs | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/SpineEditorUtilities.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/SpineEditorUtilities.cs index 0e2bf107a..787a1ba76 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/SpineEditorUtilities.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/SpineEditorUtilities.cs @@ -512,9 +512,16 @@ namespace Spine.Unity.Editor { return requiredPaths; } - TextAsset spineJson = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonDataPath, typeof(TextAsset)); - - StringReader reader = new StringReader(spineJson.text); + TextReader reader = null; + TextAsset spineJson = AssetDatabase.LoadAssetAtPath(skeletonDataPath); + if (spineJson != null) { + reader = new StringReader(spineJson.text); + } + else { + // On a "Reimport All" the order of imports can be wrong, thus LoadAssetAtPath() above could return null. + // as a workaround, we provide a fallback reader. + reader = new StreamReader(skeletonDataPath); + } var root = Json.Deserialize(reader) as Dictionary; if (!root.ContainsKey("skins")) @@ -557,8 +564,16 @@ namespace Spine.Unity.Editor { internal static void AddRequiredAtlasRegionsFromBinary (string skeletonDataPath, List requiredPaths) { SkeletonBinary binary = new SkeletonBinary(new AtlasRequirementLoader(requiredPaths)); - TextAsset data = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonDataPath, typeof(TextAsset)); - MemoryStream input = new MemoryStream(data.bytes); + Stream input = null; + TextAsset data = AssetDatabase.LoadAssetAtPath(skeletonDataPath); + if (data != null) { + // On a "Reimport All" the order of imports can be wrong, thus LoadAssetAtPath() above could return null. + // as a workaround, we provide a fallback reader. + input = File.Open(skeletonDataPath, FileMode.Open); + } + else { + input = new MemoryStream(data.bytes); + } binary.ReadSkeletonData(input); binary = null; } @@ -642,13 +657,13 @@ namespace Spine.Unity.Editor { imagePaths.Add(str); break; case ".json": - var jsonAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset)); + var jsonAsset = AssetDatabase.LoadAssetAtPath(str); if (jsonAsset != null && IsSpineData(jsonAsset)) skeletonPaths.Add(str); break; case ".bytes": if (str.ToLower().EndsWith(".skel.bytes", System.StringComparison.Ordinal)) { - if (IsSpineData((TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset)))) + if (IsSpineData(AssetDatabase.LoadAssetAtPath(str))) skeletonPaths.Add(str); } break; @@ -658,7 +673,7 @@ namespace Spine.Unity.Editor { // Import atlases first. var atlases = new List(); foreach (string ap in atlasPaths) { - TextAsset atlasText = (TextAsset)AssetDatabase.LoadAssetAtPath(ap, typeof(TextAsset)); + TextAsset atlasText = AssetDatabase.LoadAssetAtPath(ap); AtlasAssetBase atlas = IngestSpineAtlas(atlasText); atlases.Add(atlas); } @@ -674,13 +689,13 @@ namespace Spine.Unity.Editor { string dir = Path.GetDirectoryName(skeletonPath); #if SPINE_TK2D - IngestSpineProject(AssetDatabase.LoadAssetAtPath(sp, typeof(TextAsset)) as TextAsset, null); + IngestSpineProject(AssetDatabase.LoadAssetAtPath(sp), null); #else var localAtlases = FindAtlasesAtPath(dir); var requiredPaths = GetRequiredAtlasRegions(skeletonPath); var atlasMatch = GetMatchingAtlas(requiredPaths, localAtlases); if (atlasMatch != null || requiredPaths.Count == 0) { - IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath, typeof(TextAsset)) as TextAsset, atlasMatch); + IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath), atlasMatch); } else { SkeletonImportDialog(skeletonPath, localAtlases, requiredPaths, ref abortSkeletonImport); } @@ -694,7 +709,7 @@ namespace Spine.Unity.Editor { static void ReloadSkeletonData (string skeletonJSONPath) { string dir = Path.GetDirectoryName(skeletonJSONPath); - TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonJSONPath, typeof(TextAsset)); + TextAsset textAsset = AssetDatabase.LoadAssetAtPath(skeletonJSONPath); DirectoryInfo dirInfo = new DirectoryInfo(dir); FileInfo[] files = dirInfo.GetFiles("*.asset"); @@ -960,7 +975,7 @@ namespace Spine.Unity.Editor { #region Spine Skeleton Data File Validation public static bool CheckForValidSkeletonData (string skeletonJSONPath) { string dir = Path.GetDirectoryName(skeletonJSONPath); - TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonJSONPath, typeof(TextAsset)); + TextAsset textAsset = AssetDatabase.LoadAssetAtPath(skeletonJSONPath); DirectoryInfo dirInfo = new DirectoryInfo(dir); FileInfo[] files = dirInfo.GetFiles("*.asset"); @@ -1068,20 +1083,20 @@ namespace Spine.Unity.Editor { var atlasMatch = AssetUtility.GetMatchingAtlas(requiredPaths, localAtlases); if (atlasMatch != null) { resolved = true; - AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath, typeof(TextAsset)) as TextAsset, atlasMatch); + AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath), atlasMatch); } } break; case 0: // Resolve AtlasAssets... var atlasList = MultiAtlasDialog(requiredPaths, Path.GetDirectoryName(skeletonPath), Path.GetFileNameWithoutExtension(skeletonPath)); if (atlasList != null) - AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath, typeof(TextAsset)) as TextAsset, atlasList.ToArray()); + AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath), atlasList.ToArray()); resolved = true; break; case 1: // Import without atlas Debug.LogWarning("Imported with missing atlases. Skeleton will not render: " + Path.GetFileName(skeletonPath)); - AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath, typeof(TextAsset)) as TextAsset, new AtlasAssetBase[] { }); + AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath(skeletonPath), new AtlasAssetBase[] { }); resolved = true; break; case 2: // Stop importing all