PlayMaker Updates & Downloads > Share New Actions

Swap Single Sprite

(1/6) > >>

collidernyc:
Very simple Sprite switch. Useful for home made buttons, swapping that sword for the rail gun, dressing your macho hero up in that brand new skull helmet with the cute pink bow etc....

Make sure you have a 2D Sprite Renderer attached to your object. Choose your new sprite.

Besides the simplicity, this differs from Jeans awesome Play Sprites Animation by the fact that you can call it from a touch began/touch ended and it always calls finished()  - you can click/touch quickly and always get to the next state. Jean's needs to finish the animation before finished() - not good for quick touches (your finger might have already moved before the animation is done so touch ended never gets called). There's probably a way to interrupt this in the Play Sprites Animation code, but I like having the stripped down simplicity of this one.


--- Code: ---// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
//--- __ECO__ __ACTION__

using UnityEngine;
using System.Collections;


namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Animation)]
[Tooltip("Replaces a single Sprite on any GameObject. Object must have a Sprite Renderer.")]
public class SwapSingleSprite : FsmStateAction
{

[RequiredField]
[CheckForComponent(typeof(SpriteRenderer))]
public FsmOwnerDefault gameObject;

public Sprite spriteToSwap;

private SpriteRenderer spriteRenderer;

public override void Reset()
{
gameObject = null;

spriteToSwap = new Sprite();
}

public override void OnEnter()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

spriteRenderer = go.GetComponent<SpriteRenderer>();

if (spriteRenderer == null)
{
LogError("SwapSingleSprite: Missing SpriteRenderer!");
return;
}

SwapSprites();

Finish();
}

void SwapSprites()
{
spriteRenderer.sprite = spriteToSwap;
}

}
}
--- End code ---

jeanfabre:
Hi,

 That makes perfect sense. Thanks. I have added it to ecosystem. You already spotted the commenting "//--- __ECO__ __ACTION__" which is key for the ecosystem to find this action, thanks :)

I modified the class name to "SwapSprite", I don't think "single is adding to the comprehension of what it does, and I added a "ResetOnExit" option so that you can easily toggle without reference the original sprite by just leaving the state, very useful for user interactions. I also declared a "Sprite" category, as it seems more appropriate than using the Animation category.

 Bye,

 Jean

btw: Your showreel is really impressive!

collidernyc:
Thanks Jean - all good changes. I had thought about making a Sprite category, but didn't know if it was cool to just add new ones like that - thanks!

My reel is ancient by now, but thanks. I've been so busy the last few years I haven't updated it. And now when I have a bit of free time I'd rather make a game than work on my website or reel... Which is too bad, as we've been doing some pretty cool projects :)

TheBadFeeling:
This action is just what I needed. Thanks a lot!  :)

nrhyde:
Please provide more information about how to use this script. 

How do I add this script to playmaker?  I used Create Script, pasted the script text in and saved it, and dragged it to one of my gameobjects.  In the inspector there's an error message under the script component: The associated script cannot be loaded. Please fix any compile errors and assign a valid script.

How do I add this script to an FSM?  I assume I would use Send Message, but what message name should I use? SwapSingleSprite?  How do I specify the new sprite I want to use? With a variable of type texture?

Navigation

[0] Message Index

[#] Next page

Go to full version