Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mybluesock · Apr 12, 2017 at 06:36 PM · texture2dwwwloading

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by mybluesock · Apr 12, 2017 at 07:56 PM

Both great suggestions, thank you for the help!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges