I've been working on this lately, try this action:
Tk2dPlayAnimationExtended.cs
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.
using System.Collections;
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("2D Toolkit/SpriteAnimator")]
[Tooltip("Plays a sprite animation. \nNOTE: The Game Object must have a tk2dSpriteAnimator attached.")]
public class Tk2dPlayAnimationExtended : FsmStateAction
{
[RequiredField]
[Tooltip("The Game Object to work with. NOTE: The Game Object must have a tk2dSpriteAnimator component attached.")]
[CheckForComponent(typeof(tk2dSpriteAnimator))]
public FsmOwnerDefault gameObject;
[Tooltip("Wait until the animation is finished. If the action is entered again before this animation has ended, it will not interrupt the previous one.")]
public FsmBool waitForEnd;
[Tooltip("Plays the animation that was originally playing before entering this action.")]
public FsmBool leaveDefaultAnim;
public int clipId;
public tk2dSpriteAnimation library;
private tk2dSpriteAnimator _sprite;
private tk2dSpriteAnimationClip _clip;
private void _getSprite()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
_sprite = go.GetComponent<tk2dSpriteAnimator>();
}
private void _getClip()
{
_clip = library.GetClipById(clipId);
}
public override void Reset()
{
gameObject = null;
waitForEnd = null;
leaveDefaultAnim = null;
clipId = 0;
library = null;
_sprite = null;
_clip = null;
}
public override void OnEnter()
{
_getSprite();
_getClip();
DoPlayAnimation();
if (!waitForEnd.Value)
Finish();
}
public override void OnUpdate()
{
DoCheckPlaying();
}
void DoPlayAnimation()
{
if (_sprite == null)
{
LogWarning("Missing tk2dSpriteAnimator component");
Finish();
return;
}
if (_sprite.Library != library)
_sprite.Library = library;
else if (_sprite.IsPlaying(_clip) && !waitForEnd.Value)
_sprite.StopAndResetFrame();
_sprite.Play(_clip);
if (leaveDefaultAnim.Value)
_sprite.AnimationCompleted = (sprite, clip) => sprite.Play(sprite.DefaultClip);
return;
}
void DoCheckPlaying()
{
if (!_sprite.IsPlaying(_clip))
Finish();
return;
}
}
}
And the editor, Tk2dPlayAnimationExtendedEditor.cs:
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMakerEditor;
using System.Collections.Generic;
[CustomActionEditor(typeof(Tk2dPlayAnimationExtended))]
public class Tk2dPlayAnimationExtendedEditor : CustomActionEditor
{
Tk2dPlayAnimationExtended action;
string[] animLibNames = null;
tk2dGenericIndexItem[] animLibs = null;
GameObject _owner = null;
// Get the owner
GameObject GetOwner()
{
GameObject owner = null;
if (action.Fsm != null)
owner = action.Fsm.GetOwnerDefaultTarget(action.gameObject);
return owner;
}
// Get the sprite of the go
tk2dSpriteAnimator GetSprite(GameObject go)
{
tk2dSpriteAnimator sprite = null;
if (go != null)
sprite = go.GetComponent<tk2dSpriteAnimator>();
return sprite;
}
// Assign to the action a default lib an clip
void SetDefaultLibrary()
{
action.library = animLibs[0].GetAsset<tk2dSpriteAnimation>();
action.clipId = action.library.GetClipIdByName(action.library.clips[0].name);
}
void SetSpritesLibrary(tk2dSpriteAnimator sprite)
{
action.library = sprite.Library;
action.clipId = sprite.DefaultClipId;
}
// Init
public override void OnEnable()
{
// For referencing the action directly:
action = target as Tk2dPlayAnimationExtended;
animLibs = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations();
if (animLibs != null)
{
animLibNames = new string[animLibs.Length];
for (int i = 0; i < animLibs.Length; ++i)
{
animLibNames[i] = animLibs[i].AssetName;
}
}
//if (action.library == null)
// SetDefaultLibrary();
}
public override bool OnGUI()
{
// Default inspector for gameObject
EditField("gameObject");
// Check for Owner change
var currOwner = GetOwner();
if (currOwner != _owner && currOwner != null)
{
// If new owner and the new is replacing an old one, import the animation
if (_owner != null)
{
var sprite = GetSprite(currOwner);
if (sprite != null)
{
action.library = sprite.Library;
action.clipId = sprite.DefaultClipId;
}
}
_owner = currOwner;
}
// Check for library
if (action.library == null)
SetDefaultLibrary();
#region Library
// Get selected library
int selAnimLib = 0;
string selectedGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(action.library));
for (int i = 0; i < animLibs.Length; ++i)
{
if (animLibs[i].assetGUID == selectedGUID)
{
selAnimLib = i;
break;
}
}
// Display libraries
int newAnimLib = EditorGUILayout.Popup("Anim Lib", selAnimLib, animLibNames);
if (newAnimLib != selAnimLib)
{
action.library = animLibs[newAnimLib].GetAsset<tk2dSpriteAnimation>();
action.clipId = 0;
}
#endregion
#region Clip
List<string> clipNames = new List<string>(action.library.clips.Length);
List<int> clipIds = new List<int>(action.library.clips.Length);
// Fill names (with ids if necessary)
for (int i = 0; i < action.library.clips.Length; ++i)
{
if (action.library.clips[i].name != null && action.library.clips[i].name.Length > 0)
{
string name = action.library.clips[i].name;
if (tk2dPreferences.inst.showIds) {
name += "\t[" + i.ToString() + "]";
}
clipNames.Add( name );
clipIds.Add( i );
}
}
// Show options
int newClipId = EditorGUILayout.IntPopup("Clip", action.clipId, clipNames.ToArray(), clipIds.ToArray());
if (newClipId != action.clipId)
{
action.clipId = newClipId;
}
#endregion
// Default inspector for other fields
EditField("waitForEnd");
EditField("leaveDefaultAnim");
return GUI.changed;
}
}