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 /
avatar image
17
Question by Grady · May 22, 2011 at 12:44 AM · transitionswitch scenes

Transitions between changing scenes

hey guys,

Is it possible to make transitions when changing scenes. So like it might fade out, or zoom out or whatever....

thanks :)

-Grady

Comment
Add comment · Show 1
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 carlosabreuRD · Sep 07, 2014 at 06:33 PM 0
Share

i'm using this on my game, i would like to put you on the credits. please let me know carlosabreu1981@gmail.com

6 Replies

· Add your reply
  • Sort: 
avatar image
39
Best Answer

Answer by Bunny83 · May 22, 2011 at 03:12 AM

I've written a "small" script that allows you to load a level, but it fades the old level out and fades the new one in. The documentation states that the GL class is only available in Unity pro, but in my Unity version (3.0) it works fine in the free version ;)

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


You don't need to do anything with this script, just place it somewhere in your project. It creates automatically a GameObject when you use the LoadLevel function.

To load a new level just write in any of your scripts:

 AutoFade.LoadLevel(1 ,3,1,Color.black);
 // or
 AutoFade.LoadLevel("MyLevelName" ,3,1,Color.black);

The 4 parameters are:

  1. LevelName or LevelIndex

  2. FadeOutTime - time in seconds until the level actually starts loading

  3. FadeInTime - time in seconds until the fade-in process is completed.

  4. FadeColor - this is the color the screen fades to.

Keep in mind that after calling AutoFade.LoadLevel the game continues "FadeOutTime" seconds. You might want to block Input or something while the fade-process is active. Just check if AutoFade.Fading is true.

Have fun ;)

edit here's my test project (webplayer required) where you can see the script in action. I've posted the link already in a comment below, but just in case you can't find it.

second edit
Since Time.deltaTime returns 0 when Time.timeScale is set to 0, it won't fade since the t value doesn't change. You can replace Time.deltaTime with Time.unscaledDeltaTime. That way it will work even when your game is paused. However this has one problem. Since unscaledDeltaTime is not modified / scaled / clamped, if the loading of the new level takes some time, unscaledDeltaTime might return huge values (well the time since the last frame which could even be seconds when using LoadLevel).

Time.deltaTime solves this problem by clamping the value to "Maximum Allowed Timestep". So if a frame takes longer than that it will be clamped to that value. You would have to do that manually. an alternative would be to just skip the first frame after the level load:

 //[ ... ]
     Application.LoadLevel(m_LevelIndex);
 yield return null; // skip first frame in new level
 while (t>0.0f)
 //[ ... ]

However there might be other reasons why the unscaledDeltaTime going wrong so clamping it like Time.deltaTime is probably the best solution:

 float unscaledClampedDT()
 {
     return Mathf.Clamp(Time.unscaledDeltaTime ,0.00001f, Time.maximumDeltaTime);
 }

You might want to clamp it to a smaller value than maximumDeltaTime. maximumDeltaTime is usually 0.33333 (1/3) so it will skip into the fading by 1/3 of a second if the loading took more than that. It's probably the best to use both, the clampedDeltaTime and the yield to skip the first frame.

The changes would look like this:

         //[...]
         while (t<1.0f)
         {
             yield return new WaitForEndOfFrame();
             t = Mathf.Clamp01(t + unscaledClampedDT() / aFadeOutTime);
             DrawQuad(aColor,t);
         }
         if (m_LevelName != "")
             Application.LoadLevel(m_LevelName);
         else
             Application.LoadLevel(m_LevelIndex);
         yield return null; // skip first frame
         while (t>0.0f)
         {
             yield return new WaitForEndOfFrame();
             t = Mathf.Clamp01(t - unscaledClampedDT() / aFadeInTime);
             DrawQuad(aColor,t);
         }
         //[...]


Comment
Add comment · Show 61 · 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 Grady · Jul 02, 2011 at 02:12 AM 0
Share

do you have that in javascript?

thanks

-Grady

avatar image testure · Jul 02, 2011 at 02:19 AM 5
Share

I may be speaking out of turn and @Bunny83 could whip out a JS version- but most people don't code two copies of everything they're doing to make sure they have it in both languages :)

converting C# to javascript is not that hard, it could be a good exercise to convert it yourself- give it a try! :D

avatar image Grady · Jul 02, 2011 at 05:25 AM 0
Share

errrr... i don't really know anything about c# though.... :/

avatar image testure · Jul 02, 2011 at 06:53 AM 0
Share

It's pretty easy.. Just remember that c# is strictly typed, so functions require a return type. I remember seeing a thread on the unity forums that described all of the differences, but I don't have the link handy.

avatar image Bunny83 · Oct 18, 2015 at 01:31 AM 3
Share

@Skatola
@ExCx

Well, since Unity has removed the ability to create a shader at runtime you have to use a shader that's in your project. However it is possible to still ship the shader within the script file and have it extracted automatically when used for the first time in the editor:

     private void Awake()
     {
         DontDestroyOnLoad(this);
         m_Instance = this;
         m_$$anonymous$$aterial = Resources.Load<$$anonymous$$aterial>("Plane_No_zTest");
 #if UNITY_EDITOR
         if (m_$$anonymous$$aterial == 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 One$$anonymous$$inusSrcAlpha ZWrite Off Cull Off Fog { $$anonymous$$ode 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 $$anonymous$$aterial(s);
             mat.name = "Plane_No_zTest";
             UnityEditor.AssetDatabase.CreateAsset(mat, "Assets/Resources/Plane_No_zTest.mat");
             m_$$anonymous$$aterial = mat;
 
         }
 #endif
     }


The "UNITY_EDITOR" section within the Awake callback will only be included and executed when run inside the editor. All it does is saving the shader source code into a .shader file inside the Resources folder if it doesn't exist yet and also create a $$anonymous$$aterial asset. It also forces an asset reload so the shader becomes available. If the $$anonymous$$aterial exists already, nothing special happens. The normal runtime code just uses Resources.Load to get the material reference.

It's not very as nice as the old way, but there's no way around creating an actual shader asset in the project.

avatar image waxx Bunny83 · Feb 22, 2016 at 01:53 PM 0
Share

Any idea why the quads are not rendered at all (material loads fine) under Unity 5.2.1?

Show more comments
avatar image
1

Answer by anwille_555 · Jul 30, 2017 at 03:35 PM

Any idea how to implement AutoFade to Scene Management in Unity?

Comment
Add comment · 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
-1

Answer by Justin Warner · May 22, 2011 at 01:24 AM

http://www.tildee.com/0kCzlg&m=1

http://www.google.com/search?client=opera&rls=en&q=transitions+unity3d&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest

Have fun! =)

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 robbgray · Jul 10, 2014 at 08:30 PM 0
Share

This is the second highest link result.

avatar image
0

Answer by trelobyte · Mar 12, 2014 at 06:25 PM

ok so the new version 4.3.4 seems that has killed the GL class on unity free...is there any alternatives to fading in and out of a scene ?

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 Trusty · Jun 16, 2014 at 10:39 AM 0
Share

I just add it to my project and it's still works fine ( using unity n 4.3.4f1, no pro ).

avatar image
0

Answer by Propagant · Sep 15, 2015 at 10:58 AM

I made easiest method of AutoFade in unity by Unity Game engine, UI elemets etc... You can free download it here... Fully explained and showed. Enjoy and thanks, if any opinions, just write. AutoFadeAlternative by matt


autofadealternative.zip (3.8 kB)
autofadealternative.zip (3.8 kB)
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 shaneparsons · Aug 04, 2016 at 04:35 PM 0
Share

Where exactly is it "fully explained and showed"?

  • 1
  • 2
  • ›

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

47 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 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

Transition a camera from ortographic to perspective mode (Trombone effect) 2 Answers

Can someone explain Mecanim transition options? 1 Answer

Day/Night cycle transition help? 2 Answers

Animating Transitions for GUI Results 0 Answers

Animation stransition problem 0 Answers


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