Loading image with WWW before any other scripts in scene start
Here's my situation. When each of my game scenes starts, it loads an image as a texture, and three scripts immediately need access to this texture or else they break. (They need information about the aspect ratio of the image in Start() ). To get this to work, a PuzzleStarter in each scene calls the GameController (a singleton that appears in every scene)'s GetImage() function. This loads the image into a texture called tex in the GameController, and then the coroutine passes control back to the PuzzleStarter's StartPuzzle() function which uses SetActive() to turn on the three scripts. These scripts then call GameController.GC.tex to access the texture data. This is what that looks like, a bit simplified:
public class GameController : MonoBehaviour
{
public Texture2D tex;
void Awake()
{
tex = new Texture2D(1, 1);
}
public void GetImage()
{
string url = System.IO.Path.Combine(Application.streamingAssetsPath, "image.jpg");
WWW www = new WWW(url);
StartCoroutine(LoadTextureIntoImage(www));
}
IEnumerator LoadTextureIntoImage(WWW www)
{
yield return www;
if (www.error == null)
{
byte[] bytes = www.bytes;
tex.LoadImage(bytes);
GameObject.Find("PuzzleStarter").GetComponent<PuzzleStarter>().StartPuzzle();
}
}
}
My problem is that one of the scripts requiring the image/texture's aspect ratio is the camera. So when I first start a scene with the camera deactivated, there is a weird momentary flicker during the texture load where my GUI canvas (which is scaled to fit the camera) spazzes out. So I'm thinking I need some sort of loading screen to allow the image to get loaded into the texture before the other scripts start. Any advice on how I can implement this?
Answer by Cuttlas-U · Apr 12, 2017 at 07:06 PM
hi; why don't u create a fading screen ; just create a full black ui over your screen ; then when the textures is loaded fade it with the function:
GetComponent<Image>().CrossFadeAlpha(1,1,false);
but i think another way is that u load the texture before u load the second scene;
when u are going to change the scene first start loading and when its over pass that script to the next scene with "DontDestroyOnLoad" method ;
and the scripts in the seconds scene give that texture from that ;
sorry for my bad english :)
Your answer
Follow this Question
Related Questions
Save and Load Audio from web 0 Answers
Fast Converting RenderTexture to Texture2D 1 Answer
Unity create large Texture2D is too slow 0 Answers
Texture is bad on device 0 Answers
How to change Texture in editor 0 Answers