Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 阿尔法不起作用_C#_Unity3d - Fatal编程技术网

C# 阿尔法不起作用

C# 阿尔法不起作用,c#,unity3d,C#,Unity3d,我对这个脚本有一个问题:它应该创建一个带有alpha的黑色四边形,然后淡出,然后淡入,但“淡出”部分似乎不起作用。它只是一片漆黑,没有褪色。 以下是我使用的代码: // AutoFade.cs using UnityEngine; using System.Collections; public class AutoFade : MonoBehaviour { private static AutoFade m_Instance = null; private M

我对这个脚本有一个问题:它应该创建一个带有alpha的黑色四边形,然后淡出,然后淡入,但“淡出”部分似乎不起作用。它只是一片漆黑,没有褪色。 以下是我使用的代码:

 // AutoFade.cs
 using UnityEngine;
 using System.Collections;

 public class AutoFade : MonoBehaviour
 {
     private static AutoFade m_Instance = null;
     private Material m_Material = null;
     private string m_LevelName = "";
     private int m_LevelIndex = 0;
     private bool m_Fading = false;

     private static AutoFade Instance
     {
         get
         {
             if (m_Instance == null)
             {
                 m_Instance = (new GameObject("AutoFade")).AddComponent<AutoFade>();
             }
             return m_Instance;
         }
     }
     public static bool Fading
     {
         get { return Instance.m_Fading; }
     }

     private void Awake()
     {
         DontDestroyOnLoad(this);
         m_Instance = this;
         m_Material = new Material("Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Fog { Mode Off } BindChannels { Bind \"Color\",color } } } }");
     }

     private void DrawQuad(Color aColor,float aAlpha)
     {
         aColor.a = aAlpha;
         m_Material.SetPass(0);
         GL.Color(aColor);
         GL.PushMatrix();
         GL.LoadOrtho();
         GL.Begin(GL.QUADS);
         GL.Vertex3(0, 0, -1);
         GL.Vertex3(0, 1, -1);
         GL.Vertex3(1, 1, -1);
         GL.Vertex3(1, 0, -1);
         GL.End();
         GL.PopMatrix();
     }

     private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, Color aColor)
     {
         Debug.Log("Fading");
         float t = 0.0f;
         while (t<1.0f)
         {
             yield return new WaitForEndOfFrame();
             t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
             DrawQuad(aColor,t);
             Debug.Log (t);
         }
         if (m_LevelName != "")
             Application.LoadLevel(m_LevelName);
         else 
             Application.LoadLevel(m_LevelIndex);
         while (t>0.0f)
         {
             yield return new WaitForEndOfFrame();
             t = Mathf.Clamp01(t - Time.deltaTime / aFadeInTime);
             DrawQuad(aColor,t);
             Debug.Log (t);
         }
         m_Fading = false;
     }
     private void StartFade(float aFadeOutTime, float aFadeInTime, Color aColor)
     {
         m_Fading = true;
         StartCoroutine(Fade(aFadeOutTime, aFadeInTime, aColor));
     }

     public static void LoadLevel(string aLevelName,float aFadeOutTime, float aFadeInTime, Color aColor)
     {
         if (Fading) return;
         Instance.m_LevelName = aLevelName;
         Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
     }
     public static void LoadLevel(int aLevelIndex,float aFadeOutTime, float aFadeInTime, Color aColor)
     {
         if (Fading) return;
         Instance.m_LevelName = "";
         Instance.m_LevelIndex = aLevelIndex;
         Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
     }
 }

谢谢

信息太少了。您在哪个平台上使用脚本?当我编写它时,我在windows单机版32位、windows webplayer和android(Nexus7)上进行了测试。现在,我刚刚在我的新a 64位机器上刷新了我的所有构建,并且一切仍然正常工作。顺便说一句,我仍然在使用Unity的免费版本。正如你在文章中看到的,过去有些人也有问题。然而,由于你没有告诉我们你的规格和平台,我怀疑这里是否有人能帮你…我不习惯在帖子中放硬件/操作系统信息,但如果有用的话,我正在mboth Windows 8.1 x64和OS X YOsemite上测试,这两个都是Unity Pro,没有特定的构建,它也不能在编辑器模式下工作。我是否必须在项目设置中设置一些特殊的相机配置或特殊设置?或者只是复制并粘贴你的脚本?好吧,它应该是现成的。由于它在“EndOfFrame”处绘制四边形,因此它不绑定到任何摄影机。您可以将其视为后处理效果;)在Unity中实际上不可能有任何特定的构建。构建设置中选择的平台对Unity的行为有很大的影响。然而,我还没有在一个不起作用的平台上测试过它。你试过Magendaz在评论中提出的建议吗?使用透明/漫反射着色器?如果这样也不行,说明你的显卡或驱动程序有问题。我应该把材料放在调用脚本的对象中吗?这应该是问题所在。
AutoFade.LoadLevel(Application.loadedLevel, 1f, 1f, Color.black);