Unity 5 material alpha

Hi all,

I’m trying to set material alphas in the project window through a C# editor script. I manage to set it to the value I want using SetColor(“_Color”,newColor) and it shows up correctly in the inspector, however, it doesn’t update in the scene.
If I then change ANY value in the inspector it updates the alpha setting.
I have tried setting the material as dirty and doing an AssetDatabase.Refresh() but it doesn’t seem to work. Nor does it update when saving the project, closing unity and reopening.
There must be something I’m missing but I can’t work out what. Any help would be much appreciated.

Thanks,

Matt

Hey there,

I’ve been trying to do what I think you’re trying to do.

First, I set the shader of the object to “Legacy Shaders > Transparent > Diffuse”

Then in a script attached to the game object, I did…

	public float alpha;
	public Color colourPicker = new Color (0.5f, 0.5f, 0.5f);

	// Use this for initialization
	void Start () {
		colourPicker.a = colourPicker.a - alpha;
		this.gameObject.GetComponent<MeshRenderer> ().material.SetColor ("_Color", colourPicker);

In the inspector, I set the colour picker alpha to 255 to start with. If you use this script, just change the Alpha float variable between 0 ( no transparency ) and 1 (see-through)

Hope this helps…I’m sure someone will come along and have a better way. But if not, I hope this serves you for now :slight_smile:

Unfortunately this didn’t help :frowning:

My solution in the end was to put all transparent materials to an array then use Selection.objects to select them in the project window then manually change the value of one of the common variables in the inspector window, in my case the tiling offset.

renderer.material.SetFloat(“_Mode”, 3);
renderer.material.color = GameHelper.SetAlpha(renderer.material.color, 0.5f);

        renderer.material.SetOverrideTag("RenderType", "Transparent");
        renderer.material.SetInt("_SrcBlend", 1);
        renderer.material.SetInt("_DstBlend", 10);
        renderer.material.SetInt("_ZWrite", 0);
        renderer.material.DisableKeyword("_ALPHATEST_ON");
        renderer.material.DisableKeyword("_ALPHABLEND_ON");
        renderer.material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
        renderer.material.renderQueue = 3000;