G
G
Georgy Pelageykin2016-02-15 16:38:05
C++ / C#
Georgy Pelageykin, 2016-02-15 16:38:05

How to properly organize the process of loading a complex scene in unity3d?

Hello. There is a space game with a rather complex main scene (more precisely, with a constantly growing number of actions in the development process). At the moment, the loading process consists mainly of creating and initializing various GOs (star, planets, ships, asteroid fields, etc.). Now the LevelBuilder script is responsible for this, in which the Start method contains a footcloth for a hundred lines of code. As part of a big refactoring, I need to rework this, I'm trying to understand how such things are done in normal projects.
In fact, I need something like a pipeline in which I can add / change stages in isolation. In general, we need advice on how best to do this - maybe there is some generally accepted technique, maybe at events or something else.
To understand the scale of the disaster - here is the current LevelBuilder(you can not talk about obvious violations of naming rules, OOP principles, etc. - I'm working on this).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2016-02-16
@ArXen42

You can move planets, asteroids, etc. into separate controllers, like this:

public class AsteroidsController : MonoBehaviour
{
    public GameObject asteroidFieldPrefab;

    private GameObject[] asteroids;

    public void SpawnAsteroids(GameObject star, AsteroidBelt[] asteroidBelts)
    {
        asteroids = new GameObject[asteroidBelts.Length];
        for (int i = 0; i < asteroids.Length; i++)
        {
            AsteroidBelt ast = asteroidBelts[i];
            asteroids[i] = (GameObject) Instantiate(asteroidFieldPrefab, new Vector3(0, 0, 0), Quaternion.identity);

            //Поскольку система изначально имеет радиус 1, она начинает спавнить астероиды внутри звезды. Не надо так
            asteroids[i].SetActive(false);

            asteroids[i].name = ast.name;
            asteroids[i].transform.localScale *= Units_class.MetersToUnityUnits(ast.radius);

            //А теперь надо
            asteroids[i].SetActive(true);

            AsteroidFieldOrbiter_script orbScript = asteroids[i].GetComponent<AsteroidFieldOrbiter_script>();
            orbScript.astdField = ast;
            orbScript.star = star;
            orbScript.SetStartPosit();
        }
    }
}

In the LevelBuilder, you pull SpawnAsteroids, and the asteroid controller lives on by itself, so it will be neater than one big class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question