playMaker

Author Topic: Set Material Float on uGUI Image or RawImage  (Read 2004 times)

anadin

  • Playmaker Newbie
  • *
  • Posts: 5
Set Material Float on uGUI Image or RawImage
« on: November 16, 2015, 08:46:02 AM »
I found the SetMaterialFloat action and tried to use it to change a shader float on my background shader when using uGUI, that doesn't work because uGUI doesn't have Renderer.

I modified the action as follows..

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("uGui")]
[Tooltip("Sets a named float in a game object's material.")]
public class uGuiImageSetMaterialFloat : FsmStateAction
{
[CheckForComponent(typeof(UnityEngine.UI.Image))]
[Tooltip("The GameObject with the Image ui component.")]
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;

[RequiredField]
[Tooltip("A named float parameter in the shader.")]
public FsmString namedFloat;

[RequiredField]
[Tooltip("Set the parameter value.")]
public FsmFloat floatValue;

[Tooltip("Repeat every frame. Useful if the value is animated.")]
public bool everyFrame;

public override void Reset()
{
gameObject = null;
materialIndex = 0;
material = null;
namedFloat = "";
floatValue = 0f;
everyFrame = false;
}

public override void OnEnter()
{
DoSetMaterialFloat();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate ()
{
DoSetMaterialFloat();
}

void DoSetMaterialFloat()
{
if (material.Value != null)
{
material.Value.SetFloat(namedFloat.Value, floatValue.Value);
return;
}

if (UnityEngine.UI.Image.defaultGraphicMaterial == null)
{
LogError("Missing Material!");
return;
}

if (materialIndex.Value == 0)
{
UnityEngine.UI.Image.defaultGraphicMaterial.SetFloat(namedFloat.Value, floatValue.Value);
}

}
}
}

(I know it could be cleaned up a bit).

My question is how to change it to work on an instance of the material?, right now its working but changing he material on disk (I am changing ScreenX and ScreenY instead of doing it in the shader)

Thanks in advance for any help :)