- Home /
Smoothly Fade in a texture
Hi I am trying to produce a Fade in effect and refine it so that I can fade in a texture alpha using exact increments.
Im mostly there and it seems to make sense however when I use the script to effect the objects texture nothing happens to it's alpha it just automatically pops in at full opacity instead of gradually raising the opacity from 0 - 1.
Here is my code so far.....
using UnityEngine; using System.Collections;
public class Environmental_FX_trigger : MonoBehaviour {
GameObject environmental_FX_trigger;
GameObject new_background_plane;
GameObject blur_background_plane;
public float Fade_start_Value;
//public float Fade_end_Value = 1;
//public float current_Value;
void Awake ()
{
environmental_FX_trigger = GameObject.Find("special_FX/environmental_FX_trigger");
new_background_plane = GameObject.Find("special_FX/environment_background_grp/new_background_plane");
blur_background_plane = GameObject.Find("special_FX/environment_background_grp/blur_background_plane");
blur_background_plane.SetActiveRecursively(true);
}
public void Wait_Fade() {
StartCoroutine(wait());
}
private IEnumerator wait()
{
//x starts at zero - next - while x is less than 20 do stuff - next - repeat the "for loop" equation.
for(int x = 0; x < 20; x++)
{
//wait for one second and then add 1 to the Fade_start_Value by the specified increment.
yield return new WaitForSeconds(1);
Fade_start_Value += 0.1f;
//Declare an instance of the objects color value
//Plugin in a numerical increment into the new value's alpha channel.
//Then make the original objects color value equal to the instanced value.
Color fadeColour = blur_background_plane.renderer.material.color;
fadeColour.a = Fade_start_Value;
blur_background_plane.renderer.material.color = fadeColour;
}
//Keep the Faade_start_Value at 0 if the Couritine hasn't been activated.
Fade_start_Value = 0;
}
void OnTriggerEnter (Collider environmental_FX_trigger)
{
// 1. When entering the trigger change blur texture over time (fade in).
Wait_Fade();
print("IN TRIGGER NOW!!!!!");
blur_background_plane.SetActiveRecursively(true);
}
void OnTriggerExit (Collider environmental_FX_trigger)
{
// 1. When entering the trigger change blur texture over time (fade out).
print("OUT TRIGGER NOW!!!!!");
}
/*
//----------------------------------------
if (Patient_choice_GUI_Skin != null)
{
if(showPatientChoiceGUI)
{
GUI.skin = Patient_choice_GUI_Skin;
Color patientChoice_newAlpha = GUI.color;
patientChoice_newAlpha.a = patientChoice_thisColor * patientChoice_InTransition;
GUI.color = patientChoice_newAlpha;
print("working");
{
if(Time.time < 6f)
{
patientChoice_InTransition = 0.2f * Time.time;
}
else if(Time.time > 6f)
{
patientChoice_InTransition = 1f;
}
}
//----------------------------------------
*/
}
Where am I going wrong?
@karl take a look at the link posted by eric, that has helped me in numerous occasions :)
Thanks Erich5h5 and flamy will check this out and see what I come up with.
Answer by Wolfram · May 25, 2012 at 10:49 AM
One reason could be that your Coroutine is triggered several times. Since you don't reset Fade_start_Value at the beginning of your coroutine, each simultaneously active coroutine adds 0.1 to the alpha each second.
Do you get your "IN TRIGGER NOW!!!!!" message once or several times?
No the msg only comes up once whenever I enter the trigger area if I go back out and then re-enter it shows again once so maybe I need to use on TriggerStay or something like that is that where Im going wrong?
Okay I checked out your link Eric5h5 and copied the following code replacing the object in question for the Fade with my own the code is as follows now in c#.....
public class Fade : $$anonymous$$onoBehaviour {
void Start ()
{
Fade.use.StartCoroutine(blur_background_plane, 0.0, 1.0, 5.0f, EaseType.InOut);
// StartCoroutine(Fade.use.Alpha(.renderer.material, 0, 1.0, 3.0f, EaseType.InOut));
}
}
I seem to get the following errors up however....
ERROR_1
Assets/Standard Assets/Scripts/Fade.cs(10,22): error CS0117: Fade' does not contain a definition for
use'
No, you need to copy the whole script at the bottom of the Wiki page into a script named "Fade.js". Then you can use Fade.use.whatever from your own scripts.
I presume you do use one of the "Transparent" shaders for your blur_background_plane object?
Thanks Wolfram.
I tweaked added all the script and now have the new function of fade.
I have implemented a new script with the following code ......
void OnGUI () {
if(Event.current.type == EventType.keyUp && Event.current.keyCode == $$anonymous$$eyCode.L)
{
//Fade.use.StartCoroutine(renderer.material.color.a, 0, 1, 5.0f, EaseType.InOut);
Fade.use.Alpha(this.renderer.material.color.a, 1, 0, 20f, EaseType.InOut);
}
}
}
The script seems to be fine and does make something happen (as follows)..
In the inspector view when I attach my script to said game object, as soon as the script is invoked the material preview box appears at the bottom right of the inspector, so I know something is happening.
However still no luck with fading in and out as far as the alpha goes the plane with the texture I want to fade in and out doesn't change a bit in my game view?
Any ideas as to what might be happening?
Your answer
Follow this Question
Related Questions
Fade In / Out UI Image 3 Answers
Fade out sprite on click 4 Answers
How can I fade in/out an object nonstop automatic ? 2 Answers
Fade logo before main menu 2 Answers
Sprite smooth fade out/in 2 Answers