No problem king =) Everything find around here.
You want it to fade straight into the next background or would you rather fade it into nothing, change the background and then fade in again?
If you want it to fade into another Background, you would have to add another GameObject in the same place with one image and a script that calls the CrossFadeAlpha from inside this new GameObject (seems that the CrossFadeAlpha don’t work when you are trying to change the alpha of another GameObject, just the one that the script is attached to)
I tried it out, I added this script to the second GameObject (I had to use one CrossFadeAlpha with 0 of duration to change the alpha of the image, otherwise it wouldn’t work, seems that if you start the game with the alpha set to 0, you wont be able to change it with CrossFadeAlpha):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CrossFade : MonoBehaviour {
Image myImage;
void Start()
{
myImage = GetComponent<Image>();
myImage.CrossFadeAlpha(0,0,false);
}
public void ChangeMyAlpha(float Amount, float Duration, bool TimeDep)
{
myImage.CrossFadeAlpha(Amount,Duration,TimeDep);
}
This Way you can control the crossfade from another GameObject.
After that you can add an routine that calls the fadein and fadeout in both GameObjects, change the image index and the image in both gameObjects and then calls itself again such as the following:
Image background1;
CrossFade background2;
public float FadeTime;
void Start () {
background1 = GetComponent<Image>();
background2 = FindObjectOfType<CrossFade>().GetComponent<CrossFade>();
StartCoroutine(CrossAlpha(10f,FadeTime));
}
IEnumerator CrossAlpha(float TimeUntilChangeBackground, float FadingTime)
{
yield return new WaitForSeconds(TimeUntilChangeBackground);
SpriteOntheBackground2 = NextSpriteToLoad; // Names choosen as example
SpriteIndex++; // Names choosen as example
background1.CrossFadeAlpha(0,FadingTime,false);
background2.ChangeMyAlpha(1f,FadingTime,false);
yield return new WaitForSeconds(TimeUntilChangeBackground);
SpriteOntheBackground1 = NextSpriteToLoad;
SpriteIndex++;
background2.ChangeMyAlpha(0,FadingTime,false);
background1.CrossFadeAlpha(1f,FadingTime,false);
StartCoroutine(CrossAlpha(FadingTime));
}
I’ve not done all the code, you would have to add the definition for NextSpriteToLoad
, SpriteOntheBackground2
, SpriteIndex
and SpriteOntheBackground2
, I have just done the part regarding the FadeIN and FadeOut, This kind of structure would allow you to Fade in and Fade out directly to another Background.
But if you rather Fade IN and Out to alpha0 instead of Fade into another background, you wouldn’t need to add another GameObject as the example above and would be ready to go with a script as simple as this:
IEnumerator CrossAlpha(float TimeUntilChangeBackground, float FadingTime)
{
yield return new WaitForSeconds(TimeUntilChangeBackground-(2*FadingTime));
background.CrossFadeAlpha(0,FadingTime,false)
yield return new WaitForSeconds(FadingTime);
Change();
background.CrossFadeAlpha(1,FadingTime,false);
yield return new WaitForSeconds(FadingTime);
StartCoroutine(CrossAlpha(TimeUntilChangeBackground, FadeTime));
}
Although you would have to take the StartCoroutine out of the SetBackground() and add it just to the void Start(),
It should work. Please Let me know the results.