SG Patcher API

SGResources


T SGResources.Load<T>(string path)


Parameters
path Path to the target resource to load.
Returns

T The requested asset's Type.

Description

Loads an asset stored at path in a folder called SGResources.

Returns the asset at path if it can be found otherwise returns null. Only an object of type T will be returned. The path is relative to any folder named SGResources inside the Assets folder of your project. More than one SGResources folder can be used. For example, a project may have SGResources folders called Assets/SGResources and Assets/Guns/SGResources. String names that include Assets and SGResources are not needed. For example the loading of a GameObject at Assets/Guns/SGResources/Shotgun.prefab does not use the folder names. Also, if Assets/SGResources/Guns/Missiles/PlasmaGun.prefab exists it will be loaded using Prefabs/Missiles/PlasmaGun. If you have multiple SGResources folders you cannot duplicate use of an asset name.

Another example of the SGResources folder. In SGResources there are two files, fancyA and fancyB. SGResources also has Resources2 folder. This folder contains two files, fancyA2 and fancyB2. Finally, Resources2 has a folder called Resources3 with a single file also called fancyB. (This means the file in Resources3 has the same name as in SGResources.) The files in SGResources can be loaded as fancyA and fancyB with no need for the folder hierarchy Assets/SGResources. Also, the files in Resources2 can be loaded. Loading these require the folder directory so an example load will be Resources2/fancyB2. Finally, loading from Resources3 will be Resources2/Resources3/fancyB.

Extensions must be omitted.

All asset names and paths in Unity use forward slashes, paths using backslashes will not work.


Object SGResources.Load(string path)

Object SGResources.Load(string path, System.Type type)


Parameters
path Path to the target resource to load. When using an empty string (i.e., ""), the function loads the entire contents of the SGResources folder.
systemTypeInstance Type filter for objects returned.
Returns

Object The requested asset returned as an Object.

Description

Loads an asset stored at path in a SGResources folder.

Returns the asset at path if it can be found, otherwise returns null. If the type parameter is specified, only objects of this type are returned. The path is relative to any SGResources folder inside the Assets folder of your project, extensions must be omitted.

Note: All asset names and paths in Unity use forward slashes, paths using backslashes will not work.


T[] SGResources.LoadAll(string path)


Parameters
path Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the SGResources folder.
Returns

 List of all objects of Type T.

Description

Loads all assets in a folder or file at path in a SGResources folder.

If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned. Only objects of type T will be returned. The path is relative to any SGResources folder inside the Assets folder of your project.


Object[] SGResources.LoadAll(string path)

Object[] SGResources.LoadAll(string path, System.Type type)


Parameters
path Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the SGResources folder.
Returns

 List of all objects of Type Object.

Description

Loads all assets in a folder or file at path in a SGResources folder.

If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned. The path is relative to any SGResources folder inside the Assets folder of your project.

All asset names and paths in Unity use forward slashes. Paths using backslashes will not work.


T[] FindObjectsOfTypeAll<T>()


Returns

 List of all objects of Type T.

Description

This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal objects, therefore be careful with the way you handle the returned objects.

Contrary to Object.FindObjectsOfType this function will also list disabled objects.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ExampleScript : MonoBehaviour
{
    List<GameObject> GetAllObjectsOnlyInScene()
    {
        List<GameObject> objectsInScene = new List<GameObject>();

        foreach (GameObject go in SGResources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
        {
            if (!EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
                objectsInScene.Add(go);
        }

        return objectsInScene;
    }
}

Object[] SGResources.FindObjectsOfTypeAll(System.Type type)


Returns

 List of all objects of Type T.

Description

This function using non-generic types can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore be careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.

That this function is very slow and is not recommended to be used every frame.

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class ExampleScript : MonoBehaviour
{
    List<UnityEngine.Object> GetSceneObjectsNonGeneric()
    {
        List<UnityEngine.Object> objectsInScene = new List<UnityEngine.Object>();

        foreach (UnityEngine.Object go in SGResources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)) as UnityEngine.Object[])
        {
            GameObject cGO = go as GameObject;
            if (cGO != null && !EditorUtility.IsPersistent(cGO.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
                objectsInScene.Add(go);
        }

        return objectsInScene;
    }
}

SGSceneManager

Scene management at run-time.

In-App Settings:

image-1595077954273.png

 

Unity Build Settings:

image-1595078006037.png

SGSceneManager provides only methods for loading scenes, unloading scenes and number of scenes in the In-App Settings. Use the rest of the methods, events and properties through UnityEngine.SceneManagment.SceneManager. 

SGSceneManager and SceneManager work together. But you cannot use SceneManager methods to load and unload scenes that are specified in In-App Settings.

You can load scenes that are in the Unity Build Settings via the standard SceneManager.

Note that the Unity Build Settings and In-App Settings scene indexes are independent!

SceneManager events will work for SGSceneManager too.

Also you can use the following SceneManager methods after loading the scene via SGSceneManager:

You can read more about SceneManager here.


SceneCountInSettings


The number of Scenes which have been added to the Scenes in the In-App Settings. The Editor will contain Scenes that were open before entering playmode.


LoadScene

public static void LoadScene(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, LoadSceneMode mode = LoadSceneMode.Single);\

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the In-App Settings to load.
mode Allows you to specify whether or not to load the Scene additively. See LoadSceneMode for more information about the options.

Description

Loads the Scene by its name or index in the In-App Settings.

In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is: LoadSceneAsync.

When using SGSceneManager.LoadScene, the scene loads in the next frame, that is it does not load immediately. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.

Because loading is set to complete in the next rendered frame, calling SceneManager.LoadScene forces all previous AsyncOperations to complete, even if SGAsyncOperation.allowSceneActivation is set to false. To avoid this, use LoadSceneAsync instead.

The given sceneName can either be the Scene name only, without the .unity extension.

Note that sceneName is case insensitive.

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        // Only specifying the sceneName or sceneIndex will load the Scene with the Single mode
        SGSceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
    }
}

The following two script examples show how LoadScene can load Scenes from the In-App Settings. LoadSceneA uses the name of the Scene to load. LoadSceneB uses the number of the Scene to load. The scripts work together.

LoadSceneA file.

// SceneA.
// SceneA is given the sceneName which will
// load SceneB from the In-App Settings

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment;

public class LoadScenesA : MonoBehaviour
{
    void Start()
    {
        Debug.Log("LoadSceneA");
    }

    public void LoadA(string scenename)
    {
        Debug.Log("sceneName to load: " + scenename);
        SGSceneManager.LoadScene(scenename);
    }
}

LoadSceneB file.

// SceneB.
// SceneB is given the sceneIndex of 0 which will
// load SceneA from the In-App Settings

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment;

public class LoadScenesB : MonoBehaviour
{
    void Start()
    {
        Debug.Log("LoadSceneB");
    }

    public void LoadB(int sceneANumber)
    {
        Debug.Log("sceneIndex to load: " + sceneANumber);
        SGSceneManager.LoadScene(sceneANumber);
    }
}

public static Scene LoadScene(int sceneBuildIndex, LoadSceneParameters parameters);
public static Scene LoadScene(string sceneName, LoadSceneParameters parameters);

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the In-App Settings to load.
parameters Various parameters used to load the Scene.

Returns

Scene A handle to the Scene being loaded.

Description

Loads the Scene by its name or index in the In-App Settings.

An example using two scenes called Scene1 and Scene2. ExampleScript1.cs is for scene1 and ExampleScript2.cs is for scene2.

using UnityEngine;
using UnityEngine.SceneManagement;
using SIDGIN.Patcher.Unity.SceneManagment;
// This is scene1.  It loads 3 copies of scene2.
// Each copy has the same name.

public class ExampleScript1 : MonoBehaviour
{
    private Scene scene;

    private void Start()
    {
        var parameters = new LoadSceneParameters(LoadSceneMode.Additive);

        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 1 of scene2: " + scene.name);
        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 2 of scene2: " + scene.name);
        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 3 of scene2: " + scene.name);
    }
}

Scene2:

using UnityEngine;

// create a randomly placed cube

public class ExampleScript2 : MonoBehaviour
{
    private void Start()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = new Vector3(Random.Range(-5.0f, 5.0f), 0.0f, Random.Range(-5.0f, 5.0f));
    }
}

LoadSceneAsync


public static SGAsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Single);
public static SGAsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single);
public static SGAsyncOperation LoadSceneAsync(string sceneName, LoadSceneParameters parameters);
public static SGAsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneParameters parameters);

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the Build Settings to load.
mode If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
parameters Struct that collects the various parameters into a single place except for the name and index.

Returns

SGAsyncOperation Use the SGAsyncOperation to determine if the operation has completed.

Description

Loads the Scene asynchronously in the background.

The given Scene name can either be the full Scene path, the path shown in the In-App Settings window or just the Scene name. If only the Scene name is given this will load the first Scene in the list that matches.

Examples of supported formats:
"Scene1"
"Scene2"

Note: The name of the Scene to load is case sensitive.

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using SIDGIN.Patcher.Unity;
using SIDGIN.Patcher.Unity.SceneManagment;

public class Example : MonoBehaviour
{
    void Update()
    {
        // Press the space key to start coroutine
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Use a coroutine to load the Scene in the background
            StartCoroutine(LoadYourAsyncScene());
        }
    }

    IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in the In-App Settings.

        SGAsyncOperation asyncLoad = SGSceneManager.LoadSceneAsync("Scene2");

        // Wait until the asynchronous scene fully loads
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }
}
Example with progress:
  IEnumerator LoadGameAsync()
        {
            var asyncOperation = SGSceneManager.LoadSceneAsync(0);
            while (!asyncOperation.isDone)
            {
                Debug.Log($"Loading progress:{asyncOperation.progress * 100}%");
                yield return null;
            }
        }

UnloadSceneAsync


public static SGAsyncOperation UnloadSceneAsync(int sceneBuildIndex);
public static SGAsyncOperation UnloadSceneAsync(string sceneName);
public static SGAsyncOperation UnloadSceneAsync(Scene scene);
public static SGAsyncOperation UnloadSceneAsync(int sceneBuildIndex, UnloadSceneOptions options);
public static SGAsyncOperation UnloadSceneAsync(string sceneName, UnloadSceneOptions options);
public static SGAsyncOperation UnloadSceneAsync(Scene scene, UnloadSceneOptions options);

 

Parameters

sceneBuildIndex Index of the Scene in BuildSettings.
sceneName Name or path of the Scene to unload.
scene Scene to unload.
options Scene unloading options.

Returns

SGAsyncOperation Use the SGAsyncOperation to determine if the operation has completed.

Description

Destroys all GameObjects associated with the given Scene and removes the Scene from the SGSceneManager.

The given Scene name can either be the full Scene path, the path shown in the In-App Settings window or just the Scene name. If only the Scene name is given this will load the first Scene in the list that matches.

Examples of supported formats:
"Scene1"
"Scene2"

This is case-sensitive and due to it being async there are no guarantees about completion time.

Assets from Resources(unity) are currently not unloaded. In order to free up asset memory call Resources.UnloadUnusedAssets.

It is not possible to UnloadSceneAsync if there are no scenes to load. For example, a project that has a single scene cannot use this static member.

SGAsyncOperation

Description

Asynchronous operation coroutine.

You can yield until asynchronous operation continues, or manually check whether it's done (isDone) or progress (progress).

Properties

allowSceneActivation Allow Scenes to be activated as soon as it is ready.
isDone Has the operation finished? (Read Only)
progress What's the operation's progress. (Read Only)

See Also: SGSceneManager.LoadSceneAsync.

 

SGPatcherLoader


Events


SGPatcherProgressEvent<PatcherProgress> onProgressChanged.

Unity Event for callback process.

public struct DownloadProgress
    {
        public float progress;
        public string status;
        public string speed;
        public string downloadedSize;
        public string targetSize;
    }
    public class PatcherProgress
    {
        public float progress;
        public string status;
        public DownloadProgress downloadProgress;
    }
SGPatcherErrorEvent<ErrorMessage> onError.

Unity Event for handling errors.

public class ErrorMessage
    {
        public string message;
        public System.Exception exception;
    }
SGPatcherErrorEvent<ErrorMessage> onInternalError.

Unity Event for handling Internal errors.


async void UpdateGame()


Runs an integrity check and updates the game as needed.

For run use async await (Tasks);


void LoadGame()


If the game was installed, it loads level 0 without checking for updates.


async Task<UpdateMetaData> GetUpdateMetaData()


Returns preliminary update meta data.

 public enum UpdateStatus
    {
        No_Required,
        Required,
        Main_Build_Update_Required
    }
    public class UpdateMetaData
    {
        public UpdateStatus updateStatus;
        public long updateSize;
        public int updateCount;
        public string notes;
    }

 


async Task InstallPackage(packageName)


Installing package by name.