[unity] Fixed SkeletonData asset preview constantly loading when editing a prefab. Closes #1545.

This commit is contained in:
Harald Csaszar 2019-12-16 17:12:10 +01:00
parent f0e4a1ebf0
commit 3f9d7b1c63
9 changed files with 68 additions and 59 deletions

View File

@ -175,7 +175,7 @@ namespace Spine.Unity.Editor {
// Header // Header
EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(target.name + " (SkeletonDataAsset)", Icons.spine), EditorStyles.whiteLargeLabel); EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(target.name + " (SkeletonDataAsset)", Icons.spine), EditorStyles.whiteLargeLabel);
if (targetSkeletonData != null) EditorGUILayout.LabelField("(Drag and Drop to instantiate.)", EditorStyles.miniLabel); if (targetSkeletonData != null) EditorGUILayout.LabelField("(Drag and Drop to instantiate.)", EditorStyles.miniLabel);
// Main Serialized Fields // Main Serialized Fields
using (var changeCheck = new EditorGUI.ChangeCheckScope()) { using (var changeCheck = new EditorGUI.ChangeCheckScope()) {
using (new SpineInspectorUtility.BoxScope()) using (new SpineInspectorUtility.BoxScope())
@ -201,11 +201,11 @@ namespace Spine.Unity.Editor {
} }
} }
} }
// Unity Quirk: Some code depends on valid preview. If preview is initialized elsewhere, this can cause contents to change between Layout and Repaint events, causing GUILayout control count errors. // Unity Quirk: Some code depends on valid preview. If preview is initialized elsewhere, this can cause contents to change between Layout and Repaint events, causing GUILayout control count errors.
if (NoProblems()) if (NoProblems())
preview.Initialize(this.Repaint, targetSkeletonDataAsset, this.LastSkinName); preview.Initialize(this.Repaint, targetSkeletonDataAsset, this.LastSkinName);
if (targetSkeletonData != null) { if (targetSkeletonData != null) {
GUILayout.Space(20f); GUILayout.Space(20f);
@ -597,7 +597,7 @@ namespace Spine.Unity.Editor {
warnings.Add("Missing Skeleton JSON"); warnings.Add("Missing Skeleton JSON");
} else { } else {
var fieldValue = (TextAsset)skeletonJSON.objectReferenceValue; var fieldValue = (TextAsset)skeletonJSON.objectReferenceValue;
if (!AssetUtility.IsSpineData(fieldValue, out compatibilityProblemInfo)) { if (!AssetUtility.IsSpineData(fieldValue, out compatibilityProblemInfo)) {
warnings.Add("Skeleton data file is not a valid Spine JSON or binary file."); warnings.Add("Skeleton data file is not a valid Spine JSON or binary file.");
} else { } else {
@ -807,13 +807,13 @@ namespace Spine.Unity.Editor {
return; return;
} }
const int PreviewLayer = 30;
const int PreviewCameraCullingMask = 1 << PreviewLayer;
if (previewRenderUtility == null) { if (previewRenderUtility == null) {
previewRenderUtility = new PreviewRenderUtility(true); previewRenderUtility = new PreviewRenderUtility(true);
animationLastTime = CurrentTime; animationLastTime = CurrentTime;
const int PreviewLayer = 30;
const int PreviewCameraCullingMask = 1 << PreviewLayer;
{ {
var c = this.PreviewUtilityCamera; var c = this.PreviewUtilityCamera;
c.orthographic = true; c.orthographic = true;
@ -825,32 +825,32 @@ namespace Spine.Unity.Editor {
} }
DestroyPreviewGameObject(); DestroyPreviewGameObject();
}
if (previewGameObject == null) { if (previewGameObject == null) {
try { try {
previewGameObject = EditorInstantiation.InstantiateSkeletonAnimation(skeletonDataAsset, skinName).gameObject; previewGameObject = EditorInstantiation.InstantiateSkeletonAnimation(skeletonDataAsset, skinName, useObjectFactory:false).gameObject;
if (previewGameObject != null) { if (previewGameObject != null) {
previewGameObject.hideFlags = HideFlags.HideAndDontSave; previewGameObject.hideFlags = HideFlags.HideAndDontSave;
previewGameObject.layer = PreviewLayer; previewGameObject.layer = PreviewLayer;
skeletonAnimation = previewGameObject.GetComponent<SkeletonAnimation>(); skeletonAnimation = previewGameObject.GetComponent<SkeletonAnimation>();
skeletonAnimation.initialSkinName = skinName; skeletonAnimation.initialSkinName = skinName;
skeletonAnimation.LateUpdate(); skeletonAnimation.LateUpdate();
previewGameObject.GetComponent<Renderer>().enabled = false; previewGameObject.GetComponent<Renderer>().enabled = false;
#if SPINE_UNITY_2018_PREVIEW_API #if SPINE_UNITY_2018_PREVIEW_API
previewRenderUtility.AddSingleGO(previewGameObject); previewRenderUtility.AddSingleGO(previewGameObject);
#endif #endif
}
if (this.ActiveTrack != null) cameraAdjustEndFrame = EditorApplication.timeSinceStartup + skeletonAnimation.AnimationState.GetCurrent(0).Alpha;
AdjustCameraGoals();
} catch {
DestroyPreviewGameObject();
} }
RefreshOnNextUpdate(); if (this.ActiveTrack != null) cameraAdjustEndFrame = EditorApplication.timeSinceStartup + skeletonAnimation.AnimationState.GetCurrent(0).Alpha;
AdjustCameraGoals();
} catch {
DestroyPreviewGameObject();
} }
RefreshOnNextUpdate();
} }
} }

View File

@ -47,7 +47,7 @@ namespace Spine.Unity.Editor {
[MenuItem ("CONTEXT/SkeletonGraphic/Add BoneFollower GameObject")] [MenuItem ("CONTEXT/SkeletonGraphic/Add BoneFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) { static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonGraphic = cmd.context as SkeletonGraphic; var skeletonGraphic = cmd.context as SkeletonGraphic;
var go = EditorInstantiation.NewGameObject("BoneFollower", typeof(RectTransform)); var go = EditorInstantiation.NewGameObject("BoneFollower", true, typeof(RectTransform));
var t = go.transform; var t = go.transform;
t.SetParent(skeletonGraphic.transform); t.SetParent(skeletonGraphic.transform);
t.localPosition = Vector3.zero; t.localPosition = Vector3.zero;

View File

@ -45,7 +45,7 @@ namespace Spine.Unity.Editor {
[MenuItem ("CONTEXT/SkeletonRenderer/Add BoneFollower GameObject")] [MenuItem ("CONTEXT/SkeletonRenderer/Add BoneFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) { static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer; var skeletonRenderer = cmd.context as SkeletonRenderer;
var go = EditorInstantiation.NewGameObject("New BoneFollower"); var go = EditorInstantiation.NewGameObject("New BoneFollower", true);
var t = go.transform; var t = go.transform;
t.SetParent(skeletonRenderer.transform); t.SetParent(skeletonRenderer.transform);
t.localPosition = Vector3.zero; t.localPosition = Vector3.zero;

View File

@ -191,7 +191,7 @@ namespace Spine.Unity.Editor {
#endregion #endregion
static GameObject AddBoundingBoxFollowerChild (SkeletonRenderer sr, BoundingBoxFollower original = null) { static GameObject AddBoundingBoxFollowerChild (SkeletonRenderer sr, BoundingBoxFollower original = null) {
var go = EditorInstantiation.NewGameObject("BoundingBoxFollower"); var go = EditorInstantiation.NewGameObject("BoundingBoxFollower", true);
go.transform.SetParent(sr.transform, false); go.transform.SetParent(sr.transform, false);
var newFollower = go.AddComponent<BoundingBoxFollower>(); var newFollower = go.AddComponent<BoundingBoxFollower>();

View File

@ -46,7 +46,7 @@ namespace Spine.Unity.Editor {
[MenuItem("CONTEXT/SkeletonRenderer/Add PointFollower GameObject")] [MenuItem("CONTEXT/SkeletonRenderer/Add PointFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) { static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer; var skeletonRenderer = cmd.context as SkeletonRenderer;
var go = EditorInstantiation.NewGameObject("PointFollower"); var go = EditorInstantiation.NewGameObject("PointFollower", true);
var t = go.transform; var t = go.transform;
t.SetParent(skeletonRenderer.transform); t.SetParent(skeletonRenderer.transform);
t.localPosition = Vector3.zero; t.localPosition = Vector3.zero;

View File

@ -205,7 +205,7 @@ namespace Spine.Unity.Editor {
} }
static GameObject NewSkeletonGraphicGameObject (string gameObjectName) { static GameObject NewSkeletonGraphicGameObject (string gameObjectName) {
var go = EditorInstantiation.NewGameObject(gameObjectName, typeof(RectTransform), typeof(CanvasRenderer), typeof(SkeletonGraphic)); var go = EditorInstantiation.NewGameObject(gameObjectName, true, typeof(RectTransform), typeof(CanvasRenderer), typeof(SkeletonGraphic));
var graphic = go.GetComponent<SkeletonGraphic>(); var graphic = go.GetComponent<SkeletonGraphic>();
graphic.material = SkeletonGraphicInspector.DefaultSkeletonGraphicMaterial; graphic.material = SkeletonGraphicInspector.DefaultSkeletonGraphicMaterial;
return go; return go;

View File

@ -36,12 +36,12 @@ namespace Spine.Unity.Editor {
public static class Menus { public static class Menus {
[MenuItem("GameObject/Spine/SkeletonRenderer", false, 10)] [MenuItem("GameObject/Spine/SkeletonRenderer", false, 10)]
static public void CreateSkeletonRendererGameObject () { static public void CreateSkeletonRendererGameObject () {
EditorInstantiation.InstantiateEmptySpineGameObject<SkeletonRenderer>("New SkeletonRenderer"); EditorInstantiation.InstantiateEmptySpineGameObject<SkeletonRenderer>("New SkeletonRenderer", true);
} }
[MenuItem("GameObject/Spine/SkeletonAnimation", false, 10)] [MenuItem("GameObject/Spine/SkeletonAnimation", false, 10)]
static public void CreateSkeletonAnimationGameObject () { static public void CreateSkeletonAnimationGameObject () {
EditorInstantiation.InstantiateEmptySpineGameObject<SkeletonAnimation>("New SkeletonAnimation"); EditorInstantiation.InstantiateEmptySpineGameObject<SkeletonAnimation>("New SkeletonAnimation", true);
} }
} }
} }

View File

@ -330,9 +330,11 @@ namespace Spine.Unity.Editor {
#endif #endif
} }
SkeletonDataAssetInspector[] skeletonDataInspectors = Resources.FindObjectsOfTypeAll<SkeletonDataAssetInspector>(); if (atlasPaths.Count > 0 || imagePaths.Count > 0 || skeletonPaths.Count > 0) {
foreach (var inspector in skeletonDataInspectors) { SkeletonDataAssetInspector[] skeletonDataInspectors = Resources.FindObjectsOfTypeAll<SkeletonDataAssetInspector>();
inspector.UpdateSkeletonData(); foreach (var inspector in skeletonDataInspectors) {
inspector.UpdateSkeletonData();
}
} }
// Any post processing of images // Any post processing of images
@ -905,13 +907,17 @@ namespace Spine.Unity.Editor {
} }
} }
public static SkeletonAnimation InstantiateSkeletonAnimation (SkeletonDataAsset skeletonDataAsset, string skinName, bool destroyInvalid = true) { public static SkeletonAnimation InstantiateSkeletonAnimation (SkeletonDataAsset skeletonDataAsset, string skinName,
bool destroyInvalid = true, bool useObjectFactory = true) {
var skeletonData = skeletonDataAsset.GetSkeletonData(true); var skeletonData = skeletonDataAsset.GetSkeletonData(true);
var skin = skeletonData != null ? skeletonData.FindSkin(skinName) : null; var skin = skeletonData != null ? skeletonData.FindSkin(skinName) : null;
return InstantiateSkeletonAnimation(skeletonDataAsset, skin, destroyInvalid); return InstantiateSkeletonAnimation(skeletonDataAsset, skin, destroyInvalid, useObjectFactory);
} }
public static SkeletonAnimation InstantiateSkeletonAnimation (SkeletonDataAsset skeletonDataAsset, Skin skin = null, bool destroyInvalid = true) { public static SkeletonAnimation InstantiateSkeletonAnimation (SkeletonDataAsset skeletonDataAsset, Skin skin = null,
bool destroyInvalid = true, bool useObjectFactory = true) {
SkeletonData data = skeletonDataAsset.GetSkeletonData(true); SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
if (data == null) { if (data == null) {
@ -928,7 +934,8 @@ namespace Spine.Unity.Editor {
} }
string spineGameObjectName = string.Format("Spine GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", "")); string spineGameObjectName = string.Format("Spine GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", ""));
GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation)); GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, useObjectFactory,
typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation));
SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>(); SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>();
newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset; newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset;
TryInitializeSkeletonRendererSettings(newSkeletonAnimation, skin); TryInitializeSkeletonRendererSettings(newSkeletonAnimation, skin);
@ -954,28 +961,28 @@ namespace Spine.Unity.Editor {
} }
/// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary> /// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary>
public static GameObject NewGameObject (string name) { public static GameObject NewGameObject (string name, bool useObjectFactory) {
#if NEW_PREFAB_SYSTEM #if NEW_PREFAB_SYSTEM
return ObjectFactory.CreateGameObject(name); if (useObjectFactory)
#else return ObjectFactory.CreateGameObject(name);
return new GameObject(name);
#endif #endif
return new GameObject(name);
} }
/// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary> /// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary>
public static GameObject NewGameObject (string name, params System.Type[] components) { public static GameObject NewGameObject (string name, bool useObjectFactory, params System.Type[] components) {
#if NEW_PREFAB_SYSTEM #if NEW_PREFAB_SYSTEM
return ObjectFactory.CreateGameObject(name, components); if (useObjectFactory)
#else return ObjectFactory.CreateGameObject(name, components);
return new GameObject(name, components);
#endif #endif
return new GameObject(name, components);
} }
public static void InstantiateEmptySpineGameObject<T> (string name) where T : MonoBehaviour { public static void InstantiateEmptySpineGameObject<T> (string name, bool useObjectFactory) where T : MonoBehaviour {
var parentGameObject = Selection.activeObject as GameObject; var parentGameObject = Selection.activeObject as GameObject;
var parentTransform = parentGameObject == null ? null : parentGameObject.transform; var parentTransform = parentGameObject == null ? null : parentGameObject.transform;
var gameObject = EditorInstantiation.NewGameObject(name, typeof(T)); var gameObject = EditorInstantiation.NewGameObject(name, useObjectFactory, typeof(T));
gameObject.transform.SetParent(parentTransform, false); gameObject.transform.SetParent(parentTransform, false);
EditorUtility.FocusProjectWindow(); EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject; Selection.activeObject = gameObject;
@ -988,7 +995,8 @@ namespace Spine.Unity.Editor {
return InstantiateSkeletonMecanim(skeletonDataAsset, skeletonDataAsset.GetSkeletonData(true).FindSkin(skinName)); return InstantiateSkeletonMecanim(skeletonDataAsset, skeletonDataAsset.GetSkeletonData(true).FindSkin(skinName));
} }
public static SkeletonMecanim InstantiateSkeletonMecanim (SkeletonDataAsset skeletonDataAsset, Skin skin = null, bool destroyInvalid = true) { public static SkeletonMecanim InstantiateSkeletonMecanim (SkeletonDataAsset skeletonDataAsset, Skin skin = null,
bool destroyInvalid = true, bool useObjectFactory = true) {
SkeletonData data = skeletonDataAsset.GetSkeletonData(true); SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
if (data == null) { if (data == null) {
@ -1005,7 +1013,8 @@ namespace Spine.Unity.Editor {
} }
string spineGameObjectName = string.Format("Spine Mecanim GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", "")); string spineGameObjectName = string.Format("Spine Mecanim GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", ""));
GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(Animator), typeof(SkeletonMecanim)); GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, useObjectFactory,
typeof(MeshFilter), typeof(MeshRenderer), typeof(Animator), typeof(SkeletonMecanim));
if (skeletonDataAsset.controller == null) { if (skeletonDataAsset.controller == null) {
SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset); SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset);

View File

@ -184,7 +184,7 @@ namespace Spine.Unity.Editor {
Debug.LogError("Could not export Spine Skeleton because SkeletonDataAsset is null or invalid!"); Debug.LogError("Could not export Spine Skeleton because SkeletonDataAsset is null or invalid!");
return; return;
} }
if (outputPath == "") { if (outputPath == "") {
outputPath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(skeletonDataAsset)).Replace('\\', '/') + "/Baked"; outputPath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(skeletonDataAsset)).Replace('\\', '/') + "/Baked";
System.IO.Directory.CreateDirectory(outputPath); System.IO.Directory.CreateDirectory(outputPath);
@ -299,7 +299,7 @@ namespace Spine.Unity.Editor {
} }
} }
GameObject prefabRoot = EditorInstantiation.NewGameObject("root"); GameObject prefabRoot = EditorInstantiation.NewGameObject("root", true);
Dictionary<string, Transform> slotTable = new Dictionary<string, Transform>(); Dictionary<string, Transform> slotTable = new Dictionary<string, Transform>();
Dictionary<string, Transform> boneTable = new Dictionary<string, Transform>(); Dictionary<string, Transform> boneTable = new Dictionary<string, Transform>();
@ -308,7 +308,7 @@ namespace Spine.Unity.Editor {
//create bones //create bones
for (int i = 0; i < skeletonData.Bones.Count; i++) { for (int i = 0; i < skeletonData.Bones.Count; i++) {
var boneData = skeletonData.Bones.Items[i]; var boneData = skeletonData.Bones.Items[i];
Transform boneTransform = EditorInstantiation.NewGameObject(boneData.Name).transform; Transform boneTransform = EditorInstantiation.NewGameObject(boneData.Name, true).transform;
boneTransform.parent = prefabRoot.transform; boneTransform.parent = prefabRoot.transform;
boneTable.Add(boneTransform.name, boneTransform); boneTable.Add(boneTransform.name, boneTransform);
boneList.Add(boneTransform); boneList.Add(boneTransform);
@ -339,7 +339,7 @@ namespace Spine.Unity.Editor {
//create slots and attachments //create slots and attachments
for (int slotIndex = 0; slotIndex < skeletonData.Slots.Count; slotIndex++) { for (int slotIndex = 0; slotIndex < skeletonData.Slots.Count; slotIndex++) {
var slotData = skeletonData.Slots.Items[slotIndex]; var slotData = skeletonData.Slots.Items[slotIndex];
Transform slotTransform = EditorInstantiation.NewGameObject(slotData.Name).transform; Transform slotTransform = EditorInstantiation.NewGameObject(slotData.Name, true).transform;
slotTransform.parent = prefabRoot.transform; slotTransform.parent = prefabRoot.transform;
slotTable.Add(slotData.Name, slotTransform); slotTable.Add(slotData.Name, slotTransform);
@ -389,7 +389,7 @@ namespace Spine.Unity.Editor {
} else } else
continue; continue;
Transform attachmentTransform = EditorInstantiation.NewGameObject(attachmentName).transform; Transform attachmentTransform = EditorInstantiation.NewGameObject(attachmentName, true).transform;
attachmentTransform.parent = slotTransform; attachmentTransform.parent = slotTransform;
attachmentTransform.localPosition = offset; attachmentTransform.localPosition = offset;
@ -1437,7 +1437,7 @@ namespace Spine.Unity.Editor {
Directory.CreateDirectory(bakedDirPath); Directory.CreateDirectory(bakedDirPath);
if (prefab == null) { if (prefab == null) {
root = EditorInstantiation.NewGameObject("temp", typeof(MeshFilter), typeof(MeshRenderer)); root = EditorInstantiation.NewGameObject("temp", true, typeof(MeshFilter), typeof(MeshRenderer));
#if NEW_PREFAB_SYSTEM #if NEW_PREFAB_SYSTEM
prefab = PrefabUtility.SaveAsPrefabAsset(root, bakedPrefabPath); prefab = PrefabUtility.SaveAsPrefabAsset(root, bakedPrefabPath);
#else #else