淡入播放模式但不在可执行文件中[Unity 5 - C#]

时间:2015-08-07 07:35:37

标签: c# unity3d fade unity5

我对Unity 5和C#脚本有点新鲜。我正在研究Unity的Turorial" Roll a Ball"现在我已经实现了在游戏开始时插入菜单和启动画面(当然是在Unity之后)。它工作得非常好.....但只在Unity Editor中,在Play模式下。当我构建可执行文件时,启动画面背景的淡入和淡出效果不起作用(只是文本),并且在文本消失后,游戏立即跳转到下一个场景(菜单)

脚本:

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);
        }
    }
}

}

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题...我通过在编辑 - >项目设置 - >图形设置上添加标准着色器来修复。

在数组上始终包含着色器增加数组的大小并搜索标准着色器。

希望它也适合你。 Cas