From f9a4107e41009bdbdcf7d03f15356165a1da980b Mon Sep 17 00:00:00 2001 From: pharan Date: Thu, 22 Jun 2017 11:24:22 +0800 Subject: [PATCH] [unity] BoneFollower.SetBoneByName. --- .../Assets/spine-unity/BoneFollower.cs | 24 ++++++++++++++----- .../Editor/BoneFollowerInspector.cs | 2 +- .../Editor/BoundingBoxFollowerInspector.cs | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/spine-unity/Assets/spine-unity/BoneFollower.cs b/spine-unity/Assets/spine-unity/BoneFollower.cs index 2b88a12c2..c9c7746c9 100644 --- a/spine-unity/Assets/spine-unity/BoneFollower.cs +++ b/spine-unity/Assets/spine-unity/BoneFollower.cs @@ -47,9 +47,9 @@ namespace Spine.Unity { } } - /// If a bone isn't set in code, boneName is used to find the bone. + /// 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. [SpineBone(dataField: "skeletonRenderer")] - public String boneName; + [SerializeField] public string boneName; public bool followZPosition = true; public bool followBoneRotation = true; @@ -65,10 +65,25 @@ namespace Spine.Unity { #endregion [NonSerialized] public bool valid; + /// + /// The bone. + /// [NonSerialized] public Bone bone; Transform skeletonTransform; bool skeletonTransformIsParent; + /// + /// Sets the target bone by its bone name. Returns false if no bone was found. + 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 () { if (initializeOnAwake) Initialize(); } @@ -115,10 +130,7 @@ namespace Spine.Unity { if (bone == null) { if (string.IsNullOrEmpty(boneName)) return; bone = skeletonRenderer.skeleton.FindBone(boneName); - if (bone == null) { - Debug.LogError("Bone not found: " + boneName, this); - return; - } + if (!SetBoneByName(boneName)) return; } Transform thisTransform = this.transform; diff --git a/spine-unity/Assets/spine-unity/Editor/BoneFollowerInspector.cs b/spine-unity/Assets/spine-unity/Editor/BoneFollowerInspector.cs index 494f5ebbb..7372c5b17 100644 --- a/spine-unity/Assets/spine-unity/Editor/BoneFollowerInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/BoneFollowerInspector.cs @@ -95,7 +95,7 @@ namespace Spine.Unity.Editor { var transform = skeletonRendererComponent.transform; var skeleton = skeletonRendererComponent.skeleton; - if (string.IsNullOrEmpty(tbf.boneName)) { + if (string.IsNullOrEmpty(boneName.stringValue)) { SpineHandles.DrawBones(transform, skeleton); SpineHandles.DrawBoneNames(transform, skeleton); Handles.Label(tbf.transform.position, "No bone selected", EditorStyles.helpBox); diff --git a/spine-unity/Assets/spine-unity/Modules/BoundingBoxFollower/Editor/BoundingBoxFollowerInspector.cs b/spine-unity/Assets/spine-unity/Modules/BoundingBoxFollower/Editor/BoundingBoxFollowerInspector.cs index 2974d3a9e..f00f3cb3c 100644 --- a/spine-unity/Assets/spine-unity/Modules/BoundingBoxFollower/Editor/BoundingBoxFollowerInspector.cs +++ b/spine-unity/Assets/spine-unity/Modules/BoundingBoxFollower/Editor/BoundingBoxFollowerInspector.cs @@ -156,7 +156,7 @@ namespace Spine.Unity.Editor { if (Event.current.type == EventType.Repaint) { if (addBoneFollower) { var boneFollower = follower.gameObject.AddComponent(); - boneFollower.boneName = follower.Slot.Data.BoneData.Name; + boneFollower.SetBoneByName(follower.Slot.Data.BoneData.Name); addBoneFollower = false; }