ScritableObjects for runtime reload


#1

As seen on this article: https://blogs.unity3d.com/2017/11/20/making-cool-stuff-with-scriptableobjects/

I’ve recently got rid of my project’s singletons, converted my shared classes to ScritableObjects with the following helper script. This change removed all of my runtime reload errors I used to have.

// Just an example code
public class SingletonScritableObject<T> : ScriptableObject where T : ScriptableObject {
    private static T _instance;

    public static T Instance {
        get {
            if (!_instance) {
                var objs = Resources.FindObjectsOfTypeAll<T>();
                if (objs.Length!=0) _instance = objs[0];
            }
            if (!_instance) _instance = ScriptableObject.CreateInstance<T>();
 
            return _instance;
        }
    }
}


// Or even a class that contains all references
public class GlobalReferences : ScriptableObject {
    public WorldController WorldController;
    public GameController GameController;
    public IntelManager IntelManager;
}

// Usage ( you can either have it existing as an asset, or create a new one for cases like this one )
SingletonScritableObject<GlobalReferences>.Instance.GameController.DoStuff();

Using this pattern you can have shared refs across your scene.

Perhaps if you do things this way, we’ll be able to have runtime reload working properly.


#2

Hi

Unfortunately that is not the issue. Unity’s serialization system would choke on having to serialize as much data as the pathfinding system stores. It is really not feasible to make that work with runtime reload as far as I know.


#3

Not everything needs to serialized, no?

For instance, generated graph could be stored but the rest could be simply discarded/recalculated.


#4

The generated graph is the part that contains a large amount of data.