Search Unity

Texture.AwakeFromLoad?

Discussion in 'Editor & General Support' started by bibbinator, Dec 6, 2010.

  1. bibbinator

    bibbinator

    Joined:
    Nov 20, 2009
    Posts:
    507
    Hi,
    I load up a 3D level and start moving, after about 5 seconds the game locks up for about a second and then continues fine with no more stalls.

    Upon investigation with profile, I see that stall is caused by Texture.AwakeFromLoad.

    What is this? And how to I get it to "awaken" before the game starts to prevent this stall?

    Thanks!
     

    Attached Files:

  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Does the game pause at the point where the an object or texture becomes visible onscreen or during any other event that might affect a texture? It's a bit hard to narrow this down without some more information.
     
  3. bibbinator

    bibbinator

    Joined:
    Nov 20, 2009
    Posts:
    507
    Thanks for your reply!

    Well, as you can imagine, it's hard to say what's becoming visible in a level full of stuff. But let's assume it is and it's something on my side. For me to track it down or think it through, what does Texture.AwakeFromLoad do? Does it "awake" when it becomes visible, or is it like the MonoBehavior awake and gets called on load after construction? Is there any way to force it to become visible/awake if that's what it is?

    Cheers
     
  4. hunterua

    hunterua

    Joined:
    Nov 5, 2009
    Posts:
    42
    We had the same problem on iPhone.

    We load a scene and once characters became visible, iPhone starts getting laggy

    Our scene was pretty simple, just textured plane as background and 6 animated human characters at right of the screen, but at start those characters was so far, so it was not visible. Once we start rolling camera to right and characters gets appear on screen iPhone start lagging there.

    While reproducing this problem on Unity Mac with profiler we saw the huge spikes at the moment they appear on screen. Most time consuming method was Texture.AwakeFromLoad

    Well, it was pretty challenge to understand what's going on and how to solve it, because official doc for this don't give any clue and different options like "Update when off-screen" is not help there since its for skin/animation.

    So, I found the cheat/workaround for this.. when scene was load in Awake/Start phase, we get rendering component for every model and then call any texture method. I guess, that during calling "almost any" texture methods while it's not loaded to memory yet, cause to load it and install it VRAM. After this we noticed more smooth framerate for this scene.

    Here is piece of code to see what we did:

    Code (csharp):
    1.  
    2. //... loop for every loaded character  game object as "contestant" variable
    3. {
    4. //....
    5.     skinRender = contestant.GetComponentInChildren<SkinnedMeshRenderer> ();
    6.     if (skinRender != null) {
    7.         skinRender.updateWhenOffscreen = false;
    8.         if (skinRender.sharedMaterial.mainTexture != null)
    9.             {
    10.             int tw = skinRender.sharedMaterial.mainTexture.width;
    11.             //Debug.Log("PA tex w="+skinRender.sharedMaterial.mainTexture.width);
    12.        }
    13.     }
    14. //....         
    15. } // end of loop/iterator for each model
    16.  
    I don't know if it's bug or feature, but for Unity iOS, can be really good to have more fine controls for things like this,
    since performance sometimes is critical there...

    Hope this helps for you.

    Regards,
    Vyacheslav
     
    herefjr likes this.
  5. bibbinator

    bibbinator

    Joined:
    Nov 20, 2009
    Posts:
    507
    Vyacheslav,
    Thanks very much for your note and help! I'll give it a try.
    Cheers!
     
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    Vyacheslav is basically right - AwakeFromLoad for Texture can upload it to gpu making it stall. Though - in unity3.x we are making sure we are loading all assets during loading so tricks like that shouldn't be needed. If you don't create/change textures at runtime or load them through asset bundles you shouldn't really see spikes like that
     
  7. hunterua

    hunterua

    Joined:
    Nov 5, 2009
    Posts:
    42
    Hello Alexey.
    This is for which platfform ? Because I see for Unity 3.1 for iPhone/iPad it's not the case :(
    I just faced this problem again with our iPad project on same scene, but this time characters has many materials/textures and this time, the stall is much more noticeable. The trick in my code make it smooth again.
    We load characters as prefabs from resources (using Resources.Load) so may be this is the case.
     
  8. bibbinator

    bibbinator

    Joined:
    Nov 20, 2009
    Posts:
    507
    Alexey,
    Thanks for the note, and thanks hunterua for your note too.

    We definitely have this problem still in the latest 3.1 version while running in the editor on Mac as well as the iOS devices. We're needing to put a model with all the textures on it at the feet of the player on start to force everything to load.
     
  9. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    Yeah, Resources.Load falls into same category (after loading we need to upload them to gpu - and this can happen at any time)
    If you have Texture.AwakeFromLoad spikes for textures that were on scene - file a bug
     
  10. hunterua

    hunterua

    Joined:
    Nov 5, 2009
    Posts:
    42
    Thanks.
    Ok, I will if we found something. In my case all models are loaded from resources on each scene because the models/characters depends on player decisions/gameplay, so we don't have predefined characters models on scene.
     
  11. hunterua

    hunterua

    Joined:
    Nov 5, 2009
    Posts:
    42
    Alexey, may be there is more "elegant" solution for Unity iOS (other than I found) for preloading textures during Awake/Start when we instantiate models loaded from resources ?
     
  12. ReJ

    ReJ

    Unity Technologies

    Joined:
    Nov 1, 2008
    Posts:
    378
    Unfortunately resource loading / allocation patterns in the Editor might be very different comparing with the code running on device. I wouldn't trust Editor Profiler on Texture.AwakeFromLoad.

    However if you see and hiccups that you suspect because of loading resources - then:
    1) first try to have empty Main scene (index 0) which is loaded first and then kicks loading of your actual scene
    2) if that doesn't help with hiccups - please submit a repro case project via BugReporter - we definitely need them to hunt down problems like that.
     
  13. xcodeusa

    xcodeusa

    Joined:
    Jun 27, 2009
    Posts:
    26
    Problem with Texture.AwakeFromLoad is still noticable with our project. Currently building off of 3.3.0f4. Noticable in the Profiler on first time Instantiatons.
     
  14. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Know this is an old thread, but just noticed this issue when in the editor... will test if it happens in a build and post an update in a few minutes...
     
  15. Psyco92

    Psyco92

    Joined:
    Nov 15, 2013
    Posts:
    22
    It would be nice to be able to force objects from resources to be "fully" loaded before instantiation.
    Or did i miss it? (I did look)
     
  16. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    This is still a thing. Game still lags during texture's first visibility on screen. Anyone has any elegant solution?
     
  17. YoNeyers

    YoNeyers

    Joined:
    Feb 10, 2017
    Posts:
    32
    i have the same thing , Any solution?
     
  18. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    2019.1 still a thing
     
  19. projectorgames_unity

    projectorgames_unity

    Joined:
    Oct 15, 2018
    Posts:
    107
    I think this bug has regressed then. I've seen this get as high as 3 seconds.

    upload_2022-4-26_14-21-15.png
     
  20. AR_Tehcnoplus

    AR_Tehcnoplus

    Joined:
    Jan 17, 2022
    Posts:
    15
    I got the same problem for a very simple Scene. The lag happens between scene changing upload_2022-6-1_16-46-33.png
     
  21. fizzyfrosty

    fizzyfrosty

    Joined:
    Mar 3, 2016
    Posts:
    18
    I've found a pretty good and simple solution I think, that completely worked for me. I basically duplicated all my scene objects with textures, put them under 1 parent object, and scaled them to 1/100th of a size to fit them in the camera at my player's feet. Then I reference them to my scene controller, and on scene start, I put them in front of the camera for 1 frame, and on the next frame, I deactivate the game object.

    Again:
    1. Make a copy of all objects with textures, and place them under 1 parent object.
    2. Miniaturize them by setting scale enough to fit inside your camera.
    3. Hook this object up to a reference in scene controller.
    4. On load, wait 1 frame, set the position to be in front of the camera (or in front of your player)
    5. On the next frame (2nd frame), deactivate the game object.
    I've found that textures had to be in front of the camera and rendered for at least 1 frame for it to work. My game with a bunch of textures is now buttery smooth on an iPhone SE2, whereas it used to stutter every time the camera went in front of a building or any object for the first time.

    For the implementation, I used a coroutine to make sure it happens on 1st and 2nd frames.


    Code (CSharp):
    1. void Start()
    2. {
    3.     // other loading code
    4.     HidePreloadedTextureObjects();
    5.  
    6. }
    7.  
    8. void HidePreloadedTextureObjects()
    9. {
    10.     // Set location of objects to player, in front of camera
    11.     StartCoroutine(HidePreloadedTextureObjectsCoroutine());
    12. }
    13.  
    14. IEnumerator HidePreloadedTextureObjectsCoroutine()
    15. {
    16.     // wait 1 frame, set position, then hide
    17.     yield return null;
    18.     this.preloadTextureObjects.transform.position = gameManager.playerObject.transform.position;
    19.     yield return null;
    20.     this.preloadTextureObjects.SetActive(false);
    21. }
     
    radiantboy likes this.