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 /
This question was closed Dec 06, 2012 at 11:19 PM by burnpsy for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by burnpsy · Feb 24, 2012 at 10:25 AM · c#texture2dimage

Making Images Fade In and Out

Basically, I'm working with code that changes a variable to set what image is being displayed based on a certain prompt.

That's working fine, but I want to make it so that, when I switch to a new image, the old image fades out and the new image fades in.

But I don't know where to start. How do I make stuff fade in and fade out?

Comment
Add comment · Show 7
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 Eric5h5 · Feb 24, 2012 at 12:34 PM 0
Share

It depends on what the "image" actually is. Please elaborate.

avatar image CHPedersen · Feb 24, 2012 at 01:08 PM 0
Share

I suspect he's talking about textures displayed in the GUI, and he wants to fade between them like slides in a powerpoint presentation, or something. I think the easiest way to do that is to use GUITextures; they come with a color whose alpha you can gradually decrease and increase when you fade them in and out. I saw somewhere that people think GUI.DrawTexture is more convenient, but I couldn't confirm whether that method allows you to change the texture's alpha in an efficient way. I don't think it does.

avatar image Eric5h5 · Feb 24, 2012 at 01:14 PM 0
Share

Yes, but what kind of GUI...OnGUI code, GUITexture, texture on a plane....

avatar image FLASHDENMARK · Feb 24, 2012 at 01:15 PM 0
Share

@Christian Efficiency is subjective to an extend I guess, but you can easily change the alpha value using GUI.DrawTexture. You will just have to say: "GUI.color.a = value", before rendering the texture.

avatar image CHPedersen · Feb 24, 2012 at 01:22 PM 0
Share

@OrangeLightning: Oh, right. :) I'd forgotten about GUI.color. Yeah, I guess you could use that.

Show more comments

5 Replies

  • Sort: 
avatar image
0

Answer by pedronaroga · Feb 25, 2012 at 03:11 AM

There are two ways I can think of doing that. The first one, you'll do it manually. The other one, which I would recommend, would be using iTween (download here), and setting the alpha value of the images using iTween.ValueTo. There goes an example:

 class FadeMaterials : MonoBehaviour {

     public void FadeOut() {

         iTween.ValueTo(gameObject, iTween.Hash(
             "from", 1.0f, "to", 0.0f,
             "time", 3f, "easetype", "linear",
             "onupdate", "setAlpha");

     }

     public void FadeIn() {

         iTween.ValueTo(gameObject, iTween.Hash(
             "from", 0f, "to", 1f,
             "time", 3f, "easetype", "linear",
             "onupdate", "setAlpha");

     }

     public void setAlpha(float newAlpha) {

         foreach (Material mObj in renderer.materials) {

              mObj.color = new Color(
                  mObj.color.r, mObj.color.g, 
                  mObj.color.b, newAlpha);

         }

     }
 
 }
 

Now, whenever you want to fade out a gameobject, just call

 gameobject.GetComponent<FadeMaterials>().FadeOut();

Obviously, to fade in another gameobject, just call:

 gameobject.GetComponent<FadeMaterials>().FadeIn();

You can change the parameter "time" on the iTween calls, to change the fading time duration. You can also change the "easetype", to any acceptable iTween easetype values. You can find them here.

Please keep in mind that not all shaders support alpha. I'd suggest changing your materials' shaders to their equivalents in the Transparent section. For example, if you are using a Diffuse shader, change it to Transparent/Diffuse. If you are using a VertexLit shader, change it to Transparent/VertexLit.

Also, this will only work for 3D gameobjects in your scene. If you want to fade in/out items in your GUI, it'll be somewhat trickier, for you cannot change a texture alpha without using SetPixels (or so I think, I'm not sure).

I did not test the previous code. Just thought it'd point you in the right direction, even if theres an eventual sintax error.

Hope it helps.

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 burnpsy · Feb 25, 2012 at 04:35 AM 0
Share

If it can't alter the Texture2D stuff, which is what I need faded, that entire suggestion doesn't touch upon what I need.

avatar image
0

Answer by James Tima · Feb 25, 2012 at 03:10 AM

You Can play with alpha value to create effect fade in fade out

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 burnpsy · Feb 25, 2012 at 04:37 AM 0
Share

You just repeated what everyone else said, but with less information.

avatar image
0

Answer by devPhil · Feb 25, 2012 at 03:09 AM

Maybe this is what you're looking for?

http://answers.unity3d.com/questions/10511/change-alpha-on-individual-texture2ds.html

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 burnpsy · Feb 25, 2012 at 04:36 AM 0
Share

Draw$$anonymous$$e doesn't exist, apparently.

avatar image
0

Answer by kievar1983 · Feb 24, 2012 at 04:03 PM

i found this to be helpful.. it might do exactly what you are looking to do..

 public var theTexture : Texture2D;
 private var alpha : float = 0;
 var duration: float = 1;
 function Start(){
     yield FadeIn();
     yield FadeOut();
     yield NextLevel();
 }
 function FadeIn(){
 var d = 0.3f / duration;
     while( alpha < 1 ){ alpha += Time.deltaTime * d; yield; }
 }
 function FadeOut(){
 var d = 0.3f / duration;
     while( alpha > 0 ){ alpha -= Time.deltaTime * d; yield; }
 }
 function NextLevel(){
     yield FadeOut();
     Application.LoadLevel(1);
 }
 function OnGUI(){
     GUI.color = Color(1,1,1, alpha );
     GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), theTexture);
 }

if you aren't loading a level get rid of the next level function. This is also done in javascript.

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 burnpsy · Feb 25, 2012 at 01:57 AM 0
Share

I converted it to C# and this line:

GUI.color = Color(1, 1, 1, alphaLeft);

Got me this error:

error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

What now?

avatar image burnpsy · Feb 25, 2012 at 03:02 AM 0
Share

Fixed the issue, but it doesn't do anything.

avatar image kievar1983 · Feb 25, 2012 at 05:45 PM 0
Share

learn javascript.. it works cause i've used it for multiple faders of a 2d texture

avatar image
0

Answer by dentedpixel · Dec 06, 2012 at 09:07 PM

You can use LeanTween to fade an object, here is an example:

 var time:float = 2.0;
 var alphaVal:float = 0.0;
 LeanTween.alpha(gameObject, alphaVal, time);

Just make sure that the object has a shader that supports transparency! I would recommend Owl Labs Shaders: http://owlchemylabs.com/content/ (Unlit with Alpha);

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

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Multiple Cars not working 1 Answer

How to convert texture2D to image in C# 3 Answers

Distribute terrain in zones 3 Answers

How do I iterate over a large Texture2d quickly? 1 Answer

Selection Grid 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