⏳Initialization

Used to make seamless transitions between scenes and hide load scene/objects process

How to use

add using Game.Core;

If your script needs to be initialized you need to implement IInitializable interface. This interface declares float GetInitProgress() to count initialization progress of all objects that being initialized

here is Example:

                                                  // Inherit from interface here
public class InitializationExample : MonoBehaviour, IInitializable {
    [SerializeField] private GameObject[] _cubes;
    private float _replacedCubesCount = 0f;

    private void Awake() {
        // tell initialization manager that we will init this script now
        Initialization.CreateOrGetCurrent().AddInitProc(this);
        
        // start coroutine that activates cubes
        StartCoroutine(ReplaceCubesInitialization());
    }

    // this is IInitializable interface function
    // it takes progress of current initializable object 
    // (cubes activation in this example)
    public float GetInitProgress() {
        // if you can't count progress then simply return 1f;
        
        // progress needs to be [0..1] range
        return _replacedCubesCount / _cubes.Length;
    }

    IEnumerator ReplaceCubesInitialization() {
        // each cube will spawn with delay 0.2f
        var cubeActivateDelay = new WaitForSeconds(0.2f);

        for (int i = 0; i < _cubes.Length; i++) {
            _cubes[i].SetActive(true);
            _replacedCubesCount++; // increment for progress
            yield return cubeActivateDelay;
        }

        // at the end we tell that we done with initialization
        Initialization.current.RemoveInitProc(this);
    }
}

API

// use it when you know that Initialization manager instance exists
public static Initialization current;
// use this to get Initialization manager instance even if it's null
public static Initialization CreateOrGetCurrent();

// Is initialization process is done
public bool IsDone;

/// Adds script initialization proc
/// Must be called at the start of script initialization
public void AddInitProc(IInitializable script);

/// Removes script initialization proc<br>
/// Must be called at the end of script initialization</br>
/// </summary>
public void RemoveInitProc(IInitializable script);

/// Returns total progress of initialization
/// It uses information from all scripts that was added by AddInitProc func
/// Script that is using IInitializable interface must return progress from 0f to 1f!
/// returns total progress [0..1], if all scripts returns progress in range [0..1]
public float GetInitProgress();

Example on scene load initialization in Assets\_Examples\SceneSystem\ when game scene loads.

Last updated