[unity] BoneFollower.SetBoneByName.

This commit is contained in:
pharan 2017-06-22 11:24:22 +08:00
parent 0f3786d869
commit f9a4107e41
3 changed files with 20 additions and 8 deletions

View File

@ -47,9 +47,9 @@ namespace Spine.Unity {
} }
} }
/// <summary>If a bone isn't set in code, boneName is used to find the bone.</summary> /// <summary>If a bone isn't set in code, boneName is used to find the bone at the beginning. For runtime switching by name, use SetBoneByName. You can also set the BoneFollower.bone field directly.</summary>
[SpineBone(dataField: "skeletonRenderer")] [SpineBone(dataField: "skeletonRenderer")]
public String boneName; [SerializeField] public string boneName;
public bool followZPosition = true; public bool followZPosition = true;
public bool followBoneRotation = true; public bool followBoneRotation = true;
@ -65,10 +65,25 @@ namespace Spine.Unity {
#endregion #endregion
[NonSerialized] public bool valid; [NonSerialized] public bool valid;
/// <summary>
/// The bone.
/// </summary>
[NonSerialized] public Bone bone; [NonSerialized] public Bone bone;
Transform skeletonTransform; Transform skeletonTransform;
bool skeletonTransformIsParent; bool skeletonTransformIsParent;
/// <summary>
/// Sets the target bone by its bone name. Returns false if no bone was found.</summary>
public bool SetBoneByName (string name) {
bone = skeletonRenderer.skeleton.FindBone(name);
if (bone == null) {
Debug.LogError("Bone not found: " + name, this);
return false;
}
boneName = name;
return true;
}
public void Awake () { public void Awake () {
if (initializeOnAwake) Initialize(); if (initializeOnAwake) Initialize();
} }
@ -115,10 +130,7 @@ namespace Spine.Unity {
if (bone == null) { if (bone == null) {
if (string.IsNullOrEmpty(boneName)) return; if (string.IsNullOrEmpty(boneName)) return;
bone = skeletonRenderer.skeleton.FindBone(boneName); bone = skeletonRenderer.skeleton.FindBone(boneName);
if (bone == null) { if (!SetBoneByName(boneName)) return;
Debug.LogError("Bone not found: " + boneName, this);
return;
}
} }
Transform thisTransform = this.transform; Transform thisTransform = this.transform;

View File

@ -95,7 +95,7 @@ namespace Spine.Unity.Editor {
var transform = skeletonRendererComponent.transform; var transform = skeletonRendererComponent.transform;
var skeleton = skeletonRendererComponent.skeleton; var skeleton = skeletonRendererComponent.skeleton;
if (string.IsNullOrEmpty(tbf.boneName)) { if (string.IsNullOrEmpty(boneName.stringValue)) {
SpineHandles.DrawBones(transform, skeleton); SpineHandles.DrawBones(transform, skeleton);
SpineHandles.DrawBoneNames(transform, skeleton); SpineHandles.DrawBoneNames(transform, skeleton);
Handles.Label(tbf.transform.position, "No bone selected", EditorStyles.helpBox); Handles.Label(tbf.transform.position, "No bone selected", EditorStyles.helpBox);

View File

@ -156,7 +156,7 @@ namespace Spine.Unity.Editor {
if (Event.current.type == EventType.Repaint) { if (Event.current.type == EventType.Repaint) {
if (addBoneFollower) { if (addBoneFollower) {
var boneFollower = follower.gameObject.AddComponent<BoneFollower>(); var boneFollower = follower.gameObject.AddComponent<BoneFollower>();
boneFollower.boneName = follower.Slot.Data.BoneData.Name; boneFollower.SetBoneByName(follower.Slot.Data.BoneData.Name);
addBoneFollower = false; addBoneFollower = false;
} }