Reputation: 45
I'm a bit new to Unity 5 and C# scripting. I'm working on an evolution of the Unity's Turorial "Roll a Ball" and now I've achieved to insert a menu and a splashscreen (after the Unity one, of course) at the start of the game. It works absolutely fine.....but only inside the Unity Editor, during the Play Mode. When I build the executable, the fade in and out effects of the splashscreen background don't work (just the text) and, after have faded in the text, the game immediately jumps to the next scene (the menu)
That's the script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SplashController : MonoBehaviour {
private float timer; //simply a...timer
private float now_alpha; //temporary variable storing the background alpha
private int in_out; //flag variable storing the current fading direction (1 = fade backgr. in; 0 = fade text in; -1 = fade all out)
private Color temp; //temporary variable storing the text alpha
public RawImage image; //background
public Text presents; //a text that has to be displayed
public int load_me; //the next scene to be loaded
void SetAlpha(float x) //this sets the backgr. alpha
{
Color temp = image.color;
temp.a = x;
now_alpha = x;
image.color = temp;
}
void Start()
{
timer = 4.0f;
in_out = 1; //fade direction = backgr. in
temp = presents.material.color; //hides the text
temp.a = 0f;
presents.material.color = temp;
}
void Fade()
{
if (in_out == 1) //fades the back in
{
now_alpha += System.Convert.ToSingle(0.5 * Time.deltaTime);
SetAlpha (now_alpha);
}
else if (in_out == -1) //fades the back out
{
now_alpha -= System.Convert.ToSingle(0.5 * Time.deltaTime);
SetAlpha (now_alpha);
if (now_alpha <= 0.02)
{
timer = 0;
}
}
}
void FadeText()
{
if (in_out == 0) //fades the text in
{
temp = presents.material.color;
temp.a += System.Convert.ToSingle(0.5 * Time.deltaTime);
presents.material.color = temp;
}
else if (in_out == -1) //fades the text out
{
temp = presents.material.color;
temp.a -= System.Convert.ToSingle(0.5 * Time.deltaTime);
presents.material.color = temp;
}
}
void Update()
{
timer -= Time.deltaTime;
if (timer >= 0) //before the 0
{
if (in_out == 1) //step 1: fade the backgr. in
{
Fade();
}
else if (in_out == 0) //step 2: fade the text in
{
FadeText();
}
else if (in_out == -1) //step 3: fade all out
{
FadeText();
Fade();
}
}
else //after the 0 and before the next step
{
if (in_out == 1)
{
timer = 4.0f;
in_out--; //changes fading direction/phase
}
else if (in_out == 0)
{
timer = 4.0f;
in_out--;
temp = presents.material.color; //idk why, but after being faded in, the text is bold and so I have to turn it normal
temp.a = 1;
presents.material.color = temp;
}
else if (in_out == -1)
{
Application.LoadLevel (load_me);
}
}
}
}
Any ideas? Thanks
Upvotes: 0
Views: 736
Reputation: 1
I had the same problem... I fixed by adding the Standard Shader on Edit->Project Settings->Graphics Settings.
On the array Always Included Shaders increase the size of the array and search for the Standard shader.
Hope it works for you too. Cas
Upvotes: 0