• 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
Question by Story Specialist · May 19, 2010 at 11:21 AM · javascriptguicrossfadedrawtexture

Fade between textures in the same script?

Another issue I can't seem to wrap my head around... Similar to the one I had earlier.

Issue: I want to crossfade from one DrawTexture to another.

My system: My system works with an XML parser. Basically, all the GUI items are in the same script.

For instance, my DrawTexture is being drawn in the GUI, like so:

GUI.DrawTexture (Rect (0, 0, 1280, 1024), textureBackground, ScaleMode.StretchToFill);

but changed by this command when the XML parser detects the tag :

textureBackground =  Resources.Load("Backgrounds/" + input.change,Texture2D);

the "input.change" is the name of the texture (which I state in the XML file like so: example01.

So what I'd like to do is sort of crossfade into the next texture as soon as I give that command.

My idea was, in layman's terms, to first make a second DrawTexture, which assumes the new texture when the command is given. Then I'd fade the alpha up to completely opaque (in say 2 seconds time), set the first DrawTexture to match the second DrawTexture, and then reset the second one's alpha back to 0, so the background image is now set. And of course repeat the process when the texture changes again.

I realize this is probably very amateuristic, but looking around UnityAnswers, I can't seem to find another way of doing it. All other Answers I find mostly use external script, and I can't, as I need the depth to stay the same (it is a background after all, there are many more GUI elements being drawn on top of it). Nor can I seem to get it working.

Please help me out, I've tried to do it myself but I'm just too much of a beginner/bad scripter to grasp this. Getting to the end of my project time, as well...

-Veliremus

Comment

People who like this

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

Answer by Molix · May 19, 2010 at 01:39 PM

During the crossfade period you'll actually be drawing two textures, so just give yourself another reference and set a timer, e.g.:

var textureBackground : Texture2D; var previousBackground : Texture2D; public var crossfadeDuration : float = 2.0; var crossfadeEnd : float;

function OnGUI() { var oldColor : Color = GUI.color; var alpha : float = 1;

if( crossfadeEnd > Time.time ) { alpha = 1.0 - (( crossfadeEnd - Time.time ) / crossfadeDuration); if( previousBackground != null ) { GUI.color = Color( 1, 1, 1, 1 - alpha ); GUI.DrawTexture (Rect (0, 0, 1280, 1024), previousBackground, ScaleMode.StretchToFill); }
}

GUI.color = Color( 1, 1, 1, alpha ); GUI.DrawTexture (Rect (0, 0, 1280, 1024), textureBackground, ScaleMode.StretchToFill);

GUI.color = oldColor; // other controls that are in front of backgrounds }

function NewBackground() { // do your loading, etc, and start the timer, e.g. previousBackground = textureBackground; textureBackground = Resources.Load("Backgrounds/" + input.change,Texture2D); crossfadeEnd = Time.time + crossfadeDuration; }

(note: this is off-the-cuff, and I don't usually use JS, but it should get you in the right direction).

Comment
Story Specialist
Lo0NuhtiK
Eli-Davis

People who like this

3 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 Story Specialist · May 19, 2010 at 03:57 PM 0
Share

Dude, you are a lifesaver! Had to correct a spelling error, as you used both crossfadeEnd and crossFadeEnd (capital letter on Fade), but that was easily discovered in the debug :). Thank you so much, it works like a charm. =D

avatar image Molix · May 19, 2010 at 04:26 PM 0
Share

You're welcome; I'm glad it works! (I fixed the crossfadeEnd spelling now in case others find it useful).

avatar image Donilias · Jan 26, 2012 at 04:24 PM 0
Share

Sorry i'm to late, But i want the same one. But this isn't work. First i got an error. The error was input, so i changed input into Input. So then i run the game, but nothing happends. Someone can tell me what to do?

avatar image

Answer by Juan 1 · May 06, 2011 at 05:51 AM

Is there a way to do this using a 3D plane with a texture instead of GUI elements?

Comment

People who like this

0 Show 0 · 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

Answer by ellens · Sep 06, 2012 at 08:21 AM

You can do this easily with a custom shader and a simple script: http://www.sundh.com/blog/2012/09/real-time-blend-2-textures-in-unity/

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[Closed]GUI.DrawTexture inside GUI.DrawTexture 1 Answer

Texture on center bottom of screen 3 Answers

Setting Scroll View Width GUILayout 1 Answer

GUI.Drawtexture Overlaping Other GUI.Drawtexture. 1 Answer

How to change rect size? 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