[unity] Fix [SpineAttachment] not fully using dataField to find ISkeletonComponent. Minor fixes.

This commit is contained in:
pharan 2016-07-31 18:17:46 +08:00
parent 11a6717db2
commit b36d1e9e02

View File

@ -33,13 +33,12 @@ namespace Spine.Unity.Editor {
return; return;
} }
var dataProperty = property.serializedObject.FindProperty(TargetAttribute.dataField); var dataField = property.serializedObject.FindProperty(TargetAttribute.dataField);
if (dataField != null) {
if (dataProperty != null) { if (dataField.objectReferenceValue is SkeletonDataAsset) {
if (dataProperty.objectReferenceValue is SkeletonDataAsset) { skeletonDataAsset = (SkeletonDataAsset)dataField.objectReferenceValue;
skeletonDataAsset = (SkeletonDataAsset)dataProperty.objectReferenceValue; } else if (dataField.objectReferenceValue is ISkeletonComponent) {
} else if (dataProperty.objectReferenceValue is ISkeletonComponent) { var skeletonComponent = (ISkeletonComponent)dataField.objectReferenceValue;
var skeletonComponent = (ISkeletonComponent)dataProperty.objectReferenceValue;
if (skeletonComponent != null) if (skeletonComponent != null)
skeletonDataAsset = skeletonComponent.SkeletonDataAsset; skeletonDataAsset = skeletonComponent.SkeletonDataAsset;
} else { } else {
@ -49,7 +48,7 @@ namespace Spine.Unity.Editor {
} else if (property.serializedObject.targetObject is Component) { } else if (property.serializedObject.targetObject is Component) {
var component = (Component)property.serializedObject.targetObject; var component = (Component)property.serializedObject.targetObject;
ISkeletonComponent skeletonComponent = component.GetComponentInChildren(typeof(ISkeletonComponent)) as ISkeletonComponent; var skeletonComponent = component.GetComponentInChildren(typeof(ISkeletonComponent)) as ISkeletonComponent;
if (skeletonComponent != null) if (skeletonComponent != null)
skeletonDataAsset = skeletonComponent.SkeletonDataAsset; skeletonDataAsset = skeletonComponent.SkeletonDataAsset;
} }
@ -62,28 +61,42 @@ namespace Spine.Unity.Editor {
position = EditorGUI.PrefixLabel(position, label); position = EditorGUI.PrefixLabel(position, label);
var propertyStringValue = property.stringValue; var propertyStringValue = property.stringValue;
if (GUI.Button(position, string.IsNullOrEmpty(propertyStringValue) ? NoneLabel : propertyStringValue, EditorStyles.popup)) { if (GUI.Button(position, string.IsNullOrEmpty(propertyStringValue) ? NoneLabel : propertyStringValue, EditorStyles.popup))
Selector(property); Selector(property);
} }
public ISkeletonComponent GetTargetSkeletonComponent (SerializedProperty property) {
var dataField = property.serializedObject.FindProperty(TargetAttribute.dataField);
if (dataField != null) {
var skeletonComponent = dataField.objectReferenceValue as ISkeletonComponent;
if (dataField.objectReferenceValue != null && skeletonComponent != null) // note the overloaded UnityEngine.Object == null check. Do not simplify.
return skeletonComponent;
} else {
var component = property.serializedObject.targetObject as Component;
if (component != null)
return component.GetComponentInChildren(typeof(ISkeletonComponent)) as ISkeletonComponent;
}
return null;
} }
protected virtual void Selector (SerializedProperty property) { protected virtual void Selector (SerializedProperty property) {
SkeletonData data = skeletonDataAsset.GetSkeletonData(true); SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
if (data == null) if (data == null) return;
return;
GenericMenu menu = new GenericMenu(); var menu = new GenericMenu();
PopulateMenu(menu, property, this.TargetAttribute, data); PopulateMenu(menu, property, this.TargetAttribute, data);
menu.ShowAsContext(); menu.ShowAsContext();
} }
protected abstract void PopulateMenu (GenericMenu menu, SerializedProperty property, T targetAttribute, SkeletonData data); protected abstract void PopulateMenu (GenericMenu menu, SerializedProperty property, T targetAttribute, SkeletonData data);
protected virtual void HandleSelect (object val) { protected virtual void HandleSelect (object menuItemObject) {
var pair = (SpineDrawerValuePair)val; var clickedItem = (SpineDrawerValuePair)menuItemObject;
pair.property.stringValue = pair.str; clickedItem.property.stringValue = clickedItem.str;
pair.property.serializedObject.ApplyModifiedProperties(); clickedItem.property.serializedObject.ApplyModifiedProperties();
} }
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) { public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
@ -99,14 +112,12 @@ namespace Spine.Unity.Editor {
for (int i = 0; i < data.Slots.Count; i++) { for (int i = 0; i < data.Slots.Count; i++) {
string name = data.Slots.Items[i].Name; string name = data.Slots.Items[i].Name;
if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) { if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) {
if (targetAttribute.containsBoundingBoxes) { if (targetAttribute.containsBoundingBoxes) {
int slotIndex = i; int slotIndex = i;
var attachments = new List<Attachment>();
List<Attachment> attachments = new List<Attachment>(); foreach (var skin in data.Skins)
foreach (var skin in data.Skins) {
skin.FindAttachmentsForSlot(slotIndex, attachments); skin.FindAttachmentsForSlot(slotIndex, attachments);
}
bool hasBoundingBox = false; bool hasBoundingBox = false;
foreach (var attachment in attachments) { foreach (var attachment in attachments) {
@ -120,7 +131,6 @@ namespace Spine.Unity.Editor {
if (!hasBoundingBox) if (!hasBoundingBox)
menu.AddDisabledItem(new GUIContent(name)); menu.AddDisabledItem(new GUIContent(name));
} else { } else {
menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
@ -169,6 +179,10 @@ namespace Spine.Unity.Editor {
public class SpineEventNameDrawer : SpineTreeItemDrawerBase<SpineEvent> { public class SpineEventNameDrawer : SpineTreeItemDrawerBase<SpineEvent> {
protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineEvent targetAttribute, SkeletonData data) { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineEvent targetAttribute, SkeletonData data) {
var events = skeletonDataAsset.GetSkeletonData(false).Events; var events = skeletonDataAsset.GetSkeletonData(false).Events;
// <None> item
menu.AddItem(new GUIContent(NoneLabel), string.IsNullOrEmpty(property.stringValue), HandleSelect, new SpineDrawerValuePair("", property));
for (int i = 0; i < events.Count; i++) { for (int i = 0; i < events.Count; i++) {
string name = events.Items[i].Name; string name = events.Items[i].Name;
if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal))
@ -181,45 +195,32 @@ namespace Spine.Unity.Editor {
[CustomPropertyDrawer(typeof(SpineAttachment))] [CustomPropertyDrawer(typeof(SpineAttachment))]
public class SpineAttachmentDrawer : SpineTreeItemDrawerBase<SpineAttachment> { public class SpineAttachmentDrawer : SpineTreeItemDrawerBase<SpineAttachment> {
protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAttachment targetAttribute, SkeletonData data) { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAttachment targetAttribute, SkeletonData data) {
List<Skin> validSkins = new List<Skin>(); ISkeletonComponent skeletonComponent = GetTargetSkeletonComponent(property);
SkeletonRenderer skeletonRenderer = null; var validSkins = new List<Skin>();
var component = property.serializedObject.targetObject as Component; if (skeletonComponent != null && targetAttribute.currentSkinOnly) {
if (component != null) { var currentSkin = skeletonComponent.Skeleton.Skin;
if (component.GetComponentInChildren<SkeletonRenderer>() != null) { if (currentSkin != null)
skeletonRenderer = component.GetComponentInChildren<SkeletonRenderer>(); validSkins.Add(currentSkin);
//if (skeletonDataAsset != skeletonRenderer.skeletonDataAsset) Debug.LogWarning("DataField SkeletonDataAsset and SkeletonRenderer/SkeletonAnimation's SkeletonDataAsset do not match. Remove the explicit dataField parameter of your [SpineAttachment] field."); else
skeletonDataAsset = skeletonRenderer.skeletonDataAsset;
}
}
if (skeletonRenderer != null && targetAttribute.currentSkinOnly) {
if (skeletonRenderer.skeleton.Skin != null) {
validSkins.Add(skeletonRenderer.skeleton.Skin);
} else {
validSkins.Add(data.Skins.Items[0]); validSkins.Add(data.Skins.Items[0]);
}
} else { } else {
foreach (Skin skin in data.Skins) { foreach (Skin skin in data.Skins)
if (skin != null) if (skin != null) validSkins.Add(skin);
validSkins.Add(skin);
}
} }
List<string> attachmentNames = new List<string>(); var attachmentNames = new List<string>();
List<string> placeholderNames = new List<string>(); var placeholderNames = new List<string>();
string prefix = ""; string prefix = "";
if (skeletonRenderer != null && targetAttribute.currentSkinOnly) if (skeletonComponent != null && targetAttribute.currentSkinOnly)
menu.AddDisabledItem(new GUIContent(skeletonRenderer.gameObject.name + " (SkeletonRenderer)")); menu.AddDisabledItem(new GUIContent((skeletonComponent as Component).gameObject.name + " (Skeleton)"));
else else
menu.AddDisabledItem(new GUIContent(skeletonDataAsset.name)); menu.AddDisabledItem(new GUIContent(skeletonDataAsset.name));
menu.AddSeparator(""); menu.AddSeparator("");
menu.AddItem(new GUIContent("Null"), property.stringValue == "", HandleSelect, new SpineDrawerValuePair("", property)); menu.AddItem(new GUIContent("Null"), property.stringValue == "", HandleSelect, new SpineDrawerValuePair("", property));
menu.AddSeparator(""); menu.AddSeparator("");
Skin defaultSkin = data.Skins.Items[0]; Skin defaultSkin = data.Skins.Items[0];
@ -239,7 +240,7 @@ namespace Spine.Unity.Editor {
prefix = skinPrefix; prefix = skinPrefix;
for (int i = 0; i < data.Slots.Count; i++) { for (int i = 0; i < data.Slots.Count; i++) {
if (slotMatch.Length > 0 && data.Slots.Items[i].Name.ToLower().Contains(slotMatch) == false) if (slotMatch.Length > 0 && !(data.Slots.Items[i].Name.ToLower().Contains(slotMatch)))
continue; continue;
attachmentNames.Clear(); attachmentNames.Clear();
@ -251,9 +252,7 @@ namespace Spine.Unity.Editor {
skin.FindNamesForSlot(i, placeholderNames); skin.FindNamesForSlot(i, placeholderNames);
} }
for (int a = 0; a < attachmentNames.Count; a++) { for (int a = 0; a < attachmentNames.Count; a++) {
string attachmentPath = attachmentNames[a]; string attachmentPath = attachmentNames[a];
string menuPath = prefix + data.Slots.Items[i].Name + "/" + attachmentPath; string menuPath = prefix + data.Slots.Items[i].Name + "/" + attachmentPath;
string name = attachmentNames[a]; string name = attachmentNames[a];
@ -261,14 +260,13 @@ namespace Spine.Unity.Editor {
if (targetAttribute.returnAttachmentPath) if (targetAttribute.returnAttachmentPath)
name = skin.Name + "/" + data.Slots.Items[i].Name + "/" + attachmentPath; name = skin.Name + "/" + data.Slots.Items[i].Name + "/" + attachmentPath;
if (targetAttribute.placeholdersOnly && placeholderNames.Contains(attachmentPath) == false) { if (targetAttribute.placeholdersOnly && !placeholderNames.Contains(attachmentPath)) {
menu.AddDisabledItem(new GUIContent(menuPath)); menu.AddDisabledItem(new GUIContent(menuPath));
} else { } else {
menu.AddItem(new GUIContent(menuPath), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); menu.AddItem(new GUIContent(menuPath), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
} }
} }
} }
} }
@ -284,7 +282,7 @@ namespace Spine.Unity.Editor {
for (int i = 0; i < data.Bones.Count; i++) { for (int i = 0; i < data.Bones.Count; i++) {
string name = data.Bones.Items[i].Name; string name = data.Bones.Items[i].Name;
if (name.StartsWith(targetAttribute.startsWith)) if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal))
menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
} }
@ -303,12 +301,7 @@ namespace Spine.Unity.Editor {
} }
component = (Component)property.serializedObject.targetObject; component = (Component)property.serializedObject.targetObject;
atlasProp = component != null ? property.serializedObject.FindProperty("atlasAsset") : null;
if (component != null)
atlasProp = property.serializedObject.FindProperty("atlasAsset");
else
atlasProp = null;
if (atlasProp == null) { if (atlasProp == null) {
EditorGUI.LabelField(position, "ERROR:", "Must have AtlasAsset variable!"); EditorGUI.LabelField(position, "ERROR:", "Must have AtlasAsset variable!");
@ -322,9 +315,8 @@ namespace Spine.Unity.Editor {
position = EditorGUI.PrefixLabel(position, label); position = EditorGUI.PrefixLabel(position, label);
if (GUI.Button(position, property.stringValue, EditorStyles.popup)) { if (GUI.Button(position, property.stringValue, EditorStyles.popup))
Selector(property); Selector(property);
}
} }
@ -332,7 +324,7 @@ namespace Spine.Unity.Editor {
GenericMenu menu = new GenericMenu(); GenericMenu menu = new GenericMenu();
AtlasAsset atlasAsset = (AtlasAsset)atlasProp.objectReferenceValue; AtlasAsset atlasAsset = (AtlasAsset)atlasProp.objectReferenceValue;
Atlas atlas = atlasAsset.GetAtlas(); Atlas atlas = atlasAsset.GetAtlas();
FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
List<AtlasRegion> regions = (List<AtlasRegion>)field.GetValue(atlas); List<AtlasRegion> regions = (List<AtlasRegion>)field.GetValue(atlas);
for (int i = 0; i < regions.Count; i++) { for (int i = 0; i < regions.Count; i++) {
@ -340,7 +332,6 @@ namespace Spine.Unity.Editor {
menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
menu.ShowAsContext(); menu.ShowAsContext();
} }