Hello! I just discovered this command (and it was relly usefull!). But I have a problem using it. Let me explain:
I did a script for use it for all my UI components and it is working perfectly for fade out (alpha 1 to 0). But if I want the reverse (alpha 0 to 1) it isn’t working if I disabled my UI element before (SetActive(false)).
The only way that I find to fix it was disable GraphicRaycaster after set alpha to 0, so UI element is there, but player can’t interact or see it. Of course I guess this isn’t a good way to do it if game have alot more UI elemnts, but was enough good for a simple project.
So the question is… is there a way to do a CrossFadeAlpha working from 0 to 1 alpha value just after set a UI element active with an alpha 0 (that it was set previously)?
Thanks so much ^^
//=============================================
/// <summary>
/// Use this for check all Children or specific GameObject (remember use checkchildren to false for this feature).
/// </summary>
/// <param name="gameObject">Game object.</param>
/// <param name="checkchildren">If set to <c>true</c> checkchildren.</param>
/// <param name="newAlpha">0 for transparent, 255 opaque.</param>
/// <param name="duration">Duration.</param>
/// <param name="ignoreTimeScale">If set to <c>true</c> ignore time scale.</param>
public void FadingItems(GameObject gameObject, bool checkchildren, float newAlpha, float duration, bool ignoreTimeScale, bool enableGameObject)
{
if (newAlpha < 0f)
newAlpha = 0f;
if (newAlpha > 1f)
newAlpha = 1f;
if (enableGameObject) {
StartCoroutine (EnableGameObject (gameObject, 0f));
} else {
StartCoroutine (DisableGameObject (gameObject, duration + 0.01f));
}
if (checkchildren) {
foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {
Fade(reference.gameObject, newAlpha, duration, ignoreTimeScale);
}
} else {
Fade(gameObject, newAlpha, duration, ignoreTimeScale);
}
foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {
if (reference.gameObject.GetComponent<Button> ()) {
if (enableGameObject) {
reference.gameObject.GetComponent<Button> ().enabled = true;
reference.gameObject.GetComponent<Image> ().enabled = true;
} else {
reference.gameObject.GetComponent<Button> ().enabled = false;
reference.gameObject.GetComponent<Image> ().enabled = false;
}
}
}
}
//===========Fading or Unfading Objects=================================================================================
private void Fade(GameObject reference, float newAlpha, float duration, bool ignoreTimeScale)
{
if (reference.gameObject.GetComponent<Text> ()) {
reference.gameObject.GetComponent<Text> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
} else {
if (reference.gameObject.GetComponent<Image> ()) {
reference.gameObject.GetComponent<Image> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
}
}
}
//===================================================================================================================
IEnumerator DisableGameObject(GameObject reference, float delay)
{
yield return new WaitForSeconds (delay);
//reference.gameObject.SetActive (false);
try{
reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = false;
}catch{
};
}
//===================================================================================================================
IEnumerator EnableGameObject(GameObject reference, float delay)
{
yield return new WaitForSeconds (delay);
//reference.gameObject.SetActive (true);
try{
reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = true;
}catch{
};
}