public void ChangeAlphaToValue(float dstAlpha, float delayTime = 0.0f, float time = 2.0f, OnAlphaChangeCompleted alphaChange = null) { if (render == null) { return; } if (Mathf.Abs(render.material.color.a - dstAlpha) < 0.1f) { return; } //ActivateTransparent(true); //targetAlpha = dstAlpha; StopAllCoroutines(); StartCoroutine(AlphaUpdate(dstAlpha, delayTime, time, alphaChange)); }
IEnumerator AlphaUpdate(float dstAlpha, float delayTime = 0.0f, float time = 2.0f, OnAlphaChangeCompleted alphaChange = null) { yield return(new WaitForSeconds(delayTime)); ActivateTransparent(true); float startTime = Time.time; float startAlpha = render.material.color.a; float alphaValue = -1000.0f; while (render != null && Mathf.Abs(alphaValue - dstAlpha) > Accuracy) { alphaValue = Mathf.Lerp(startAlpha, dstAlpha, (Time.time - startTime) / time); for (int i = 0; i < render.materials.Length; i++) { Color oldColor = render.materials[i].color; render.materials[i].color = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue); } yield return(new WaitForSeconds(0.1f)); } if (alphaChange != null) { alphaChange(this, dstAlpha); } if (Mathf.Abs(dstAlpha - 1.0f) <= Accuracy && Mathf.Abs(render.material.color.a - 1.0f) <= Accuracy) { ActivateTransparent(false); } }