'Why when fading out list of materials each material fade out in another time and not all the materials fade at the same time?
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class FadeInOut : MonoBehaviour
{
List<Material> mats = new List<Material>();
private void Start()
{
foreach (Transform g in transform.GetComponentsInChildren<Transform>())
{
if (g.GetComponent<SkinnedMeshRenderer>() != null)
{
var l = g.GetComponent<SkinnedMeshRenderer>().sharedMaterials.ToList();
foreach(Material mat in l)
{
mats.Add(mat);
}
}
}
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.G))
{
StartCoroutine(FadeTo(mats, 0, 0.5f));
}
}
IEnumerator FadeTo(List<Material> materials, float targetOpacity, float duration)
{
for (int i = 0; i < materials.Count; i++)
{
Color color = materials[i].color;
float startOpacity = color.a;
float t = 0;
while (t < duration)
{
t += Time.deltaTime;
float blend = Mathf.Clamp01(t / duration);
color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);
materials[i].color = color;
yield return null;
}
}
}
}
The first material fade out after 0.5f second but then the other materials each one fade out in a bit more time the last one almost 1-2 seconds after the first one.
and i want all the materials to fade out a the same time if the time is 0.5f or 5f then fade out all the materials after 5 seconds or after 0.5f but the same time and not one after the other.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
