Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: awshima on September 25, 2014, 09:48:02 AM

Title: Can't change a Sprite Renderer sprite at runtime
Post by: awshima on September 25, 2014, 09:48:02 AM
Hello,

Is there a way to change the sprite from a Sprite Renderer component at runtime?

I can drag the Sprite Renderer component to a FSM State to create a Set Property action, and I can drag a Sprite into the Sprite field, but that's all.

The other option besides dragging the Sprite should be selecting a variable, but I couldn't find any variable that can fit in this field. I've tried all variable types and none worked, the drop box always show "none".

I need a way to change the sprite using the sprite name generated at runtime.

Thanks,

Alexandre
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: TheBadFeeling on September 27, 2014, 07:18:38 AM
I'm having the same problem, but found this great action to switch a sprite from inside a State.
http://hutonggames.com/playmakerforum/index.php?topic=7797.msg37521#msg37521

My only issue now is that I can't assign a sprite dynamically, only drag them into this Action manually. I need to switch the sprite based on a sprite name I build from variables.

Var1 = "up"
Var2 = "left"
Assign sprite "Head"+Var1+Var2

Anyone else got any ideas how to achieve this?
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: TrentSterling on September 27, 2014, 12:50:38 PM
Single Sprite:
Code: [Select]
SpriteRenderer.sprite = Resources.Load<Sprite>("Sprites/MySprite");
Sprite Atlas:
Code: [Select]
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites/MySpriteContainer") as Sprite[]
Turn those into actions.  8)
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: TheBadFeeling on September 28, 2014, 08:55:41 AM
Thanks, but since I'm not a programmer, I wouldn't have a clue what to do with these... Could you be more specific?
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: TrentSterling on September 30, 2014, 05:50:06 PM
Unity loads only the assets referenced in the project. It skips images that aren't needed/used. That means you can't load a bunch of sprites without adding them to an array or something in code.

Assets put in the resources folder get loaded into the build no matter what.

Resources.LoadAll('path') loads all sprites/images/sounds in the resource path you specify.

Hopefully someone cool will come across this thread and turn those snippets into actions, as that's what's needed to load sprites dynamically with a string.
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: TheBadFeeling on October 04, 2014, 03:10:16 AM
Thanks for your input. Fortunately, a friend was able to help me setting a script up that I could call from PlayMaker, so I'm up and running now.
Title: Re: Can't change a Sprite Renderer sprite at runtime
Post by: nighty9 on December 10, 2014, 12:37:04 AM
Hi :)

It would be great if you could share that script :)

Basically i would like when i instantiate a prefab, to set it's sprite based on that sprite's name.

Ex: I load UnitPrefab and set it to Unit1, i would like to set it's sprite renderer property to Unit1Sprite ( Unit1Sprite is in my sprites folder )

Any ideas how to achieve that, or if that needs a script?

Cheers,
Nicolas