mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-22 10:16:01 +08:00
[unity] Drag n drop to Scene View context menu for all types.
This commit is contained in:
parent
870bda40b7
commit
395dc5b345
@ -259,7 +259,23 @@ namespace Spine.Unity.Editor {
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Drag and Drop to Scene View
|
#region Drag and Drop to Scene View
|
||||||
private static System.Type EditorSpawnType = typeof(SkeletonAnimation);
|
|
||||||
|
public delegate Component InstantiateDelegate (SkeletonDataAsset skeletonDataAsset);
|
||||||
|
|
||||||
|
struct SpawnMenuData {
|
||||||
|
public Vector3 spawnPoint;
|
||||||
|
public SkeletonDataAsset skeletonDataAsset;
|
||||||
|
public InstantiateDelegate instantiateDelegate;
|
||||||
|
public bool isUI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SkeletonComponentSpawnType {
|
||||||
|
public string menuLabel;
|
||||||
|
public InstantiateDelegate instantiateDelegate;
|
||||||
|
public bool isUI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly List<SkeletonComponentSpawnType> additionalSpawnTypes = new List<SkeletonComponentSpawnType>();
|
||||||
|
|
||||||
static void OnSceneGUI (SceneView sceneview) {
|
static void OnSceneGUI (SceneView sceneview) {
|
||||||
var current = UnityEngine.Event.current;
|
var current = UnityEngine.Event.current;
|
||||||
@ -277,31 +293,66 @@ namespace Spine.Unity.Editor {
|
|||||||
Handles.EndGUI();
|
Handles.EndGUI();
|
||||||
|
|
||||||
if (current.type == EventType.DragPerform) {
|
if (current.type == EventType.DragPerform) {
|
||||||
var spawnPoint = MousePointToWorldPoint2D(mousePos, sceneview.camera);
|
var spawnPoint = MousePointToWorldPoint2D(mousePos, sceneview.camera);
|
||||||
try {
|
|
||||||
GameObject newGameObject = null;
|
|
||||||
|
|
||||||
if (EditorSpawnType == typeof(SkeletonAnimation)) {
|
var menu = new GenericMenu();
|
||||||
var newSkeletonAnimation = InstantiateSkeletonAnimation(skeletonDataAsset);
|
// JOHN: todo: Get rect space spawnPoint if RectTransform is selected.
|
||||||
newGameObject = newSkeletonAnimation.gameObject;
|
menu.AddItem(new GUIContent("SkeletonAnimation"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
|
||||||
}
|
skeletonDataAsset = skeletonDataAsset,
|
||||||
// else if (EditorSpawnType == typeof(SkeletonAnimator)) {
|
spawnPoint = spawnPoint,
|
||||||
// var newSkeletonAnimator = InstantiateSkeletonAnimator(skeletonDataAsset);
|
instantiateDelegate = (data) => InstantiateSkeletonAnimation(data)
|
||||||
// newGameObject = newSkeletonAnimator.gameObject;
|
});
|
||||||
// }
|
|
||||||
|
|
||||||
newGameObject.transform.position = RoundVector(spawnPoint, 2);
|
foreach (var spawnType in additionalSpawnTypes) {
|
||||||
Selection.activeGameObject = newGameObject;
|
menu.AddItem(new GUIContent(spawnType.menuLabel), false, HandleSkeletonComponentDrop, new SpawnMenuData {
|
||||||
Undo.RegisterCreatedObjectUndo(newGameObject, "Spawn Skeleton as SkeletonAnimation");
|
skeletonDataAsset = skeletonDataAsset,
|
||||||
} catch {
|
spawnPoint = spawnPoint,
|
||||||
Debug.LogError("Could not create Spine GameObject. Make sure your SkeletonData asset is valid.");
|
instantiateDelegate = spawnType.instantiateDelegate,
|
||||||
|
isUI = spawnType.isUI
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SPINE_SKELETONANIMATOR
|
||||||
|
menu.AddSeparator("");
|
||||||
|
|
||||||
|
menu.AddItem(new GUIContent("SkeletonAnimator"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
|
||||||
|
skeletonDataAsset = skeletonDataAsset,
|
||||||
|
spawnPoint = spawnPoint,
|
||||||
|
instantiateDelegate = (data) => InstantiateSkeletonAnimator(data)
|
||||||
|
});
|
||||||
|
#endif
|
||||||
|
|
||||||
|
menu.ShowAsContext();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void HandleSkeletonComponentDrop (object menuData) {
|
||||||
|
var data = (SpawnMenuData)menuData;
|
||||||
|
|
||||||
|
try {
|
||||||
|
GameObject newGameObject = null;
|
||||||
|
Component newSkeletonComponent = data.instantiateDelegate.Invoke(data.skeletonDataAsset);
|
||||||
|
newGameObject = newSkeletonComponent.gameObject;
|
||||||
|
newGameObject.transform.position = RoundVector(data.spawnPoint, 2);
|
||||||
|
|
||||||
|
var activeGameObject = Selection.activeGameObject;
|
||||||
|
if (activeGameObject != null)
|
||||||
|
newGameObject.transform.SetParent(activeGameObject.transform, true);
|
||||||
|
|
||||||
|
if (data.isUI && (activeGameObject == null || activeGameObject.GetComponent<RectTransform>() == null))
|
||||||
|
Debug.Log("Created a UI Skeleton GameObject not under a RectTransform. It may not be visible until you parent it to a canvas.");
|
||||||
|
|
||||||
|
Selection.activeGameObject = newGameObject;
|
||||||
|
Undo.RegisterCreatedObjectUndo(newGameObject, "Spawn Spine Skeleton");
|
||||||
|
} catch {
|
||||||
|
Debug.LogError("Could not create Spine GameObject. Make sure your SkeletonData asset is valid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rounds off vector components to a number of decimal digits.
|
/// Rounds off vector components to a number of decimal digits.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -1223,6 +1274,7 @@ namespace Spine.Unity.Editor {
|
|||||||
|
|
||||||
if (skeletonDataAsset.controller == null) {
|
if (skeletonDataAsset.controller == null) {
|
||||||
SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset);
|
SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset);
|
||||||
|
Debug.Log(string.Format("Mecanim controller was automatically generated and assigned for {0}", skeletonDataAsset.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
go.GetComponent<Animator>().runtimeAnimatorController = skeletonDataAsset.controller;
|
go.GetComponent<Animator>().runtimeAnimatorController = skeletonDataAsset.controller;
|
||||||
|
|||||||
@ -37,7 +37,8 @@ using UnityEditor;
|
|||||||
using Spine;
|
using Spine;
|
||||||
|
|
||||||
namespace Spine.Unity.Editor {
|
namespace Spine.Unity.Editor {
|
||||||
|
|
||||||
|
[InitializeOnLoad]
|
||||||
[CustomEditor(typeof(SkeletonGraphic))]
|
[CustomEditor(typeof(SkeletonGraphic))]
|
||||||
public class SkeletonGraphicInspector : UnityEditor.Editor {
|
public class SkeletonGraphicInspector : UnityEditor.Editor {
|
||||||
SerializedProperty material_, color_;
|
SerializedProperty material_, color_;
|
||||||
@ -48,6 +49,37 @@ namespace Spine.Unity.Editor {
|
|||||||
|
|
||||||
SkeletonGraphic thisSkeletonGraphic;
|
SkeletonGraphic thisSkeletonGraphic;
|
||||||
|
|
||||||
|
static SpineEditorUtilities.InstantiateDelegate instantiateDelegate;
|
||||||
|
|
||||||
|
static SkeletonGraphicInspector () {
|
||||||
|
if (instantiateDelegate == null)
|
||||||
|
instantiateDelegate = new SpineEditorUtilities.InstantiateDelegate(SpawnSkeletonGraphicFromDrop);
|
||||||
|
|
||||||
|
// Drag and Drop Instantiate menu item
|
||||||
|
var spawnTypes = SpineEditorUtilities.additionalSpawnTypes;
|
||||||
|
UnityEngine.Assertions.Assert.IsFalse(spawnTypes == null);
|
||||||
|
bool menuItemExists = false;
|
||||||
|
foreach (var spawnType in spawnTypes) {
|
||||||
|
if (spawnType.instantiateDelegate == SkeletonGraphicInspector.instantiateDelegate) {
|
||||||
|
menuItemExists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!menuItemExists) {
|
||||||
|
SpineEditorUtilities.additionalSpawnTypes.Add(new SpineEditorUtilities.SkeletonComponentSpawnType {
|
||||||
|
menuLabel = "SkeletonGraphic (UI)",
|
||||||
|
instantiateDelegate = SkeletonGraphicInspector.instantiateDelegate,
|
||||||
|
isUI = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static public Component SpawnSkeletonGraphicFromDrop (SkeletonDataAsset data) {
|
||||||
|
return InstantiateSkeletonGraphic(data);
|
||||||
|
}
|
||||||
|
|
||||||
void OnEnable () {
|
void OnEnable () {
|
||||||
var so = this.serializedObject;
|
var so = this.serializedObject;
|
||||||
thisSkeletonGraphic = target as SkeletonGraphic;
|
thisSkeletonGraphic = target as SkeletonGraphic;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user