Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by UltimateWalrus · Jun 19, 2010 at 01:43 AM · guifade

Fade in from black

How can I make my scene "fade in" from black?

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

4 Replies

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

Answer by qJake · Jun 19, 2010 at 01:48 AM

http://www.unifycommunity.com/wiki/index.php?title=FadeInOut

Googling "Unity Fade In/Out" would get you a lot of resources, and this method I linked to is typically the most common (stretching a 1px black texture across the screen and changing its alpha value).

Edit: I would actually use this C# script on that page, as it seems like the best one:

http://www.unifycommunity.com/wiki/index.php?title=FadeInOut#Another_Fade_Script_in_C.23

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 UltimateWalrus · Jun 19, 2010 at 06:53 AM 0
Share

Thanks!! That's exactly what I needed.

avatar image
5

Answer by Eric5h5 · Jun 19, 2010 at 02:20 AM

http://www.unifycommunity.com/wiki/index.php?title=Fade

General purpose fading routines; I use that in most of my projects.

Comment
Add comment · Show 3 · 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 qJake · Jun 19, 2010 at 06:58 AM 3
Share

Psh, of course, you're the one that wrote it! Haha. :)

avatar image Eric5h5 · Jun 19, 2010 at 03:51 PM 1
Share

Since I was using it all the time I figured I'd put it on the wiki....

avatar image POLYGAMe · Feb 08, 2012 at 02:55 AM 0
Share

Eric, you are my God ;-) Helped me with soooo many issues on my racer!!!

avatar image
0

Answer by Xorxor · Mar 17, 2015 at 01:38 AM

Here's the updated link

http://wiki.unity3d.com/index.php/Fade

And if you're trying to fade in from OVR 4.4 checkout OVR/Scripts/Util/OVRScreenFade.cs. Here I've modded it to control "manually".

 using UnityEngine;
 
 public class OVRScreenFadeCustom : MonoBehaviour
 {
     public float alpha = 1f;
     
     private Shader fadeShader = null;
     private Material fadeMaterial = null;
 
     void Awake()
     {
         fadeMaterial = (fadeShader != null) ? new Material(fadeShader) : new Material(Shader.Find("Transparent/Diffuse"));
     }
     
     void OnDestroy()
     {
         if (fadeMaterial != null)
         {
             Destroy(fadeMaterial);
         }
     }
 
     void OnPostRender()
     {
         if (alpha != 0)
         {
             fadeMaterial.color = new Color(0f, 0f, 0f, alpha);
 
             fadeMaterial.SetPass(0);
             GL.PushMatrix();
             GL.LoadOrtho();
             GL.Color(fadeMaterial.color);
             GL.Begin(GL.QUADS);
             GL.Vertex3(0f, 0f, -12f);
             GL.Vertex3(0f, 1f, -12f);
             GL.Vertex3(1f, 1f, -12f);
             GL.Vertex3(1f, 0f, -12f);
             GL.End();
             GL.PopMatrix();
         }
     }
 }
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
0

Answer by ristophonics · Oct 22, 2018 at 06:22 PM

Add:

     public void FadeIn()
     {
         StartCoroutine(Fade(1, 0));
     }

to the code below:

 Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
 
 Licensed under the Oculus SDK License Version 3.4.1 (the "License");
 you may not use the Oculus SDK except in compliance with the License,
 which is provided at the time of installation or download, or which
 otherwise accompanies this software in either electronic or hard copy form.
 
 You may obtain a copy of the License at
 
 https://developer.oculus.com/licenses/sdk-3.4.1
 
 Unless required by applicable law or agreed to in writing, the Oculus SDK
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 
 ************************************************************************************/
 
 using UnityEngine;
 using System.Collections; // required for Coroutines
 
 /// <summary>
 /// Fades the screen from black after a new scene is loaded. Fade can also be controlled mid-scene using SetUIFade and SetFadeLevel
 /// </summary>
 public class OVRScreenFade : MonoBehaviour
 {
     [Tooltip("Fade duration")]
     public float fadeTime = 2.0f;
 
     [Tooltip("Screen color at maximum fade")]
     public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
 
     public bool fadeOnStart = true;
 
     /// <summary>
     /// The render queue used by the fade mesh. Reduce this if you need to render on top of it.
     /// </summary>
     public int renderQueue = 5000;
 
     private float uiFadeAlpha = 0;
 
     private MeshRenderer fadeRenderer;
     private MeshFilter fadeMesh;
     private Material fadeMaterial = null;
     private bool isFading = false;
 
     public float currentAlpha { get; private set; }
 
     void Awake()
     {
         // create the fade material
         fadeMaterial = new Material(Shader.Find("Oculus/Unlit Transparent Color"));
         fadeMesh = gameObject.AddComponent<MeshFilter>();
         fadeRenderer = gameObject.AddComponent<MeshRenderer>();
 
         var mesh = new Mesh();
         fadeMesh.mesh = mesh;
 
         Vector3[] vertices = new Vector3[4];
 
         float width = 2f;
         float height = 2f;
         float depth = 1f;
 
         vertices[0] = new Vector3(-width, -height, depth);
         vertices[1] = new Vector3(width, -height, depth);
         vertices[2] = new Vector3(-width, height, depth);
         vertices[3] = new Vector3(width, height, depth);
 
         mesh.vertices = vertices;
 
         int[] tri = new int[6];
 
         tri[0] = 0;
         tri[1] = 2;
         tri[2] = 1;
 
         tri[3] = 2;
         tri[4] = 3;
         tri[5] = 1;
 
         mesh.triangles = tri;
 
         Vector3[] normals = new Vector3[4];
 
         normals[0] = -Vector3.forward;
         normals[1] = -Vector3.forward;
         normals[2] = -Vector3.forward;
         normals[3] = -Vector3.forward;
 
         mesh.normals = normals;
 
         Vector2[] uv = new Vector2[4];
 
         uv[0] = new Vector2(0, 0);
         uv[1] = new Vector2(1, 0);
         uv[2] = new Vector2(0, 1);
         uv[3] = new Vector2(1, 1);
 
         mesh.uv = uv;
 
         SetFadeLevel(0);
     }
 
     /// <summary>
     /// Start a fade out
     /// </summary>
     public void FadeOut()
     {
         StartCoroutine(Fade(0,1));
     }
 
 
     /// <summary>
     /// Starts a fade in when a new level is loaded
     /// </summary>
     void OnLevelFinishedLoading(int level)
     {
         StartCoroutine(Fade(1,0));
     }
 
     /// <summary>
     /// Automatically starts a fade in
     /// </summary>
     void Start()
     {
         if (fadeOnStart)
         {
             StartCoroutine(Fade(1,0));
         }
     }
 
     void OnEnable()
     {
         if (!fadeOnStart)
         {
             SetFadeLevel(0);
         }
     }
 
     /// <summary>
     /// Cleans up the fade material
     /// </summary>
     void OnDestroy()
     {
         if (fadeRenderer != null)
             Destroy(fadeRenderer);
         
         if (fadeMaterial != null)
             Destroy(fadeMaterial);
         
         if (fadeMesh != null)
             Destroy(fadeMesh);
     }
 
     /// <summary>
     /// Set the UI fade level - fade due to UI in foreground
     /// </summary>
     public void SetUIFade(float level)
     {
         uiFadeAlpha = Mathf.Clamp01(level);
         SetMaterialAlpha();
     }
     /// <summary>
     /// Override current fade level
     /// </summary>
     /// <param name="level"></param>
     public void SetFadeLevel(float level)
     {
         currentAlpha = level;
         SetMaterialAlpha();
     }
 
     /// <summary>
     /// Fades alpha from 1.0 to 0.0
     /// </summary>
     IEnumerator Fade(float startAlpha, float endAlpha)
     {
         float elapsedTime = 0.0f;
         while (elapsedTime < fadeTime)
         {
             elapsedTime += Time.deltaTime;
             currentAlpha = Mathf.Lerp(startAlpha, endAlpha, Mathf.Clamp01(elapsedTime / fadeTime));
             SetMaterialAlpha();
             yield return new WaitForEndOfFrame();
         }
     }
 
     /// <summary>
     /// Update material alpha. UI fade and the current fade due to fade in/out animations (or explicit control)
     /// both affect the fade. (The max is taken) 
     /// </summary>
     private void SetMaterialAlpha()
     {
         Color color = fadeColor;
         color.a = Mathf.Max(currentAlpha, uiFadeAlpha);
         isFading = color.a > 0;
         if (fadeMaterial != null)
         {
             fadeMaterial.color = color;
             fadeMaterial.renderQueue = renderQueue;
             fadeRenderer.material = fadeMaterial;
             fadeRenderer.enabled = isFading;
         }
     }
 }
 



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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Display GUI when kill count reaches 5 1 Answer

FadeIn/FadeOut the GUI 2 Answers

GUI buttons glow/flash/fade between textures 0 Answers

fade a button 1 Answer

Who Killed Who - Scrolling GUI Text 2 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