[Unity] SkeletonAnimation.AddToGameObject() method for instantiation at runtime.

This commit is contained in:
pharan 2016-01-26 07:29:40 +08:00
parent b8ccd5cbda
commit 6e6e5a9438
2 changed files with 31 additions and 31 deletions

View File

@ -104,39 +104,25 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
public bool loop; public bool loop;
/// <summary> /// <summary>
/// The rate at which animations progress over time. 1 means 100%. 0.5 means 50%. /// The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.</summary>
/// AnimationState and TrackEntry also have their own timeScale. These are combined multiplicatively.</summary> /// <remarks>AnimationState and TrackEntry also have their own timeScale. These are combined multiplicatively.</remarks>
#if UNITY_5 #if UNITY_5
[Tooltip("The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.")] [Tooltip("The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.")]
#endif #endif
public float timeScale = 1; public float timeScale = 1;
#region AutoReset #region Runtime Instantiation
/** /// <summary>Adds and prepares a SkeletonAnimation component to a GameObject at runtime.</summary>
[Tooltip("Setting this to true makes the SkeletonAnimation behave similar to Spine editor. New animations will not inherit the pose from a previous animation. If you need to intermittently and programmatically pose your skeleton, leave this false.")] /// <returns>The newly instantiated SkeletonAnimation</returns>
[SerializeField] public static SkeletonAnimation AddToGameObject (GameObject gameObject, SkeletonDataAsset skeletonDataAsset) {
protected bool autoReset = false; return SkeletonRenderer.AddSpineComponent<SkeletonAnimation>(gameObject, skeletonDataAsset);
/// <summary>
/// Setting this to true makes the SkeletonAnimation behave similar to Spine editor.
/// New animations will not inherit the pose from a previous animation.
/// If you need to intermittently and programmatically pose your skeleton, leave this false.</summary>
public bool AutoReset {
get { return this.autoReset; }
set {
if (!autoReset && value) {
state.Start -= HandleNewAnimationAutoreset; // make sure there isn't a double-subscription.
state.Start += HandleNewAnimationAutoreset;
}
autoReset = value;
}
} }
protected virtual void HandleNewAnimationAutoreset (Spine.AnimationState state, int trackIndex) { /// <summary>Instantiates a new UnityEngine.GameObject and adds a prepared SkeletonAnimation component to it.</summary>
if (!autoReset) return; /// <returns>The newly instantiated SkeletonAnimation component.</returns>
if (skeleton != null) skeleton.SetToSetupPose(); public static SkeletonAnimation NewSkeletonAnimationGameObject (SkeletonDataAsset skeletonDataAsset) {
return SkeletonRenderer.NewSpineGameObject<SkeletonAnimation>(skeletonDataAsset);
} }
*/
#endregion #endregion
public override void Reset () { public override void Reset () {
@ -146,12 +132,6 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData()); state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
/*
if (autoReset) {
state.Start += HandleNewAnimationAutoreset;
}
*/
if (_animationName != null && _animationName.Length > 0) { if (_animationName != null && _animationName.Length > 0) {
state.SetAnimation(0, _animationName, loop); state.SetAnimation(0, _animationName, loop);
Update(0); Update(0);
@ -185,4 +165,5 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
_UpdateComplete(this); _UpdateComplete(this);
} }
} }
} }

View File

@ -76,6 +76,25 @@ public class SkeletonRenderer : MonoBehaviour {
private readonly ExposedList<Submesh> submeshes = new ExposedList<Submesh>(); private readonly ExposedList<Submesh> submeshes = new ExposedList<Submesh>();
private SkeletonUtilitySubmeshRenderer[] submeshRenderers; private SkeletonUtilitySubmeshRenderer[] submeshRenderers;
#region Runtime Instantiation
/// <summary>Add and prepare a Spine component that derives from SkeletonRenderer to a GameObject at runtime.</summary>
/// <typeparam name="T">T should be SkeletonRenderer or any of its derived classes.</typeparam>
public static T AddSpineComponent<T> (GameObject gameObject, SkeletonDataAsset skeletonDataAsset) where T : SkeletonRenderer {
var c = gameObject.AddComponent<T>();
if (skeletonDataAsset != null) {
c.skeletonDataAsset = skeletonDataAsset;
c.Reset(); // Method signature will change.
}
return c;
}
public static T NewSpineGameObject<T> (SkeletonDataAsset skeletonDataAsset) where T : SkeletonRenderer {
return SkeletonRenderer.AddSpineComponent<T>(new GameObject("New Spine GameObject"), skeletonDataAsset);
}
#endregion
public virtual void Awake () { public virtual void Awake () {
Reset(); Reset();
} }