Jump to content
Unity Insider Forum
Ismoh

Change Material RenderingMode on Runtime

Recommended Posts

Ich habe auf folgender Basis (http://sassybot.com/blog/swapping-rendering-mode-in-unity-5-0/) eine kleine HelperClass erstellt, welche den Rendering Mode eines Materials zur Laufzeit ändert:

 

MaterialHelper:

using UnityEngine;
using System.Collections;
public static class MaterialHelper
{
public enum BlendMode
{
	Opaque,
	Cutout,
	Fade,
	Transparent
}
public static void SetMaterialRenderingMode(Material pMaterial, BlendMode pBlendMode)
{
	switch (pBlendMode)
	{
		case BlendMode.Opaque:
			pMaterial.SetFloat("_Mode", 0);
			pMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			pMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
			pMaterial.SetInt("_ZWrite", 1);
			pMaterial.DisableKeyword("_ALPHATEST_ON");
			pMaterial.DisableKeyword("_ALPHABLEND_ON");
			pMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			pMaterial.renderQueue = -1;
			break;
		case BlendMode.Cutout:
			pMaterial.SetFloat("_Mode", 1);
			pMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			pMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
			pMaterial.SetInt("_ZWrite", 1);
			pMaterial.EnableKeyword("_ALPHATEST_ON");
			pMaterial.DisableKeyword("_ALPHABLEND_ON");
			pMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			pMaterial.renderQueue = 2450;
			break;
		case BlendMode.Fade:
			pMaterial.SetFloat("_Mode", 2);
			pMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
			pMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
			pMaterial.SetInt("_ZWrite", 0);
			pMaterial.DisableKeyword("_ALPHATEST_ON");
			pMaterial.EnableKeyword("_ALPHABLEND_ON");
			pMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			pMaterial.renderQueue = 3000;
			break;
		case BlendMode.Transparent:
			pMaterial.SetFloat("_Mode", 3);
			pMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
			pMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
			pMaterial.SetInt("_ZWrite", 0);
			pMaterial.DisableKeyword("_ALPHATEST_ON");
			pMaterial.DisableKeyword("_ALPHABLEND_ON");
			pMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
			pMaterial.renderQueue = 3000;
			break;
	}
}
}

 

Anwendung:

MaterialHelper.SetMaterialRenderingMode(DEIN_MATERIAL, MaterialHelper.BlendMode.Fade);

 

Wenn man weitere Parameter ändern will, könnte man es wie folgt machen:

void ConfigurateMeshMaterial()
{
	meshMaterial = new Material(Shader.Find("Standard"));
	meshMaterial.name = "Field of View Material";
	MaterialHelper.SetMaterialRenderingMode(meshMaterial, MaterialHelper.BlendMode.Fade); //meshMaterial.SetFloat("_Mode", 2); //Rendering Mode: Fade
	meshMaterial.SetFloat("_Metallic", 1);
	meshMaterial.SetFloat("_Glossiness", 0); //Smoothness
	meshMaterial.color = new Color32(255, 255, 255, 100);
	FieldOfViewGO.GetComponent<MeshRenderer>().material = meshMaterial;
}

 

Hinzukommt:

Bitte einmal mit Senf beschmieren, danke :)

Share this post


Link to post
Share on other sites

Im Editor müsste das wohl an sich schon funktionieren, oder?

 

Aber damit es zur Laufzeit funktioniert, musst du auch folgenden Punkt beachten:

 

2. You must make sure Unity includes your required shader variants in the build

 

To do this, you need to make sure Unity knows that you want to use that shader variant by including at least one Material of that type in your Assets. The material must be used in a scene or alternatively be placed in your Resources Folder - otherwise Unity will still omit it from your build, because it appeared unused.

 

By completing both of the above steps, you have the full ability to modify your Materials using the Standard Shader at runtime.

 

If you are interested in learning more about the details of shader variants, and how to write your own, read about Making multiple shader program variants here.

 

 

Quelle: Unity Manual - Accessing and Modifying Material parameters via script

  • Like 1

Share this post


Link to post
Share on other sites

//Edit (fürchterliche Rechtschreib-/Tippfehler korrigiert):

Im Editor klappt es.

Im Build auch, aber nur weil ich per Hand ein Material erstellt habe, welches dem Script 1:1 übereinstimmt.

Dieses habe ich anschließend in einen Resource Ordner gelegt.

Edited by Ismoh

Share this post


Link to post
Share on other sites

Mach doch ne Extension drauß ;)

Material _someMaterial; // a material

public static class MaterialX{
public static void SetMaterialRenderingMode(this Material pMaterial, BlendMode pBlendMode)
{
//...
}
}
// usage
_someMaterial.SetMaterialRenderingMode(MaterialHelper.BlendMode.Fade);

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×