alpha won't change when built to iOS but works in inspector,alpha doesn't change when built to iOS but works in inspector
Script works fine in the inspector, but when it gets built to iOS, the alpha doesn’t change if the material is initially set as Opaque. All standard shaders in the default render pipeline.
This script is meant to take an object that has more than one material on it and make the GameObject transparent when the slider is used. To ensure that the materials render properly when the alpha is at 1f, I am using a shader utility script to switch the material’s render type to Opaque and switch it to Transparent so that they can fade out.
I want the materials to start as Opaque and only be made Transparent if the slider is used. This is where the script works fine in the inspector, but as soon as I build, the materials won’t change their alpha. I know the script changes them to Transparent, because they render improperly. If I make the materials start off as Transparent, the alpha changes correctly but they again render improperly until I move the slider down then put it back to 1f. So I need the materials to always start as Opaque and only change when the slider gets used.
Why would it suddenly not work when I build to iOS but work in the inspector? Unity 2019.4.3f, AR Foundation 2.1.8, Xcode 12.3, iOS 14.3
{
public GameObject currentGameObject;
public Slider slider;
private float alpha = 1f;
public float Alpha
{
get => alpha;
set
{
if (alpha != value)
{
alpha = value;
ToggleBlendMode();
ChangeAlpha();
}
}
}
private Renderer[] mRenderers;
private List<Material> mMaterials = new List<Material>();
void Start()
{
// assuming that renderers will be present on currentGameObject at Start and will never change
mRenderers = currentGameObject.GetComponentsInChildren<Renderer>();
foreach (var rend in mRenderers)
{
mMaterials.AddRange(rend.materials);
}
// if slider is present
if (slider)
{
Alpha = slider.value;
slider.onValueChanged.AddListener((arg) => Alpha = arg);
}
}
void ToggleBlendMode()
{
foreach (var mat in mMaterials)
{
if (Alpha == 1f)
{
//set BlendMode from StandardShaderUtils to Opaque
StandardShaderUtils.ChangeRenderMode(mat, StandardShaderUtils.BlendMode.Opaque);
}
else if (Alpha < 1f)
{
//set Blendmode to Transparent
StandardShaderUtils.ChangeRenderMode(mat, StandardShaderUtils.BlendMode.Transparent);
}
}
}
void ChangeAlpha()
{
foreach(var mat in mMaterials)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, Alpha);
mat.SetColor("_Color", newColor);
}
}
}
Answer by AscendReality · Jan 12, 2021 at 12:19 AM
Found a solution, just use a better transparent shader like the one here, https://docs.unity3d.com/Manual/SL-CullAndDepth.html?_ga=2.55575022.478898987.1610348523-1254744539.1609707651
Your answer
Follow this Question
Related Questions
How to uinstall IOS support? 0 Answers
How Do I Ensure that --enable-unity-event-support is Set for iOS Builds? 0 Answers
My app is MUCH larger on iOS than Android 1 Answer
How to set Stripping Level for iOS 0 Answers
What is the difference between a Unity Cloud Build and a local build for iOS App Store? 0 Answers