From d00be040e2fe0e5f88a700e93ba3b5eb2516f7fe Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 5 May 2022 17:34:21 +0200 Subject: [PATCH] [unity] Fixed null reference exception upon import when default shader is used but preferences has default shader set to a non-existing/null shader path. --- .../Editor/Utility/AssetUtility.cs | 19 ++++++++++--------- .../Editor/Windows/SpinePreferences.cs | 4 ++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs index a1836306f..acd8eaa67 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs @@ -575,22 +575,23 @@ namespace Spine.Unity.Editor { pageName = "Material"; string materialPath = assetPath + "/" + primaryName + "_" + pageName + ".mat"; - Material mat = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); + Material material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); - if (mat == null) { - mat = new Material(Shader.Find(SpineEditorUtilities.Preferences.defaultShader)); - ApplyPMAOrStraightAlphaSettings(mat, SpineEditorUtilities.Preferences.textureSettingsReference); - AssetDatabase.CreateAsset(mat, materialPath); + if (material == null) { + Shader defaultShader = Shader.Find(SpineEditorUtilities.Preferences.DefaultShader); + material = defaultShader != null ? new Material(defaultShader) : null; + ApplyPMAOrStraightAlphaSettings(material, SpineEditorUtilities.Preferences.textureSettingsReference); + AssetDatabase.CreateAsset(material, materialPath); } else { - vestigialMaterials.Remove(mat); + vestigialMaterials.Remove(material); } if (texture != null) - mat.mainTexture = texture; + material.mainTexture = texture; - EditorUtility.SetDirty(mat); + EditorUtility.SetDirty(material); // note: don't call AssetDatabase.SaveAssets() since this would trigger OnPostprocessAllAssets() every time unnecessarily. - populatingMaterials.Add(mat); //atlasAsset.materials[i] = mat; + populatingMaterials.Add(material); //atlasAsset.materials[i] = mat; } atlasAsset.materials = populatingMaterials.ToArray(); diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Windows/SpinePreferences.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Windows/SpinePreferences.cs index ccb55cbce..9fcfc4432 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Windows/SpinePreferences.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Windows/SpinePreferences.cs @@ -66,6 +66,10 @@ namespace Spine.Unity.Editor { internal const string DEFAULT_DEFAULT_SHADER = "Spine/Skeleton"; public string defaultShader = DEFAULT_DEFAULT_SHADER; + public string DefaultShader { + get { return !string.IsNullOrEmpty(defaultShader) ? defaultShader : DEFAULT_DEFAULT_SHADER; } + set { defaultShader = value; } + } internal const float DEFAULT_DEFAULT_ZSPACING = 0f; public float defaultZSpacing = DEFAULT_DEFAULT_ZSPACING;