Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Vid-Maddness · Oct 11, 2015 at 11:30 PM · c#loadlevelfadeout

Auto Fade when touches GameObject

I have a script that once made the screen fade in red and switched to another scene. After i upgraded the project to unity 5, instead of fading in red, it just instantly shows a solid pink screen for a sec, then changed to the scene. How do i fix this? i have Unity 5.1.2f1

Heres the script

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.PushMatrix();
     GL.LoadOrtho();
     GL.Begin(GL.QUADS);
     GL.Color(aColor);   // moved here, needs to be inside begin/end
     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)
 {
     float t = 0.0f;
     while (t<1.0f)
     {
         yield return new WaitForEndOfFrame();
         t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
         DrawQuad(aColor,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);
     }
     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);
 }

}

Heres the script that activates that script

using UnityEngine; using System.Collections;

public class LoadLevel : MonoBehaviour {

void OnTriggerEnter(Collider other) { AutoFade.LoadLevel("Main Menu" ,1,1,Color.red); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ExCx · Oct 18, 2015 at 12:02 PM

Just change your Awake method as follows:

 private void Awake()
      {
          DontDestroyOnLoad(this);
          m_Instance = this;
          m_Material = Resources.Load<Material>("Plane_No_zTest");
  #if UNITY_EDITOR
          if (m_Material == null)
          {
              var resDir = new System.IO.DirectoryInfo(System.IO.Path.Combine(Application.dataPath, "Resources"));
              if (!resDir.Exists)
                  resDir.Create();
              Shader s = Shader.Find("Plane/No zTest");
              if (s == null)
              {
                  string shaderText = "Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Fog { Mode Off } BindChannels { Bind \"Color\",color } } } }";
                  string path = System.IO.Path.Combine(resDir.FullName, "Plane_No_zTest.shader");
                  Debug.Log("Shader missing, create asset: " + path);
                  System.IO.File.WriteAllText(path, shaderText);
                  UnityEditor.AssetDatabase.Refresh(UnityEditor.ImportAssetOptions.ForceSynchronousImport);
                  UnityEditor.AssetDatabase.LoadAssetAtPath<Shader>("Resources/Plane_No_zTest.shader");
                  s = Shader.Find("Plane/No zTest");
              }
              var mat = new Material(s);
              mat.name = "Plane_No_zTest";
              UnityEditor.AssetDatabase.CreateAsset(mat, "Assets/Resources/Plane_No_zTest.mat");
              m_Material = mat;
  
          }
  #endif
      }

The answer is given by Bunny83 himself (The creator of the AutoFade script). So credits to him.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vid-Maddness · Oct 18, 2015 at 03:55 PM 0
Share

I get this error that says 'Assets/Scripts/AutoFade1.cs(57,9): error CS8025: Parsing error' what do i do about that?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Premature level loading. String loads level, instead of Return key. 2 Answers

How to fade in and out two panels in a scene? (c#) 1 Answer

How to fix Loading Screen? 0 Answers

I'm having trouble getting my "Restart" button to work 2 Answers

How i can check alpha level (RGBA) an UI obj? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges