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
1
Question by code-blep · Aug 24, 2012 at 04:49 PM · shaderskyboxfade

Fade SkyBox to Black

I'm trying to change the color tint on my Skybox so that it fades to black. However it does not manage to fade the SkyBox completely black.

While researching I have found the SkyBox Blended script ( http://wiki.unity3d.com/index.php?title=SkyboxBlended ), but it seems a lot to do just to fade to black. Also I have no experience of Shaders and tried to implement it with little luck, but will give it another go if it's the only way.

Is this pretty much the only other way, or does anyone have an alternate suggestion.

Thanks

Paul

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

3 Replies

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

Answer by code-blep · Aug 24, 2012 at 06:45 PM

Right, so I've noticed that Game Development seems to be a lot of 'Smoke and Mirrors'. I was only looking for a smooth transition from one skybox to another with a simple fade to black and then fade in the new skybox.

So I thought why not surround my scene with some flat planes that create a box with all 6 sides. I then used a transparent texture set to black.

So when I changed the skybox on a keypress it simply fades in the black box, changes the skybox when it's no longer visible, and then fade the box out again.... and it works great! I am using iTween for the fade by the way.

I'm sure there is probably a more elegant way to do this but I have only been using unity for a few months, but for what it's worth Here is my script. Enjoy:

 //Choose your skyboxes in the inspector
 var StartSkyBox:Material;
 var mat1:Material;
 var mat2:Material;
 var mat3:Material;
 var mat4:Material;
 var mat5:Material;
 
 //This is the parent of the giant box created using 6 planes with a black transparent texture. Select it in the inspector.
 var SkyBoxMaskObject : GameObject;
 
 //Default Skybox to start with
 RenderSettings.skybox=StartSkyBox;
 
 
 //Functions for each skybox
 function swapskybox1()
     {
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":1,"time":1});
         yield WaitForSeconds(1);
         RenderSettings.skybox=mat1;
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":0,"time":1});
     }
 
 
 function swapskybox2()
     {
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":1,"time":1});
         yield WaitForSeconds(1);
         RenderSettings.skybox=mat2;
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":0,"time":1});
     }
 
         
 function swapskybox3()
     {
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":1,"time":1});
         yield WaitForSeconds(1);
         RenderSettings.skybox=mat3;
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":0,"time":1});
     }
     
     
 function swapskybox4()
     {
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":1,"time":1});
         yield WaitForSeconds(1);
         RenderSettings.skybox=mat4;
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":0,"time":1});
     }
     
     
 function swapskybox5()
     {
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":1,"time":1});
         yield WaitForSeconds(1);
         RenderSettings.skybox=mat5;
         iTween.FadeTo(SkyBoxMaskObject,{"alpha":0,"time":1});
     }
 
 
 function Update(){
 
 
             if(Input.GetKeyDown(KeyCode.Keypad1)) {
                 Debug.Log("Keypad 1 pressed");                
 
                 //Call the change skybox function
                 swapskybox1();
                 
                 }
                 
                 
             if(Input.GetKeyDown(KeyCode.Keypad2)) {
                 Debug.Log("Keypad 2 pressed");
                 
                 //Call the change skybox function
                 swapskybox2();
                 
                 }
                 
             if(Input.GetKeyDown(KeyCode.Keypad3)) {
                 Debug.Log("Keypad 3 pressed");
                 
                 //Call the change skybox function
                 swapskybox3();
                 
                 }
                 
             if(Input.GetKeyDown(KeyCode.Keypad4)) {
                 Debug.Log("Keypad 4 pressed");
                 
                 //Call the change skybox function
                 swapskybox4();
                 
                 }
                 
             if(Input.GetKeyDown(KeyCode.Keypad5)) {
                 Debug.Log("Keypad 5 pressed");
                 
                 //Call the change skybox function
                 swapskybox5();
                 
                 }
 
 
 } 
Comment
Add comment · Show 5 · 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 fafase · Aug 24, 2012 at 07:14 PM 1
Share

You could have only one function and an array of materials:

 var index:int;
 var mat:$$anonymous$$aterial[]=new $$anonymous$$aterial[6];
 function Update(){
      if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypad1)) {
       index = 0;          
       Swapskybox(index);
       }
 }
 function Swapskybox(i:int){
    iTween.FadeTo(SkyBox$$anonymous$$askObject,{"alpha":1,"time":1});
    yield WaitForSeconds(1);
    RenderSettings.skybox=mat[i];
    iTween.FadeTo(SkyBox$$anonymous$$askObject,{"alpha":0,"time":1});
 }
avatar image code-blep · Aug 24, 2012 at 07:18 PM 0
Share

That's neat! A number of new ways to do stuff. Thanks fafase!

avatar image code-blep · Aug 24, 2012 at 07:20 PM 0
Share

Looking at it, it will be much easier to add or remove new skyboxes. Yes?

avatar image fafase · Aug 24, 2012 at 07:30 PM 1
Share

The main advantage is 20 lines of codes less in your script and some free space (not a lot ok...) in memory as the compiler does not need allocate space for each function. Also, if the function is used often, the processor will cache the address for faster access, if you use a different function each time, the address can get replaced in the cache and a cache miss occurs and a little loss of time. Well, this is deep hardware issue...

Concerning changing the material, I am not sure I fully understand what you mean but it is not going to change much, it is just neater. Using a built-in array you have in in the inspector for drag and drop. Changing them would go the same:

 mat[5] = new$$anonymous$$aterial;

ins$$anonymous$$d of

 mat5 = new$$anonymous$$aterial;

If that is what you meant.

avatar image code-blep · Aug 24, 2012 at 09:43 PM 0
Share

Yep that's it fafase. Great stuff! It's opened my eyes to other more efficient ways.

avatar image
1

Answer by fafase · Aug 24, 2012 at 05:08 PM

You could have different skyboxes for instance four of them and you can use this http://wiki.unity3d.com/index.php?title=SkyboxBlended to pass from one to the other.

Well, if you want an easy way but not the best maybe...

 var sky:Skybox;
 var skies:Material[6];
 var i:int =0;
 function Start () {
 sky= gameObject.GetComponent(Skybox);
 }
 
 function Update () {
   if(Input.GetKeyDown(KeyCode.Space)){
     RenderSettings.skybox = skies[i];
     i++;
     if(i==6)i=0;
   }
 }

Consider you have the same skybox 6 times (...)from balck to white to black with little changing between each. Then when pressing space you swap from one to the next. At the end you get back to the first you did a full day. If your game has building you get in, you could do that when you are inside so the change won't be brutal to the gameplay.

Comment
Add comment · Show 5 · 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 code-blep · Aug 24, 2012 at 05:22 PM 0
Share

I have already mentioned that I looked at this in the original question fafase, but thanks anyway :)

avatar image fafase · Aug 24, 2012 at 05:25 PM 0
Share

oops my bad...

avatar image code-blep · Aug 24, 2012 at 05:34 PM 0
Share

lol! No problem ;)

avatar image fafase · Aug 24, 2012 at 05:38 PM 0
Share

I also updated my answer with another way n case you need a scripting way.

avatar image code-blep · Aug 24, 2012 at 06:55 PM 0
Share

Thanks fafase! I'll up vote your reply as it is a solution, but I needed something a little smoother ;)

avatar image
0

Answer by nenorse · May 23, 2014 at 03:09 PM

If you just want to turn something to black - create a texture, and alter the alpha of it, like this

     public Texture blackBackground;
     
         public float Speed = 0.1f;
         private Rect backgroundRect = new Rect(0,0,Screen.width,Screen.height);
         private float alphaBackground = 0;
     
         public void OnGUI()
         {
             GUI.depth = 0;    //-- on top
             alphaBackground += (Speed / 10f)* Time.deltaTime;
             GUI.color = new Color(0,0,0, alphaBackground);
             GUI.DrawTexture (backgroundRect, blackBackground);
     }


Remember to assign the blackBackground to a black texture (png) in the IDE. (look in the script you have created, after inserting this code)..

alt text

Also note - if you are using OnGUI for mobile devices, be careful as its expensive. It will work, just watch out... ;)


blackunity.png (2.0 kB)
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

9 People are following this question.

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

Related Questions

help with fade/cutout skybox shader 0 Answers

Skybox blending 2 Answers

Unity 5 Skybox not Rotating 3 Answers

Represent Land 3D Area End 0 Answers

Transparent/Specular Shader - I want alpha to control spec, not transparency 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