浏览代码

[unity] BoneFollower.SetBoneByName.

pharan 8 年之前
父节点
当前提交
f9a4107e41

+ 18 - 6
spine-unity/Assets/spine-unity/BoneFollower.cs

@@ -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) {
-					Debug.LogError("Bone not found: " + boneName, this);
-					return;
-				}
+				if (!SetBoneByName(boneName)) return;
 			}
 			}
 
 
 			Transform thisTransform = this.transform;
 			Transform thisTransform = this.transform;

+ 1 - 1
spine-unity/Assets/spine-unity/Editor/BoneFollowerInspector.cs

@@ -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);

+ 1 - 1
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 (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;
 				}
 				}