playMaker

Author Topic: Get Material Color  (Read 8221 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Get Material Color
« on: July 23, 2012, 05:49:27 AM »
Hi,

 Following a request: 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.

Code: [Select]
// (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

Uttpd

  • Junior Playmaker
  • **
  • Posts: 70
Re: Get Material Color
« Reply #1 on: July 23, 2012, 02:45:14 PM »
Thanks a lot, working as expected  :)

Derek Jenson

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Get Material Color
« Reply #2 on: July 27, 2012, 02:58:24 PM »
Awesome, thank you!

Slimshader

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Get Material Color
« Reply #3 on: September 03, 2012, 12:41:23 PM »
Perfect! Thanks! ;D

Pawl

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Get Material Color
« Reply #4 on: August 25, 2014, 03:09:44 PM »
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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Get Material Color
« Reply #5 on: August 26, 2014, 09:01:37 AM »
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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Pawl

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Get Material Color
« Reply #6 on: August 26, 2014, 12:49:32 PM »
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 =)

LogLady

  • Full Member
  • ***
  • Posts: 150
Re: Get Material Color
« Reply #7 on: November 07, 2014, 04:26:31 PM »
I couldn't find this action in the Ecosystem. Could it be added?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Material Color
« Reply #8 on: November 21, 2014, 02:59:33 AM »
Hi,

 Done, it's now available via the Ecosystem.

 Bye,

 Jean