playMaker

Author Topic: Enable or disable shadows on a gameObject  (Read 8555 times)

KirbyRawr

  • Playmaker Newbie
  • *
  • Posts: 16
    • Overflowing Team
Enable or disable shadows on a gameObject
« on: October 20, 2013, 06:01:11 AM »
Hi, i saw there isn't any action like this so i wrote a new one, this is my first custom action ^w^.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
// CastShadows.cs
// Custom Action made by KirbyRawr from OverflowingStudios.com

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Lights)]
[Tooltip("Enable or disable shadows")]
public class CastShadows : FsmStateAction
{
public FsmGameObject gameObject;
public FsmBool skinnedMesh;
[Tooltip("Cast Shadows")]
public FsmBool castShadows;
[Tooltip("Receive Shadows")]
public FsmBool receiveShadows;

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

}

public override void OnEnter()
{
if(skinnedMesh.Value==true){
gameObject.Value.GetComponent<SkinnedMeshRenderer>().castShadows = castShadows.Value;
gameObject.Value.GetComponent<SkinnedMeshRenderer>().receiveShadows = receiveShadows.Value;
}

if(skinnedMesh.Value==false){
gameObject.Value.GetComponent<MeshRenderer>().castShadows = castShadows.Value;
gameObject.Value.GetComponent<MeshRenderer>().receiveShadows = receiveShadows.Value;
}
}

    }
}
« Last Edit: October 21, 2013, 08:09:01 AM by KirbyRawr »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Enable or disable shadows on a gameObject
« Reply #1 on: October 21, 2013, 02:10:26 AM »
Hi,

 Very good! Also, don't forget that you can access all these publich properties using the "Set property" action. Simply Drag the "mesh renderer" component onto the action stack and you will be able to pick the various properties, including shadow casting and receiving.

bye,

 Jean


KirbyRawr

  • Playmaker Newbie
  • *
  • Posts: 16
    • Overflowing Team
Re: Enable or disable shadows on a gameObject
« Reply #2 on: October 21, 2013, 07:53:31 AM »
Hi,

 Very good! Also, don't forget that you can access all these publich properties using the "Set property" action. Simply Drag the "mesh renderer" component onto the action stack and you will be able to pick the various properties, including shadow casting and receiving.

bye,

 Jean
Oh, thanks Jean i didn't know it ^^!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Enable or disable shadows on a gameObject
« Reply #3 on: October 21, 2013, 08:08:24 AM »
Hi,

 That's fine! It's anyway more advantagous to use proper custom action, as it doesn't use reflection and therefore, more performant. So it's good that you have the skills to procude custom actions! not everything is accessible via Get/set property, especially, anything static.


bye,

 Jean

KirbyRawr

  • Playmaker Newbie
  • *
  • Posts: 16
    • Overflowing Team
Re: Enable or disable shadows on a gameObject
« Reply #4 on: October 21, 2013, 08:09:40 AM »
Added support for Skinned Mesh Renderer!  :)

PD: i just saw your another reply haha, thanks anyways! ^^

misterlee

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Enable or disable shadows on a gameObject
« Reply #5 on: January 06, 2017, 06:23:25 PM »
This is just what I was looking for.... except 'castShadows' is deprecated now and we have to use ShadowCastingMode instead.

I made a modification, it works but I don't know if it's the best way of doing it. I'm not much of a programmer (hence using PlayMaker!)

Code: [Select]
public override void OnEnter()
{
if(skinnedMesh.Value==true){

gameObject.Value.GetComponent<SkinnedMeshRenderer>().receiveShadows = receiveShadows.Value;

if (castShadows.Value == true) {
gameObject.Value.GetComponent<SkinnedMeshRenderer> ().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
} else {
gameObject.Value.GetComponent<SkinnedMeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
}
}
if(skinnedMesh.Value==false){

gameObject.Value.GetComponent<MeshRenderer>().receiveShadows = receiveShadows.Value;

if (castShadows.Value == true) {
gameObject.Value.GetComponent<SkinnedMeshRenderer> ().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
} else {
gameObject.Value.GetComponent<SkinnedMeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
}
}
}



p.s. Just discovered you need to put Finish(); at the end too, otherwise it gets stuck in that state and doesn't move on.

« Last Edit: January 06, 2017, 06:33:57 PM by misterlee »