A few simple questions

I am sure this stuff has been answered before, but I for the life of me can’t find it on the board, so here goes:

Simple question one: I have a plane being used as a billboard, with a texture on it. Essentially, I want to animate the opacity of this texture so the plane appears to do a simple fade in/out of black. What would be the easiest way to do this? (I am thinking of dropping the ambient light level to 0, and animating a directional light’s intensity, will this work or am I missing something obvious here?)

Simple question two: After animating this plane fading on over a one second period, staying fully lit for two an a half seconds, and then fading off over a half second, I would like Unity to load the file for the actual level (the plane is a splash screen, and is in it’s own level/file, called Level0; I want it to display and then go into the actual demo, which is in a file called Level1. Both levels are in the same project folder). Will Unity do this automatically, or is there something I need to do to make this happen? Attached is an image of my Project View.

59264--2161--$picture_1_245.png

With the animation of colour, you select the object, then rightclick on the timeline, which will give you some options.(make sure your in “animation” layout). You want to have a shader which supports transparency, and you position on time line, change the colour of the colour swatch-specifically the alpha slider at the bottom-then rightclick on timeline-add key with attribute>colour(from memory).

For your second question, both levels need to be in the build settings, , and something like

function Start(){
yield WaitForSeconds(4);
Application.Loadlevel(1);
}

Should cut it.
The 1 may need quotation marks around it, I can never remember until I test. Try that, I may have typo’ed

(Untested.)

AC

The easiest way to do screen fading stuff is to use a full-screen GUITexture, with a plain white texture (1 pixel big is fine). The scale should be 1,1,1 with 0 for all pixel inset values; that way it will always cover the entire screen at any resolution/aspect ratio. The reason the texture should be white is that way you can use the color on the GUITexture to set it to whatever you want, including black. This way it’s trivial to fade the screen to any color, not just black. With GUIElements, .5, rather than 1, is full value (this way color values can go from 0 to 200%). In the case you describe, set up the splash scene as you like, and do something like this as the script on the GUITexture:

function Start () {
	yield Fade(.5, 0, 1);  // Start, end, length in seconds
	yield WaitForSeconds(2.5);
	yield Fade(0, .5, .5);
	Application.LoadLevel("Level1");
}

function Fade (start : float, end : float, length : float) {
	for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
		guiTexture.color.a = Mathf.Lerp(start, end, i);
		yield;
	}
}

It might be nice to use DontDestroyOnLoad for the GUITexture; that way you can keep it around from level to level, and do a fade with the Fade function at any time, which you could call from other scripts. Whenever you leave it at 100% transparent, you might as well do guiTexture.enabled = false, so it’s not drawn anymore, and then re-enable it when you need to fade the screen again. The only downside to this technique is that any OnGUI stuff is always drawn on top.

–Eric

Oh right sorry I misread. Yeah do it like Eric said. Or like I said but keyframe the GUITexture opacity, and play that animation when you want to fade during the game.
{
animation.Play(“TheAnimationYoumadeByKeyframingTheGUITexture”);
}

rename asset and script varible to suit
AC

Targos-

The method you gave me first initially made more sense to me; coming from Maya, Shake, FCP, etc., the idea of keyframing the light color is very familiar.

I ran into a couple of issues with it though: one, while I did get the keyframes set up, the values would scale higher than I had set them e.g., at 0:0, keyframe color to black, at 2:0 set color to 50% grey (white would actually blow it out), at 3:30 set color to 50% grey again (to create a constant color between 2:0-3:30), and at 4:0 set to black again for the fadeout. Oddly enough, I found that between 2:0 and 3:30, the color would ramp up to somewhere between 50% grey and white, which seems wrong. It makes me think the keyframe interpolation is a spline tangent. In Maya, I would correct this by selecting the two keyframes in the Graph Editor and clamping those frames to a step tangent (which would give a perfectly flat line or unchanging value between those two frames), but Unity is not Maya, and I don’t know of a similar function within it.
The other issue I ran into is what I would consider a user interface bug…when I would set the color of the light and go to the timeline to right click to set the keyframe, the ticker would jump to wherever I had right clicked, which would both destroy the change I had made and made it really annoying to try to keyframe on an exact frame. It would be better IMO, if the keyframe ticker only moved with a left mouse click, and right clicks should only bring up the context menu for keyframing. Less aggravation that way.

In the end, Eric’s script and method works beautifully for me, without any of the aforementioned issues. I do, however, have one small problem: I can’t figure out how to scale the GUITexture so it fits the whole screen. When I first created it, it had a Unity logo on it, which I replaced with the single white pixel texture. The color value on the GUITexture correctly controls the color (even with a white texture, as Eric said) and the color opacity changes the opacity of the texture. I attached the script he provided, which works beautifully. The problem is that I can’t figure out how to make it any bigger than the original Unity texture that was on it. Eric said:

but I can’t find where to set this. When I select the GUITexture in the Hierarchy view, all I get is what is in the below image. Am I missing something obvious and stupid here?

Sorry to bug you guys with things so small, one day I hope to return the favor of all your help. Maybe I can make some assets for you or something one of these days.

59271--2162--$picture_2_101.png

See at the top where it says simple?Try switching to full.
AC

Wow, I feel really dumb.

Thanks, Targos. :lol:

Got everything working perfectly now. Had to add something to the script:

function Start () { 
   yield Fade(.5, 0, 1);  // Start, end, length in seconds 
   yield WaitForSeconds(2.5); 
   yield Fade(0, .5, .5); 
   yield WaitForSeconds(.5);
   Application.LoadLevel("Level1"); 
}

Added the yield WaitForSeconds(.5) prior to the Applicaiton.LoadLevel because I found that if I didn’t, the screen didn’t return to a full black (splash screen was still just slightly visible). Also, it gives a slightly nicer transition to the stage, which I added a simpler version of this to to fade it in from black.

Thanks for all your help guys, again, indebted to you.

Glad it worked out. Now that I think about it, it seems to me that the “just slightly visible” problem could be also solved by adding a line to the end of the Fade function that says

guiTexture.color.a = end;

Since it’s likely that Time.deltaTime variance ends up with i being a bit less than exactly 1.0, so the opacity never quite gets to 100% of the desired end value. Speaking of which, the for statement should technically read “for (i = 0.0; i <= 1.0” etc., although it probably won’t make any noticeable difference…

–Eric

Ok guys, I think I am good but I have one more thing to ask.

I now have a dialog (in a GUITexture) that fades onto the screen, stays there for 10 seconds (so it can be read), and then fades off.

What I want to do is force it to be centered, and to always be drawn at the original resolution of the image (regardless of the screen resolution). I am not sure how to do this. Below are images of the settings on the GUITexture and a shot of it’s appearance in the Game view. On another note, if I want to force the web player to automatically fullscreen when it loads, how would I go about doing that?


59286--2166--$picture_1_185.png

You want the scale to be 0, the x and y position to be .5, and the pixel inset width and height values should be the width and height of the image (with x being -width/2 and y being -height/2).

Although it seems to me that an OnGUI function would be better for this sort of thing, since it would be actual text and not an image. That way you can easily change the text at any time (such as for fixing typos…infinitley → infinitely). :wink:

I don’t think that’s possible (or desirable…when web browsing, I’d never want anything going full-screen unless I specifically asked it to).

–Eric

Thanks for the help. I thought about using the OnGUI function, but I need to add a font to Unity to do this. Do I have to make a font sheet or can I simply add the font file that I used in PS to type this text?

You need a TrueType font (.ttf or .dfont).

–Eric