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
0
Question by hijinxbassist · Apr 21, 2012 at 01:18 AM · alphafadedraw texture

Is there a simple way of fading a Texture2D

Hi,

Is there a simple way of fading a Texture2D used in gui? Something like

 Fade(Texture2Dcopy , fadeTime, "In");    
 //Fades a Texture2D copy over the course of fadeTime, using a specific type of fade

Im not using a GUITexture. I know how to set up the function..but not sure how to change the actual alpha of the texture.

Any thoughts?

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
Best Answer

Answer by aldonaletto · Apr 21, 2012 at 02:22 AM

You can control the texture alpha with GUI.color.a:

var texture: Texture2D; // the texture to draw

private var alpha: float = 1.0; private var fading = false;

function Fade(fadeTime: float, in: boolean){ if (fading) return; // aborts other calls to Fade when already fading fading = true; // starts fading var t: float = 0.0; while (t < 1.0){ t += Time.deltaTime/fadeTime; alpha = Mathf.Clamp01(in? t : 1-t); // copy t or 1-t to alpha yield; // continue next frame } fading = false; // fading ended }

function OnGUI(){ GUI.color.a = alpha; // alpha ramps 0->1 or 1->0 when fading GUI.DrawTexture(Rect(...), texture); // draw the texture GUI.color.a = 1.0; // restore alpha for other GUI elements, if any ... // other GUI elements } If you want to fade to some color, draw a colorful mask after the main texture and control its alpha:

var mask: Texture2D; // drag the mask here...

function OnGUI(){ GUI.DrawTexture(Rect(...), texture); // draw the texture GUI.color.a = alpha; // alpha ramps 0->1 or 1->0 when fading GUI.DrawTexture(Rect(...), mask); // draw the mask GUI.color.a = 1.0; // restore alpha for other GUI elements, if any ... // other GUI elements } NOTE 1: Call Fade(time, true) to fade in and Fade(time, false) to fade out;
NOTE 2: When fading to a color, the logic is reversed - Fade(time, false) fades in and Fade(time, true) fades out.

Comment
Add comment · Show 7 · 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 AlucardJay · Apr 21, 2012 at 02:24 AM 0
Share

This is from the 3D Buzz tutorial (game manager). Fades in and out the logo when the player is idle.

 GUI.color = Color(1, 1, 1, 1 - logoFadeInTimer / logoFadeInDelay); // set alpha to 1 - [number] (fade in)
 
 GUI.color = Color(1, 1, 1, logoFadeOutTimer / logoFadeOutDelay); // fade out
 

So you could compare from the script in the tutorial :

 private var logoFadeInDelay : float = 3.0;
 private var logoFadeOutDelay : float = 0.5;
 private var logoFadeInTimer : float = 3.0;
 private var logoFadeOutTimer : float = 0.5;
 private var logoTexture : Texture;
         
         if (showLogo)
         {
             if (logoFadeInTimer > 0)
             {
                 logoFadeInTimer -= Time.deltaTime;
                 GUI.color = Color(1, 1, 1, 1 - logoFadeInTimer / logoFadeInDelay);
                 GUI.DrawTexture(Rect(Screen.width - 316, Screen.height - 134, 316, 129), logoTexture);
             }
             else
             {
                 GUI.color = Color.white;
                 GUI.DrawTexture(Rect(Screen.width - 316, Screen.height - 134, 316, 129), logoTexture);
             }
         }
         else
         {
             if (logoFadeOutTimer > 0)
             {
                 logoFadeOutTimer -= Time.deltaTime;
                 GUI.color = Color(1, 1, 1, logoFadeOutTimer / logoFadeOutDelay);
                 GUI.DrawTexture(Rect(Screen.width - 316, Screen.height - 134, 316, 129), logoTexture);
             }
         }
         
avatar image AlucardJay · Apr 21, 2012 at 02:25 AM 1
Share

Simultaneous post! Follow Aldo's advice =]

avatar image hijinxbassist · Apr 21, 2012 at 02:40 AM 0
Share

@aldonaletto Beautiful! Just one question, in? is to make function var(in) usable? Ive never seen ?variable before. Can you enlighten me to this use of "?var". As for my script, im gonna throw a switch(fadeCase) ((Thus the "In")) in there so i can deter$$anonymous$$e whether the texture is fading in or out. I suppose a boolean is just as good with 2 cases, if i added a third case id have to disregard the bool method.

Ill post my script when im done, its an update to the "Custom 2D Pointer" script using OnGUI ins$$anonymous$$d of GUITexture.

avatar image hijinxbassist · Apr 21, 2012 at 02:46 AM 0
Share

Lol! Thanks, anyway!

avatar image aldonaletto · Apr 21, 2012 at 03:49 AM 2
Share

This is one of the good inventions of the C language: the question mark selects one of the two following alternatives (first if true, second if false) - it's like a ultra compact IF.
You're right: switch is the best choice if you intend to have more than two alternatives - something like this:

  while (t < 1.0){
    t += Time.deltaTime/fadeTime;
    switch (inOut){
      case "In": 
        alpha = $$anonymous$$athf.Clamp01(t); 
        break;
      case "Out": 
        alpha = $$anonymous$$athf.Clamp01(1-t); 
        break;
      case "OtherThing":
        ...
    }
    yield; // continue next frame
  }
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Fade Shader- One texture fades in, the other fades out 2 Answers

Fading Objects does not work (Unity 5) 0 Answers

Fading all but single instance of a button 1 Answer

How to change the alpha element of particles/additive color? 1 Answer

Fading out a transparent material 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