[unity] Drag n drop now uses RectTransform plane if selected (for UI) + minor fixes.

This commit is contained in:
pharan 2016-08-01 09:12:40 +08:00
parent bd7ad96bc6
commit 0434398b36
3 changed files with 84 additions and 69 deletions

View File

@ -285,45 +285,56 @@ namespace Spine.Unity.Editor {
if (references.Length == 1) { if (references.Length == 1) {
var skeletonDataAsset = references[0] as SkeletonDataAsset; var skeletonDataAsset = references[0] as SkeletonDataAsset;
if (skeletonDataAsset != null) { if (skeletonDataAsset != null) {
DragAndDrop.visualMode = DragAndDropVisualMode.Move;
var mousePos = current.mousePosition; var mousePos = current.mousePosition;
Handles.BeginGUI(); bool invalidSkeletonData = skeletonDataAsset.GetSkeletonData(true) == null;
GUI.Label(new Rect(mousePos + new Vector2(20f, 20f), new Vector2(400f, 20f)), new GUIContent(string.Format("Create Spine GameObject ({0})", skeletonDataAsset.skeletonJSON.name), SpineEditorUtilities.Icons.spine)); if (invalidSkeletonData) {
Handles.EndGUI(); DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
Handles.BeginGUI();
GUI.Label(new Rect(mousePos + new Vector2(20f, 20f), new Vector2(400f, 40f)), new GUIContent(string.Format("{0} is invalid.\nCannot create new Spine GameObject.", skeletonDataAsset.name), SpineEditorUtilities.Icons.warning));
Handles.EndGUI();
return;
} else {
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
Handles.BeginGUI();
GUI.Label(new Rect(mousePos + new Vector2(20f, 20f), new Vector2(400f, 20f)), new GUIContent(string.Format("Create Spine GameObject ({0})", skeletonDataAsset.skeletonJSON.name), SpineEditorUtilities.Icons.spine));
Handles.EndGUI();
if (current.type == EventType.DragPerform) { if (current.type == EventType.DragPerform) {
var spawnPoint = MousePointToWorldPoint2D(mousePos, sceneview.camera); RectTransform rectTransform = (Selection.activeGameObject == null) ? null : Selection.activeGameObject.GetComponent<RectTransform>();
Plane plane = (rectTransform == null) ? new Plane(Vector3.back, Vector3.zero) : new Plane(-rectTransform.forward, rectTransform.position);
Vector3 spawnPoint = MousePointToWorldPoint2D(mousePos, sceneview.camera, plane);
var menu = new GenericMenu(); var menu = new GenericMenu();
// JOHN: todo: Get rect space spawnPoint if RectTransform is selected. menu.AddItem(new GUIContent("SkeletonAnimation"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
menu.AddItem(new GUIContent("SkeletonAnimation"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint,
instantiateDelegate = (data) => InstantiateSkeletonAnimation(data)
});
foreach (var spawnType in additionalSpawnTypes) {
menu.AddItem(new GUIContent(spawnType.menuLabel), false, HandleSkeletonComponentDrop, new SpawnMenuData {
skeletonDataAsset = skeletonDataAsset, skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint, spawnPoint = spawnPoint,
instantiateDelegate = spawnType.instantiateDelegate, instantiateDelegate = (data) => InstantiateSkeletonAnimation(data)
isUI = spawnType.isUI
}); });
foreach (var spawnType in additionalSpawnTypes) {
menu.AddItem(new GUIContent(spawnType.menuLabel), false, HandleSkeletonComponentDrop, new SpawnMenuData {
skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint,
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();
} }
#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();
} }
} }
} }
@ -332,25 +343,32 @@ namespace Spine.Unity.Editor {
public static void HandleSkeletonComponentDrop (object menuData) { public static void HandleSkeletonComponentDrop (object menuData) {
var data = (SpawnMenuData)menuData; var data = (SpawnMenuData)menuData;
try { if (data.skeletonDataAsset.GetSkeletonData(true) == null) {
GameObject newGameObject = null; EditorUtility.DisplayDialog("Invalid SkeletonDataAsset", "Unable to create Spine GameObject.\n\nPlease check your SkeletonDataAsset.", "Ok");
Component newSkeletonComponent = data.instantiateDelegate.Invoke(data.skeletonDataAsset); return;
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.");
} }
bool isUI = data.isUI;
GameObject newGameObject = null;
Component newSkeletonComponent = data.instantiateDelegate.Invoke(data.skeletonDataAsset);
newGameObject = newSkeletonComponent.gameObject;
var transform = newGameObject.transform;
var activeGameObject = Selection.activeGameObject;
if (activeGameObject != null)
transform.SetParent(activeGameObject.transform, false);
newGameObject.transform.position = isUI ? data.spawnPoint : RoundVector(data.spawnPoint, 2);
if (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.");
if (!isUI && activeGameObject != null && activeGameObject.transform.localScale != Vector3.one)
Debug.Log("New Spine GameObject was parented to a scaled Transform. It may not be the intended size.");
Selection.activeGameObject = newGameObject;
Undo.RegisterCreatedObjectUndo(newGameObject, "Create Spine GameObject");
} }
/// <summary> /// <summary>
@ -363,12 +381,14 @@ namespace Spine.Unity.Editor {
return vector; return vector;
} }
static Vector3 MousePointToWorldPoint2D (Vector2 mousePosition, Camera camera) { /// <summary>
var plane2D = new Plane(Vector3.back, Vector3.zero); /// Converts a mouse point to a world point on a plane.
/// </summary>
static Vector3 MousePointToWorldPoint2D (Vector2 mousePosition, Camera camera, Plane plane) {
var screenPos = new Vector3(mousePosition.x, camera.pixelHeight - mousePosition.y, 0f); var screenPos = new Vector3(mousePosition.x, camera.pixelHeight - mousePosition.y, 0f);
var ray = camera.ScreenPointToRay(screenPos); var ray = camera.ScreenPointToRay(screenPos);
float distance; float distance;
bool hit = plane2D.Raycast(ray, out distance); bool hit = plane.Raycast(ray, out distance);
return ray.GetPoint(distance); return ray.GetPoint(distance);
} }
#endregion #endregion
@ -890,6 +910,7 @@ namespace Spine.Unity.Editor {
texImporter.spriteImportMode = SpriteImportMode.None; texImporter.spriteImportMode = SpriteImportMode.None;
texImporter.maxTextureSize = 2048; texImporter.maxTextureSize = 2048;
EditorUtility.SetDirty(texImporter); EditorUtility.SetDirty(texImporter);
AssetDatabase.ImportAsset(texturePath); AssetDatabase.ImportAsset(texturePath);
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();

View File

@ -52,6 +52,9 @@ namespace Spine.Unity.Editor {
static SpineEditorUtilities.InstantiateDelegate instantiateDelegate; static SpineEditorUtilities.InstantiateDelegate instantiateDelegate;
static SkeletonGraphicInspector () { static SkeletonGraphicInspector () {
if (!SpineEditorUtilities.initialized)
return;
if (instantiateDelegate == null) if (instantiateDelegate == null)
instantiateDelegate = new SpineEditorUtilities.InstantiateDelegate(SpawnSkeletonGraphicFromDrop); instantiateDelegate = new SpineEditorUtilities.InstantiateDelegate(SpawnSkeletonGraphicFromDrop);
@ -158,9 +161,8 @@ namespace Spine.Unity.Editor {
var parentGameObject = Selection.activeObject as GameObject; var parentGameObject = Selection.activeObject as GameObject;
var parentTransform = parentGameObject == null ? null : parentGameObject.GetComponent<RectTransform>(); var parentTransform = parentGameObject == null ? null : parentGameObject.GetComponent<RectTransform>();
if (parentTransform == null) { if (parentTransform == null)
Debug.LogWarning("Your new SkeletonGraphic will not be visible until it is placed under a Canvas"); Debug.LogWarning("Your new SkeletonGraphic will not be visible until it is placed under a Canvas");
}
var gameObject = NewSkeletonGraphicGameObject("New SkeletonGraphic"); var gameObject = NewSkeletonGraphicGameObject("New SkeletonGraphic");
gameObject.transform.SetParent(parentTransform, false); gameObject.transform.SetParent(parentTransform, false);

View File

@ -39,7 +39,7 @@ using Spine;
namespace Spine.Unity { namespace Spine.Unity {
[ExecuteInEditMode, RequireComponent(typeof(CanvasRenderer), typeof(RectTransform)), DisallowMultipleComponent] [ExecuteInEditMode, RequireComponent(typeof(CanvasRenderer), typeof(RectTransform)), DisallowMultipleComponent]
[AddComponentMenu("Spine/SkeletonGraphic (Unity UI Canvas)")] [AddComponentMenu("Spine/SkeletonGraphic (Unity UI Canvas)")]
public class SkeletonGraphic : MaskableGraphic, ISkeletonComponent, IAnimationStateComponent { public class SkeletonGraphic : MaskableGraphic, ISkeletonComponent, IAnimationStateComponent, ISkeletonAnimation {
#region Inspector #region Inspector
public SkeletonDataAsset skeletonDataAsset; public SkeletonDataAsset skeletonDataAsset;
@ -67,15 +67,13 @@ namespace Spine.Unity {
Clear(); Clear();
Initialize(true); Initialize(true);
startingAnimation = ""; startingAnimation = "";
if (skeletonDataAsset.atlasAssets.Length > 1 || skeletonDataAsset.atlasAssets[0].materials.Length > 1) { if (skeletonDataAsset.atlasAssets.Length > 1 || skeletonDataAsset.atlasAssets[0].materials.Length > 1)
Debug.LogError("Unity UI does not support multiple textures per Renderer. Your skeleton will not be rendered correctly. Recommend using SkeletonAnimation instead. This requires the use of a Screen space camera canvas."); Debug.LogError("Unity UI does not support multiple textures per Renderer. Your skeleton will not be rendered correctly. Recommend using SkeletonAnimation instead. This requires the use of a Screen space camera canvas.");
}
} else { } else {
if (freeze) return; if (freeze) return;
skeleton.SetToSetupPose(); skeleton.SetToSetupPose();
if (!string.IsNullOrEmpty(startingAnimation)) { if (!string.IsNullOrEmpty(startingAnimation))
skeleton.PoseWithAnimation(startingAnimation, 0f, false); skeleton.PoseWithAnimation(startingAnimation, 0f, false);
}
} }
} else { } else {
if (skeletonDataAsset != null) if (skeletonDataAsset != null)
@ -89,13 +87,8 @@ namespace Spine.Unity {
protected override void Reset () { protected override void Reset () {
base.Reset(); base.Reset();
if (canvas == null) { if (material == null || material.shader != Shader.Find("Spine/SkeletonGraphic (Premultiply Alpha)"))
Debug.LogWarningFormat("SkeletonGraphic requires a Canvas to be visible. Move this GameObject ({0}) in the Hierarchy so it becomes a child of a Canvas.", gameObject.name); Debug.LogWarning("SkeletonGraphic works best with the SkeletonGraphic material.");
}
if (material == null || material.shader != Shader.Find("Spine/SkeletonGraphic (Premultiply Alpha)")) {
Debug.LogWarning("SkeletonGraphic works best with the SkeletonGraphic material.");
}
} }
#endif #endif
#endregion #endregion
@ -149,7 +142,7 @@ namespace Spine.Unity {
if (UpdateComplete != null) UpdateComplete(this); if (UpdateComplete != null) UpdateComplete(this);
} }
void LateUpdate () { public void LateUpdate () {
if (freeze) return; if (freeze) return;
//this.SetVerticesDirty(); // Which is better? //this.SetVerticesDirty(); // Which is better?
UpdateMesh(); UpdateMesh();
@ -169,10 +162,9 @@ namespace Spine.Unity {
protected Spine.Unity.MeshGeneration.ISimpleMeshGenerator spineMeshGenerator; protected Spine.Unity.MeshGeneration.ISimpleMeshGenerator spineMeshGenerator;
public Spine.Unity.MeshGeneration.ISimpleMeshGenerator SpineMeshGenerator { get { return this.spineMeshGenerator; } } public Spine.Unity.MeshGeneration.ISimpleMeshGenerator SpineMeshGenerator { get { return this.spineMeshGenerator; } }
public delegate void UpdateDelegate (SkeletonGraphic skeletonGraphic); public event UpdateBonesDelegate UpdateLocal;
public event UpdateDelegate UpdateLocal; public event UpdateBonesDelegate UpdateWorld;
public event UpdateDelegate UpdateWorld; public event UpdateBonesDelegate UpdateComplete;
public event UpdateDelegate UpdateComplete;
public void Clear () { public void Clear () {
skeleton = null; skeleton = null;