Pref Injection

Pro Only: Weaver Lite allows you to try out this feature in the Unity Editor, but it will not be available in a runtime build unless you purchase Weaver Pro.

Weaver's static dependency injection system is primarily used for asset references, but it can also be used for other things such as saving and loading simple values in PlayerPrefs with the [Pref] attribute and EditorPrefs with the [EditorPref] attribute.

  • On startup (including in Edit Mode in the Unity Editor) the attributed value is immediately retrieved from the prefs.
  • Then on shutdown its current value is saved back to the prefs.
  • If you wish to save the prefs earlier you can call PrefAttribute.SaveAll manually.
  • By default, each pref will use the Namespace.DeclaringType.MemberName as its key string, but you can specify a custom Key in the attribute's constructor.
  • You can also specify an OldKey so that it can still load old values saved with a previous key (such as if you move or rename the field).
  • The system currently supports bool, float, int, string, and enum fields. Support for more types could be added, however Unity's prefs system is generally considered bad practice so it is recommended that you use a fully fledged serialization system such as ProtoBuf or Json for anything more complex.