- Home /
Fade in with CrossFadeAlpha
So I think I reversed the meanings, FadeOut being "fade out the players vision" and the opposite for FadeIn. Now, I can make the screen fade from black to normal but not in the other direction. Is there anything wrong with my script or do I just not understand CrossFadeAlpha? The debug log suggests that all parts are being called in the correct order. I'm pretty sure it's a simple mistake somewhere but I cannot figure out what.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FadeScreen : MonoBehaviour {
private Image Image;
Color color = Color.black;
// Use this for initialization
void Start () {
Image = gameObject.GetComponentInChildren<Image>();
Disable();
}
// Update is called once per frame
void Update () {
}
public void Disable()
{
Debug.Log("Disabling Fade Image");
Image.enabled = false;
}
public void Enable()
{
Debug.Log("Enabling Fade Image");
Image.enabled = true;
}
public void FadeIn(int seconds)
{
Debug.Log("FadeIn called");
Enable();
color.a = 1;
Image.color = color;
Image.CrossFadeAlpha(0, seconds, false);
}
public void FadeOut(int seconds)
{
Debug.Log("FadeOut called");
Enable();
color.a = 0f;
Image.color = color;
Image.CrossFadeAlpha(1, seconds, false);
}
}
Answer by Fredex8 · Mar 02, 2016 at 06:31 PM
The unity API says Image.CrossFadeAlpha
uses (float alpha, float duration, bool ignoreTimeScale)
. Your seconds value is an int so it only going to be able to change the alpha value once per second rather than frame by frame.
Why this results in it working one way and not the other I am not sure but you do have color.a = 0f;
explicitly defined as a float but none of your other values have the f. I doubt that is causing a problem but they probably should all be written as 1f
and 0f
.
Your answer
Follow this Question
Related Questions
Fade In / Out UI Image 3 Answers
Fade logo before main menu 2 Answers
[4.6 UI] Image fading - How to? 2 Answers
How do I make the screen fade when it goes into the next level? 0 Answers