I'm building a DayDream VR game. I've previously had a script to fade out the screen when the user is clicking somewhere to change levels/scenes.
Since I've migrated to Unity 5.6 / Google VR SDK 1.2, any fading effect stopped working. But it still works in Preview mode on my desktop. This is because they changed the way the Camera works. I've tried different scripts online but none of them work, would anyone have an idea on how to do a screen fade on scene change please?
Here is the current main part of the code:
// Derived from OVRScreenFade
float elapsedTime = 0.0f;
Color color = fadeColor;
color.a = 0.0f;
fadeMaterial.color = color;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
color.a = Mathf.Clamp01(elapsedTime / fadeTime);
fadeMaterial.color = color;
}
I've also attempted to use Autofade script. As I mentioned they all work when tried the game on my desktop, they just don't work on the Android phone :(.
Any idea why please?
EDIT: Here is some extra code
public Material fadeMaterial = null; //starts NULL
//applied to cameras inside a function
foreach (Camera c in Camera.allCameras)
{
var fadeControl = c.gameObject.AddComponent<ScreenFadeControl>();
fadeControl.fadeMaterial = fadeMaterial;
fadeControls.Add(fadeControl);
}
Using the answer given here, I've created a script file with instructions, feel free to download it and use it if you need the same thing:
https://gist.github.com/xtrimsky/0d58ee4db1964577893353365903b91a
If you want to fade the entire screen I suggest:
- In
Unity
add aPanel
(make sure that it covers the whole screen).- Make a new
Material
and attach it to thePanel
.- The Material should have Rendering Mode set to
Transparent
.- Attach the following
Script
to thePanel
.
public Material m;
public float _colorSpeed = 0.01f;
private Color c;
private bool start = false;
void Update()
{
if (start)
{
if (c.a < 1.0f)
c.a = c.a + _colorSpeed;
m.color = c;
}
}
public void Fade()
{
c = m.color;
c.a = 0.0f;
start = true;
}
- Call the
Fade()
method when necessary.
Hope that solves your problem!