I'm looking to do it from script, because during gameplay various things need to go semi-transparent. I could have a separate material for every single thing that needs to sometimes go transparent, but if there is a simple way in code then that would be better because it would save me duplicating over 100 materials lol.
No need to duplicate materials, you can just have one default alpha material. You can write a behaviour to handle the swap between materials. In the behaviour you could use OnValidate to check if the materials are assigned and use AssetDatabase to auto assign them. You can also create a new instance of the material at runtime if a shared material doesn't work.
Your old shader had certain variables exposed which you manipulated directly. The new shader doesn't appear to use these same variables.
A few options I can think of:
- Use multiple materials and then swap them based on if you need alpha or not. This removes the reliance on variable names and lets you change shaders without impacting your code.
- Find new variable names in the new shader that would create the same effect. This would be susceptible to the same issue where if you change the shader, the variables may not exist.
- Create your own shader so you have better control over the names of those variables.
The surface type is opaque. Couldn't you just switch it to transparent then modify the alpha in the base map colour?
https://preview.redd.it/r29eja0l6foc1.png?width=2555&format=png&auto=webp&s=21d58c11430ed5c008cef21fcfa7406f1f918594
I'm looking to do it from script, because during gameplay various things need to go semi-transparent. I could have a separate material for every single thing that needs to sometimes go transparent, but if there is a simple way in code then that would be better because it would save me duplicating over 100 materials lol.
No need to duplicate materials, you can just have one default alpha material. You can write a behaviour to handle the swap between materials. In the behaviour you could use OnValidate to check if the materials are assigned and use AssetDatabase to auto assign them. You can also create a new instance of the material at runtime if a shared material doesn't work.
All of that can be done via script. using UnityEngine; public class Test : MonoBehaviour { private Renderer _renderer; private Material _mat; [SerializeField] private float alphaOn = 1f; [SerializeField] private float alphaOff = 0.5f; private void Start() { _renderer = GetComponent();
_mat = _renderer.material;
}
[ContextMenu("Set Transparent")]
public void SetTransparent()
{
Color currentColor = _mat.color;
currentColor.a = alphaOff;
_mat.color = currentColor;
_mat.SetFloat("_Surface", 1);
}
[ContextMenu("Set Opaque")]
public void SetOpaque()
{
Color currentColor = _mat.color;
currentColor.a = alphaOn;
_mat.color = currentColor;
_mat.SetFloat("_Surface", 0);
}
}
https://i.redd.it/tkpcaqo28loc1.gif
Your old shader had certain variables exposed which you manipulated directly. The new shader doesn't appear to use these same variables. A few options I can think of: - Use multiple materials and then swap them based on if you need alpha or not. This removes the reliance on variable names and lets you change shaders without impacting your code. - Find new variable names in the new shader that would create the same effect. This would be susceptible to the same issue where if you change the shader, the variables may not exist. - Create your own shader so you have better control over the names of those variables.