playMaker

Author Topic: Change Sprite Renderer Custom Action (SOLVED)  (Read 7361 times)

Alarconte

  • Playmaker Newbie
  • *
  • Posts: 5
Change Sprite Renderer Custom Action (SOLVED)
« on: July 22, 2014, 10:03:54 AM »
Trying to do a custom action to change the sprite of a sprite renderer, but seems like I don't know what to use instead of FsmTexture... Is not possible work with sprites in playmaker?

I tried this:

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions

{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Change a Sprite")]
public class AALChangeSprite : FsmStateAction
{
[Tooltip("The GameObject that the material is applied to.")]
[CheckForComponent(typeof(SpriteRenderer))]
public FsmOwnerDefault gameObject;
public FsmTexture newSprite;

private GameObject go;
private SpriteRenderer sprite;

public override void Reset()
{
gameObject = null;
newSprite = null;
}

public override void OnEnter()
{
DoSetNewSprite();
Finish();
}

void DoSetNewSprite()
{
go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
sprite = go.GetComponent<SpriteRenderer>();
sprite.sprite = newSprite;
}


}
}
« Last Edit: July 25, 2014, 09:49:57 AM by Alarconte »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Change Sprite Renderer Custom Action
« Reply #1 on: July 22, 2014, 11:00:12 AM »
This should work:
sprite.sprite = newSprite.value;

newSprite.value instead of newSprite.

Alarconte

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Change Sprite Renderer Custom Action
« Reply #2 on: July 22, 2014, 12:14:42 PM »
the actual problems seems to be this:

Assets/SCENARIOS/Character Creation/Playmaker/AALChangeSprite.cs(35,40): error CS0029: Cannot implicitly convert type `UnityEngine.Texture' to `UnityEngine.Sprite'

So the thing is that fsmTexture cannot be a sprite, and there is no fsmSprite... Maybe I didn't look well enough in documentation, but I don't know what to do.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Change Sprite Renderer Custom Action
« Reply #3 on: July 22, 2014, 12:46:58 PM »
Is the texture imported as a Sprite? In import settings...

Alarconte

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Change Sprite Renderer Custom Action
« Reply #4 on: July 22, 2014, 02:59:42 PM »
Yes, the textures are imported as sprites cause I use Sprite Renderers. The script just don't compile cause "Cannot implicitly convert type `UnityEngine.Texture' to `UnityEngine.Sprite'".

So the thing is, can playmaker handle sprites per se?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Change Sprite Renderer Custom Action
« Reply #5 on: July 22, 2014, 05:35:23 PM »
Sorry took me a while to catch up! You have to use an FsmObject variable of type UnityEngine.Sprite. For example:

[ObjectType(typeof(Sprite))]
public FsmObject sprite;

The ObectType attribute tells the editor what object type to expect.
Take a look at Play Sound, or PlayMovieTexture for example usage...

Alarconte

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Change Sprite Renderer Custom Action
« Reply #6 on: July 23, 2014, 01:48:35 PM »
I looked that scripts but...

Right now this is the code:

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions

{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Change a Sprite")]
public class AALChangeSprite : FsmStateAction
{
[Tooltip("The GameObject that the material is applied to.")]
[CheckForComponent(typeof(SpriteRenderer))]
public FsmOwnerDefault gameObject;

[ObjectType(typeof(Sprite))]
public FsmObject newSprite;




private GameObject go;
private SpriteRenderer spriteR;

public override void Reset()
{
gameObject = null;
newSprite = null;
}

public override void OnEnter()
{
DoSetNewSprite();
Finish();
}

void DoSetNewSprite()
{
go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
spriteR = go.GetComponent<SpriteRenderer>();
spriteR.sprite = newSprite.Value;
}

}
}

And the error is:

Assets/SCENARIOS/Character Creation/Playmaker/AALChangeSprite.cs(39,41): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Sprite'. An explicit conversion exists (are you missing a cast?)

Any light over here?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Change Sprite Renderer Custom Action
« Reply #7 on: July 23, 2014, 01:57:09 PM »
You have to cast Value to the correct type:

spriteR.sprite = newSprite.Value as Sprite;

Alarconte

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Change Sprite Renderer Custom Action
« Reply #8 on: July 25, 2014, 09:49:40 AM »
Works perfect!

Lots of thanks!