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

Threads co routines and loading objects into a scene.

Discussion in 'Scripting' started by rickw, Sep 22, 2014.

  1. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    I have a function which loads an XML file, visits it and spawns a bunch of objects into a scene (from prefabs).

    Immediately after this I pan the camera to view these objects. However because the loading of objects into the scene is quite expensive, I dont see the camera pan, since delta time is large.

    I've read that the Unity API isn't thread safe, so I'm unsure how I can resolve this problem.

    I dont have a natural place to yield control back to Unity since its the IO which is expensive so I dont think co routines are the way to go.

    Any thoughts?
     
    Last edited: Sep 22, 2014
  2. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    There are multiple ways to resolve this. A lot depends on your implementation. Here is one possible scenario:

    1. Create a "LOADING" screen that pops up before you start the load.
    2. "Register" your objects into your scene (just create a central component that governs that process and also controls ""LOADING" screen). This registration should take place during the first Update function loop (so Awake and Start are already done).
    3. Once everyone has been registered then you can remove "LOADING" screen.

    Just to say again that this is just on possible solution. But I also think that most solutions in this scenario should revolve around the "LOADING" screen.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What kind of objects and how many? Why don't you use prefabs or spawn + disable them and re-enable/move them to the position when needed. I'm not quite sure if that's a good approach performance wise when dealing with lots of objects, but that may solve the deltaTime issue.
     
  4. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    I am using prefabs from which my visitor constructs instances

     
  5. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    If all this takes place during a single update, deltaTime will still be large so time dependent operations (i.e. lerping etc) will still be impacted in the next frame.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Then i'd simply put them into the scene and disable them as long as you don't need them.
     
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Try to start your lerping etc a frame after you're done loading, when the deltas are back to normal.
     
  8. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    I have a single scene and a levelmanager.
    The levelManager is responsible for constructing the set of objects required for the particular level.

    For example 30 trees in the first level, but 0 trees in the next etc etc.

    Hence I have to read from a configuration file for each level.

    I can look at pre-loading and de-serialising the xml for all levels at the start of the game, which may reduce some of the lag.

    Even with this optimisation, there will still be some lag associated with object creation.

    I could also look at making the camera lerp independent of deltaTime.

    There must be a better way!
     
  9. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    This sounds interesting.

    Something horrible like this?

    int frameIndex = 0;
    bool trackFrames = false;
    //called by some event...
    public void DoExpensiveThing() {
    //expensive stuff
    trackFrames = true;
    frameIndex = 0;
    }

    void Update() {
    if(trackFrames) frameIndex++;
    if(frameIndex>2) {
    trackFrames= false
    //lerping can begin
    }
    }

    I'm hopping there's a lovely built in solution
     
  10. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I'd rather do something like this, but I don't know what your loading setup requires.
    Code (csharp):
    1.  
    2. public void StartLoading() {
    3.     StartCoroutine(LoadRoutine());
    4. }
    5. private IEnumerator LoadRoutine(){
    6.     DoExpensiveStuff();
    7.     yield return null; // wait a frame.
    8.     StartYourLerping();
    9. }
    10.  
    Don't forget your code tags.
     
  11. rickw

    rickw

    Joined:
    Jul 7, 2013
    Posts:
    15
    :) Doh!

    Works perfectly.

    Thankyou!
     
    ThermalFusion likes this.