Post by bender on Jun 8, 2019 13:57:51 GMT
I've found that sometimes Invector's vCullingFadeControl.cs script has not worked for certain situations. I am also not able to change my Materials from Opaque to Fade since some of my materials will appear transparent.
If you want another solution try this one.
- It requires the iTween library (which is FREE and everyone should have)
- For performance, I used the Update Manager (asset store) which greatly improves performance. If you don't want to deal with that change "LateUpdateMe" to the standard LateUpdate.
Add this script to the Invector Camera (or any GameObject) and attach your "target" (most likely your player).
Everything that is a child of your target will fade. Enjoy.
using UnityEngine;
[RequireComponent(typeof(iTween))]
public class TweenCameraFade : OverridableMonoBehaviour
{
[Tooltip("The main target from which all FadeObjects will be triggered to fade")]
public GameObject target = null;
[Range(0, 15)]
[Tooltip("Distance from the camera when fading begins")]
public float startFadeAt = 1.5f;
[Range(0, 10)]
[Tooltip("The time it will take to completely fade in/out")]
public float fadeTime = 1f;
private Renderer[] targets;
private bool faded = false;
public override void UpdateMe() {
if ((Camera.main.transform.position - target.transform.position).magnitude < startFadeAt)
{
if (!faded)
{
SetMaterialTransparent();
iTween.FadeTo(target, 0, fadeTime);
faded = true;
}
}
else if (faded) {
iTween.FadeTo(target, 1, fadeTime);
Invoke("SetMaterialOpaque", 1.0f);
faded = false;
}
}
private void SetMaterialTransparent()
{
Renderer[] targets = target.GetComponentsInChildren<Renderer>();
foreach (Renderer tgt in targets)
{
foreach (Material m in tgt.materials)
{
m.SetFloat("_Mode", 2);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
}
}
}
private void SetMaterialOpaque()
{
Renderer[] targets = target.GetComponentsInChildren<Renderer>();
foreach (Renderer tgt in targets)
{
foreach (Material m in tgt.materials)
{
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
m.SetInt("_ZWrite", 1);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = -1;
}
}
}