Playmaker Forum
PlayMaker Updates & Downloads => Share New Actions => Topic started by: jeanfabre on July 23, 2012, 05:49:27 AM
-
Hi,
Following a request: http://hutonggames.com/playmakerforum/index.php?topic=1944.msg8603#msg8603 (http://hutonggames.com/playmakerforum/index.php?topic=1944.msg8603#msg8603)
please find a Get Material Color action. It has an improvment over the Set Material Color because it checks first if the color property exists on that material. so it will fire an error, but simply a fail event.
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Gets a named color value from a game object's material.")]
public class GetMaterialColor : FsmStateAction
{
[Tooltip("The GameObject that the material is applied to.")]
[CheckForComponent(typeof(Renderer))]
public FsmOwnerDefault gameObject;
[Tooltip("GameObjects can have multiple materials. Specify an index to target a specific material.")]
public FsmInt materialIndex;
[Tooltip("Alternatively specify a Material instead of a GameObject and Index.")]
public FsmMaterial material;
[UIHint(UIHint.NamedColor)]
[Tooltip("The named color parameter in the shader.")]
public FsmString namedColor;
[RequiredField]
[UIHint(UIHint.Variable)]
[Tooltip("Get the parameter value.")]
public FsmColor color;
public FsmEvent fail;
public override void Reset()
{
gameObject = null;
materialIndex = 0;
material = null;
namedColor = "_Color";
color = null;
fail = null;
}
public override void OnEnter()
{
DoGetMaterialColor();
Finish();
}
void DoGetMaterialColor()
{
if (color.IsNone)
{
return;
}
var colorName = namedColor.Value;
if (colorName == "") colorName = "_Color";
if (material.Value != null)
{
if ( ! material.Value.HasProperty(colorName))
{
Fsm.Event(fail);
return;
}
color.Value = material.Value.GetColor(colorName);
return;
}
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;
if (go.renderer == null)
{
LogError("Missing Renderer!");
return;
}
if (go.renderer.material == null)
{
LogError("Missing Material!");
return;
}
if (materialIndex.Value == 0)
{
if ( ! go.renderer.material.HasProperty(colorName))
{
Fsm.Event(fail);
return;
}
color.Value = go.renderer.material.GetColor(colorName);
}
else if (go.renderer.materials.Length > materialIndex.Value)
{
var materials = go.renderer.materials;
if ( ! materials[materialIndex.Value].HasProperty(colorName))
{
Fsm.Event(fail);
return;
}
color.Value = materials[materialIndex.Value].GetColor(colorName);
}
}
}
}
Bye,
Jean
-
Thanks a lot, working as expected :)
-
Awesome, thank you!
-
Perfect! Thanks! ;D
-
Apologies for digging up an old thread, but is there any reason why the GetMaterialColor action is not included with the default actions in the latest version of PlayMaker (1.7.7.2) on the asset store? Seems like it should be there.
-
Alex usually does add a few of the useful custom actions that feel like they should be stock in the package but not all of them because it would bloat the package with scripts that people rarely use.
-
Ah okay, thanks for the reply. I just wanted to make sure it hadn't gone missing accidentally, seeing as there is a SetMaterialColor action =)
-
I couldn't find this action in the Ecosystem. Could it be added?
-
Hi,
Done, it's now available via the Ecosystem.
Bye,
Jean