Smart Reference
version 0.9.0
Summary
Smart Reference is a Unity plugin that allows you to lazy load references to other objects in ScriptableObject and MonoBehaviour.
You may be familiar to use ScriptableObject store data, but when you reference other objects,
they will be treated as dependencies and will be loaded when the ScriptableObject is loaded. This could be slow if you have a lot of references.
For details, see this article.
Smart Reference allows you to load references only when you need them at runtime with same workflow in editor.
Quick Start
-
Use SmartReference instead of Object. See how they look exactly same in the inspector.
public class MonsterData : ScriptableObject { public SmartReference<GameObject> prefab; public SmartReference<Sprite> icon; public string description; }
-
Initialize smart reference when your game start
- If you use Unity Addressables, you need to add USE_UNITY_ADDRESSABLES symbol to your player settings, then call
SmartReference.Runtime.SmartReference.InitWithAddressablesLoader();
- If you use your own custom loader, call
or
SmartReference.Runtime.SmartReference.Init((path, type) => { return MyLoadFunction.Load(path, type); }, (path, type, callback) => { MyLoadFunction.LoadAsync(path, type, obj => { callback(obj); }); });
var loader = new CustomLoader { loader = MyLoadFunction, loaderAsync = MyAsyncLoadFunction, }; SmartReference.Runtime.SmartReference.Init(loader);
- If you use Unity Resources(not recommended, see why), call
SmartReference.Runtime.SmartReference.InitWithResourcesLoader();
- If you use Unity Addressables, you need to add USE_UNITY_ADDRESSABLES symbol to your player settings, then call
-
SmartReference will automatically update paths before player build in case you move or rename the referenced asset. If you want to manually update all references in the project, go to
Tools/SmartReference/Update All Referencesto update all references in the project.
Supports
If you have any questions, please leave an issue at GitHub. Thank you for your support!