playMaker

Author Topic: Swap Single Sprite  (Read 17613 times)

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Swap Single Sprite
« on: July 05, 2014, 06:25:05 PM »
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: [Select]
// (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;
}

}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Swap Single Sprite
« Reply #1 on: July 07, 2014, 11:26:14 AM »
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!
« Last Edit: July 07, 2014, 11:30:16 AM by jeanfabre »

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Swap Single Sprite
« Reply #2 on: July 07, 2014, 07:01:22 PM »
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

  • Junior Playmaker
  • **
  • Posts: 66
Re: Swap Single Sprite
« Reply #3 on: September 21, 2014, 09:30:17 AM »
This action is just what I needed. Thanks a lot!  :)
The Force is with you, young Playmaker – but you are not a C# senpai yet.

nrhyde

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Swap Single Sprite
« Reply #4 on: October 20, 2014, 04:46:40 AM »
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?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Swap Single Sprite
« Reply #5 on: October 20, 2014, 07:18:19 AM »
Please provide more information about how to use this script.

Download this, then drag it anywhere into your Project Hierarchy.

Add the action via the action browser per usual.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Swap Single Sprite
« Reply #6 on: October 20, 2014, 07:24:30 AM »
Hi,

 it's also on the EcoSystem browser which allow you to search fo custom actions and download them directly in your project in one click.

 Bye,

 Jean

nrhyde

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Swap Single Sprite
« Reply #7 on: October 20, 2014, 10:32:22 AM »
Thanks.  I used the ecosystem browser that worked. 

New question:  I want to pull the image that is swapped from an array of textures.  However, the variable pull-down menu does not show any options, even though I have variables of type texture, Object UnityEngine.Texture2D, and Object UnityEngine.SpriteRenderer.  Is there any way to do this? 

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Swap Single Sprite
« Reply #8 on: October 20, 2014, 10:54:28 AM »
Hi @nrhyde... I've had to put this away for a bit while I finish a job, so I can't give you a specific answer. But thinking about this quickly, you would first get the sprite from your array, then run swap sprite on it. I'm sure someone here can be more specific, but the general idea is to store the sprite from the array in a Sprite variable, then run SwapSprite on that variable. Does that make sense? I can't remember if that's easy to do in Playmaker, but I think it is...

And thanks @TheBadFeeling :)

nrhyde

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Swap Single Sprite
« Reply #9 on: October 20, 2014, 02:56:23 PM »
Actually, there isn't a variable pull-down list for the Sprite to Swap field.  The pull-down I was looking at was for the Reset on Exit field, and only accepts a Bool. 

So it looks like the only option is to have a separate state for each of the possible images and drag them to the field manually. 

Or could someone modify the Swap Single Sprite code so that it accepts variables?  If so, what type of variable would it be?  A texture or an object UnityEngine.Texture2D?

Thanks!  Even if I can't use a variable, the Swap Single Sprite is a cool action to have.

nikosurfing

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Swap Single Sprite
« Reply #10 on: October 20, 2014, 06:41:21 PM »
I ask the same question about it and i tried to reach the author on email.
Yes i do agree with @nrhyde, it would be nice if someone here can modify this action to accepts variable texture, maybe @jean can help us?
« Last Edit: October 21, 2014, 12:08:24 AM by nikosurfing »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Swap Single Sprite
« Reply #11 on: October 21, 2014, 04:28:48 AM »
Hi,

 yeah, it should be given the option to define a variable, Come back to me next week if nothing come up from other people, and I'll update the action ( likely create a new one actually to not break existing projects).

 Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Swap Single Sprite
« Reply #12 on: December 01, 2014, 03:24:56 AM »
hi i have been trying to get it to work with a variable, but failing so far

there is not fsmSprite so it has to be done differently.

i have been trying with fsmGameobject but getting errors

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Swap Single Sprite
« Reply #13 on: December 01, 2014, 08:25:57 AM »
i think i got this to work now AND should not break please test and confirm so i can change it on the EcoSystem :

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

using UnityEngine;
using System.Collections;


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

[RequiredField]
[CheckForComponent(typeof(SpriteRenderer))]
public FsmOwnerDefault gameObject;
[ObjectType(typeof(Sprite))]
public FsmObject spriteToSwap;

public FsmBool resetOnExit;

private SpriteRenderer spriteRenderer;

Sprite _orig;

public override void Reset()
{
gameObject = null;
resetOnExit = false;

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;
}

_orig = spriteRenderer.sprite;

SwapSprites();

Finish();
}

public override void OnExit()
{
if (resetOnExit.Value)
{
spriteRenderer.sprite = _orig;
}
}
void SwapSprites()
{
spriteRenderer.sprite = spriteToSwap.Value as Sprite;
}



}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Swap Single Sprite
« Reply #14 on: December 02, 2014, 11:39:38 AM »
Hi,

 thanks for this. I found several issues that I reported to Alex, so I would wait until the release of this action because:

-- if you click on the selector, the list proposed contains junk, not just sprites, so that will lead to confusion.

-- on the up coming beta version, FsmObject do not get created properly if you create a variable right from the action interface.

 Bye,

 Jean