Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by magnetic992 · Mar 03, 2014 at 10:03 PM · c#graphicsspritesalphafade

Unity2D Sprite Fade In and Out

I'm trying to make a script using the new Unity2D sprites to have a level title fade out OR have my tutorial UI sprites fade in. I have gotten far enough to edit the Alpha via the code, but I can only change the alpha to a specific amount instead of having it gradually edit the alpha up and down over time. Can anyone help me out. Heres my code.

 using UnityEngine;
 using System.Collections;
 
 public class AlphaFade : MonoBehaviour {
 
 public float fadeSpeed = 1f;
 public bool fadeIn = true;
 public SpriteRenderer text;
     
     // Use this for initialization
     
     // Invisible on Awake
     void Awake() {
         text.color = new Color(1f,1f,1f,0f);
     }
     
     // Update is called once per frame
     void Update () {
         float Fade = 0f;
         if (fadeIn) {
                 text.color = new Color(1f,1f,1f,Fade=+.1f);
         }
     }
 }
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
11

Answer by fonko · May 11, 2018 at 03:34 AM

you should use coroutines for this, something like

  void Update ()
  {
      if(Input.GetKeyUp(KeyCode.T))
      {
          StartCoroutine(FadeTo(0.0f, 1.0f));
      }
      if(Input.GetKeyUp(KeyCode.F))
      {
          StartCoroutine(FadeTo(1.0f, 1.0f));
      }
  }
  
  IEnumerator FadeTo(float aValue, float aTime)
  {
      float alpha = transform.renderer.material.color.a;
      for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
      {
          Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,aValue,t));
          transform.renderer.material.color = newColor;
          yield return null;
      }
  }
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 fonko · May 11, 2018 at 03:38 AM 2
Share

sorry it should be

     IEnumerator FadeTo(float aValue, float aTime)
     {
         float alpha = transform.GetComponent<SpriteRenderer>().material.color.a;
         for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
         {
             Color newColor = new Color(1, 1, 1, $$anonymous$$athf.Lerp(alpha, aValue, t));
             transform.GetComponent<SpriteRenderer>().material.color = newColor;
             yield return null;
         }
     }
 }
avatar image
4

Answer by treasgu · Jul 28, 2014 at 09:12 AM

I fix the code adding ref before fadeSpeed

float Fade = Mathf.SmoothDamp(0f,1f,ref fadeSpeed,fadeTime);

But I recommend use SmoothStep for fade effect.

        public float minimum = 0.0f;
         public float maximum = 1f;
         public float duration = 5.0f;
         private float startTime;
         public SpriteRenderer sprite;
         void Start() {
             startTime = Time.time;
         }
         void Update() {
             float t = (Time.time - startTime) / duration;
             sprite.color = new Color(1f,1f,1f,Mathf.SmoothStep(minimum, maximum, t));        }

Sorry for my english too.

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 Pioneer.Yoon · Dec 05, 2015 at 06:06 PM 0
Share

Thx treasgu!!!

I didn't know that ...

You have solved my problem!! :D

so can you tell me that why do recommend using the SmoothStep???

avatar image shieldgenerator7 · Mar 17, 2016 at 03:53 PM 0
Share

Thanks! This worked for me

avatar image Atliinux · Feb 25, 2017 at 06:01 AM 0
Share

This does not work for me though. I have an if statement that wraps around the fading code like so: if (boolean == true) { float t = (Time.time - startTime) / duration; sprite.color = new Color(1f,1f,1f,$$anonymous$$athf.SmoothStep($$anonymous$$imum, maximum, t)); }

avatar image
0

Answer by amilo1003 · Mar 03, 2014 at 10:56 PM

hi, try using Mathf.SmoothDamp. http://docs.unity3d.com/Documentation/ScriptReference/Mathf.SmoothDamp.html

Comment
Add comment · Show 2 · 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 magnetic992 · Mar 03, 2014 at 11:19 PM 0
Share

$$anonymous$$y new code now has some errors.

 using UnityEngine;
 using System.Collections;
 
 public class AlphaFade : $$anonymous$$onoBehaviour {
 
 public float fadeSpeed = 1f;
 public float fadeTime = 1f;
 public bool fadeIn = true;
 public SpriteRenderer text;
     
     // Update is called once per frame
     void Update () {
         
         
         if (fadeIn) {
             float Fade = $$anonymous$$athf.SmoothDamp(0f,1f,fadeSpeed,fadeTime);
             text.color = new Color(1f,1f,1f,Fade);
         }
         
         if (!fadeIn) {
             float Fade = $$anonymous$$athf.SmoothDamp(1f,0f,fadeSpeed,fadeTime);
             text.color = new Color(1f,1f,1f,Fade);
         }
     }
 }

I need a ref float for my third argument in both SmoothDamp statements, what is it referring to?

avatar image amilo1003 · Mar 03, 2014 at 11:48 PM 0
Share

you have to declare "fade" float :)

 public float fade;

sorry about my english

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

26 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

Related Questions

Fading out doesn't fade out, but instantly goes from black to clear 0 Answers

Fading Objects does not work (Unity 5) 0 Answers

Smoothing Directionally between three materials 1 Answer

Object see-through when rendering mode is set to Fade 1 Answer

Multiple Cars not working 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