[unity] Fixed SkeletonGraphic related import error occurring occasionally on first import. Error message was usually 'Skeleton JSON file not set for SkeletonData asset'. Closes #1226.

This commit is contained in:
Harald Csaszar 2019-01-31 14:43:39 +01:00
parent 4e8365012e
commit 52245d55f0
2 changed files with 48 additions and 5 deletions

View File

@ -453,6 +453,9 @@ namespace Spine.Unity.Editor {
public static class DataReloadHandler {
internal static Dictionary<int, string> savedSkeletonDataAssetAtSKeletonGraphicID = new Dictionary<int, string>();
#if NEWPLAYMODECALLBACKS
internal static void OnPlaymodeStateChanged (PlayModeStateChange stateChange) {
#else
@ -462,6 +465,7 @@ namespace Spine.Unity.Editor {
}
public static void ReloadAllActiveSkeletonsEditMode () {
if (EditorApplication.isPaused) return;
if (EditorApplication.isPlaying) return;
if (EditorApplication.isCompiling) return;
@ -475,10 +479,20 @@ namespace Spine.Unity.Editor {
if (skeletonDataAsset != null) skeletonDataAssetsToReload.Add(skeletonDataAsset);
}
// Under some circumstances (e.g. on first import) SkeletonGraphic objects
// have their skeletonGraphic.skeletonDataAsset reference corrupted
// by the instance of the ScriptableObject being destroyed but still assigned.
// Here we save the skeletonGraphic.skeletonDataAsset asset path in order
// to restore it later.
var activeSkeletonGraphics = GameObject.FindObjectsOfType<SkeletonGraphic>();
foreach (var sg in activeSkeletonGraphics) {
var skeletonDataAsset = sg.skeletonDataAsset;
if (skeletonDataAsset != null) skeletonDataAssetsToReload.Add(skeletonDataAsset);
if (skeletonDataAsset != null) {
var assetPath = AssetDatabase.GetAssetPath(skeletonDataAsset);
var sgID = sg.GetInstanceID();
savedSkeletonDataAssetAtSKeletonGraphicID[sgID] = assetPath;
skeletonDataAssetsToReload.Add(skeletonDataAsset);
}
}
foreach (var sda in skeletonDataAssetsToReload) {
@ -780,6 +794,22 @@ namespace Spine.Unity.Editor {
#endif
}
// Any post processing of images
// Under some circumstances (e.g. on first import) SkeletonGraphic objects
// have their skeletonGraphic.skeletonDataAsset reference corrupted
// by the instance of the ScriptableObject being destroyed but still assigned.
// Here we restore broken skeletonGraphic.skeletonDataAsset references.
var skeletonGraphicObjects = Resources.FindObjectsOfTypeAll(typeof(SkeletonGraphic)) as SkeletonGraphic[];
foreach (var skeletonGraphic in skeletonGraphicObjects) {
if (skeletonGraphic.skeletonDataAsset == null) {
var skeletonGraphicID = skeletonGraphic.GetInstanceID();
if (DataReloadHandler.savedSkeletonDataAssetAtSKeletonGraphicID.ContainsKey(skeletonGraphicID)) {
string assetPath = DataReloadHandler.savedSkeletonDataAssetAtSKeletonGraphicID[skeletonGraphicID];
skeletonGraphic.skeletonDataAsset = (SkeletonDataAsset)AssetDatabase.LoadAssetAtPath<SkeletonDataAsset>(assetPath);
}
}
}
}
static void ReloadSkeletonData (string skeletonJSONPath) {

View File

@ -100,12 +100,16 @@ namespace Spine.Unity {
}
} else {
if (skeletonDataAsset != null)
// Under some circumstances (e.g. sometimes on the first import) OnValidate is called
// before SpineEditorUtilities.ImportSpineContent, causing an unnecessary exception.
// The (skeletonDataAsset.skeletonJSON != null) condition serves to prevent this exception.
if (skeletonDataAsset != null && skeletonDataAsset.skeletonJSON != null)
Initialize(true);
}
}
protected override void Reset () {
base.Reset();
if (material == null || material.shader != Shader.Find("Spine/SkeletonGraphic (Premultiply Alpha)"))
Debug.LogWarning("SkeletonGraphic works best with the SkeletonGraphic material.");
@ -154,8 +158,17 @@ namespace Spine.Unity {
}
protected override void Awake () {
base.Awake ();
if (!this.IsValid) {
#if UNITY_EDITOR
// workaround for special import case of open scene where OnValidate and Awake are
// called in wrong order, before setup of Spine assets.
if (!Application.isPlaying) {
if (this.skeletonDataAsset != null && this.skeletonDataAsset.skeletonJSON == null)
return;
}
#endif
Initialize(false);
Rebuild(CanvasUpdate.PreRender);
}