Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Preloading/uploading textures to vram prior to first render

Discussion in 'Scripting' started by crushforth, Sep 10, 2010.

  1. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    Hi,

    I'm having framerate spikes objects first become visible. Looking at the profiler is looks like this is being caused because the file/resource hasn't actually been loaded yet.

    I've seen numerous posts about solving this by rendering everything prior to it being needed. Before I start writing this I was wondering if anyone had any good implementations of this or suggestions on the best was to achieve this.

    I'm thinking of looping through every MeshFilter and building a dynamic mesh in code that uses this material and rendering them all out for 1 frame and then destroying them. This would hopefully all happen behind my loading screen.

    I think this should be fairly easy for GameObjects that are in the scene that have MeshFilters but I'm not sure about detecting prefabs that haven't yet been instantiated.

    I've only been using unity for a few weeks so I'm still trying to figure stuff out.

    Anyone willing to share their implementations?
     
  2. oblivionfeet

    oblivionfeet

    Joined:
    Jul 24, 2010
    Posts:
    481
    Is it happening to you just while playing it in the editor, or as a standalone / web player version too?

    I've often seen spikes like that while in the editor, but seem fine once in a standalone.
     
  3. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    It's mostly effecting the iPad and iPhone but the spikes are still there on the Mac but they're not as noticable.

    I spent a few days trying to eliminate all the GC.Collect spikes and I found out that almost all of them were from Editor scripts and GUI functions in those scripts. Now they've been commented out I only get spikes from

    Camera.Render
    -> Drawing
    -> Render.Prepare
    -> Loading.ReadObject
    -> Loading.LoadFileHeaders

    Each time I make an object visible for the first time. If I can sort these out it'll be super smooth.
     
  4. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    327
    So anyone find a good preloading solution?

    I guess the best you can do is just make the object visible for a frame to force a gpu load.
     
  5. hurleybird

    hurleybird

    Joined:
    Mar 4, 2013
    Posts:
    257
    I'm bumping this as I'm having the exact same problem in Unity 5, looking for an elegant solution.
     
    jones6 likes this.
  6. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    If any body has some solution for this problem then it helps me as well.
    Please give some suggestion for this problem.
     
    jones6 likes this.
  7. duclaw

    duclaw

    Joined:
    May 9, 2013
    Posts:
    44
    I may be late but I'll add a solution. as the thread topic stated, you have to upload the textures to vram prior to first rendering by putting it in front of a camera before your game starts.

    I solved my problem by taking all my game assets (every single one with textures) and placed them on a plane offscreen.
    then I added a camera to the scene to render the plane and all the assets. I set the camera depth higher than my main camera to save me extra coding work of activating and deactivating.

    After that I wrote a simple script that ran a yield statement for 3 seconds if the scene was loaded, when the 3 seconds were up, the script will disable itself and the plane along with its contents (the assets). also the script will remove the loading screen that covered it from the players view.

    your game will run smoothly after this (if you don't have other performance issues).

    I hope this helps other people because this was a source of worry to me during development of my game