substance scripting in c#

I would like to access the float value of a substance in my project. I’m referencing this page…

I plan to access them through the GUI. What is the best way to set this up? Would I attach a script to the object? What isn’t clear from the example is how I get the substance I want. SetProceduralFloat is straight forward enough. Can someone post an example? Thanks.

You have to get the material from the GameObject’s renderer and cast it to a ProceduralMaterial.

void ChangeSubstanceValue()
{
	ProceduralMaterial mat;
        mat = (ProceduralMaterial)renderer.material;
        mat.SetProceduralFloat("Blah", 1f);
        mat.RebuildTextures();
}

I don’t even think you need the cast even. But I just tested that out and it appeared to work for me.

The problem I was running into is my meshes contain multi-subobject materials. So a single mesh renderer has several materials. I needed to be able to access the material directly. I guess a work around for this would have been to have a mesh for each substance on a hidden layer. Attach the script to that mesh and access that script from my GUI script.

But What I ended up doing is accessing the substance directly and controlling the float value with a slider using the following…

		//public float dirt = 0.5F;
		
		ProceduralMaterial substance = Resources.Load("Stripes_01", typeof(ProceduralMaterial)) as ProceduralMaterial;
		GUILayout.Label("Dirt");
		dirt = GUILayout.HorizontalSlider(dirt, 0.0F, 1.0F);
		substance.SetProceduralFloat("Dirt", dirt);
		substance.RebuildTextures ();