mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-10 00:58:43 +08:00
[Unity] SkeletonAnimation.AddToGameObject() method for instantiation at runtime.
This commit is contained in:
parent
b8ccd5cbda
commit
6e6e5a9438
@ -104,39 +104,25 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
|
||||
public bool loop;
|
||||
|
||||
/// <summary>
|
||||
/// The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.
|
||||
/// AnimationState and TrackEntry also have their own timeScale. These are combined multiplicatively.</summary>
|
||||
/// The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.</summary>
|
||||
/// <remarks>AnimationState and TrackEntry also have their own timeScale. These are combined multiplicatively.</remarks>
|
||||
#if UNITY_5
|
||||
[Tooltip("The rate at which animations progress over time. 1 means 100%. 0.5 means 50%.")]
|
||||
#endif
|
||||
public float timeScale = 1;
|
||||
|
||||
#region AutoReset
|
||||
/**
|
||||
[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.")]
|
||||
[SerializeField]
|
||||
protected bool autoReset = false;
|
||||
|
||||
/// <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;
|
||||
}
|
||||
#region Runtime Instantiation
|
||||
/// <summary>Adds and prepares a SkeletonAnimation component to a GameObject at runtime.</summary>
|
||||
/// <returns>The newly instantiated SkeletonAnimation</returns>
|
||||
public static SkeletonAnimation AddToGameObject (GameObject gameObject, SkeletonDataAsset skeletonDataAsset) {
|
||||
return SkeletonRenderer.AddSpineComponent<SkeletonAnimation>(gameObject, skeletonDataAsset);
|
||||
}
|
||||
|
||||
protected virtual void HandleNewAnimationAutoreset (Spine.AnimationState state, int trackIndex) {
|
||||
if (!autoReset) return;
|
||||
if (skeleton != null) skeleton.SetToSetupPose();
|
||||
/// <summary>Instantiates a new UnityEngine.GameObject and adds a prepared SkeletonAnimation component to it.</summary>
|
||||
/// <returns>The newly instantiated SkeletonAnimation component.</returns>
|
||||
public static SkeletonAnimation NewSkeletonAnimationGameObject (SkeletonDataAsset skeletonDataAsset) {
|
||||
return SkeletonRenderer.NewSpineGameObject<SkeletonAnimation>(skeletonDataAsset);
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
|
||||
public override void Reset () {
|
||||
@ -146,12 +132,6 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
|
||||
|
||||
state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
|
||||
|
||||
/*
|
||||
if (autoReset) {
|
||||
state.Start += HandleNewAnimationAutoreset;
|
||||
}
|
||||
*/
|
||||
|
||||
if (_animationName != null && _animationName.Length > 0) {
|
||||
state.SetAnimation(0, _animationName, loop);
|
||||
Update(0);
|
||||
@ -185,4 +165,5 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
|
||||
_UpdateComplete(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -76,6 +76,25 @@ public class SkeletonRenderer : MonoBehaviour {
|
||||
private readonly ExposedList<Submesh> submeshes = new ExposedList<Submesh>();
|
||||
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 () {
|
||||
Reset();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user