Runtime Injection

Pro Only: Weaver Lite does not include this feature. It allows you to try out Asset Injection in the Unity Editor, but it will not function in a runtime build unless you purchase Weaver Pro.

During the build process a procedural script is generated, added to the start scene, and given references to all injected assets so that they all get included in the build and so it can assign them to their respective fields efficiently on startup at runtime. This happens automatically and does not require any input from the user.

If you get error CS0117 ('class' does not contain a definition for 'member') in the Injector script during a build, you most likely have that member in a #if UNITY_EDITOR region without setting EditorOnly = true in its injector attribute. Unfortunately it isn't possible for Weaver to automatically detect the #if so the EditorOnly flag needs to be set manually in the attribute's constructor like so: [Weaver.AssetReference(EditorOnly = true)].

If you experience any other issues with the Injector script, please email mail@kybernetik.com.au.

Sample Output

// This file was procedurally generated by Weaver. Any modifications will be overwritten.

#pragma warning disable IDE1006 // Naming Styles.

namespace Weaver
{
    /// <summary>This class was procedurally generated by Weaver.</summary>
    [UnityEngine.DefaultExecutionOrder(-20000)]
    [UnityEngine.AddComponentMenu("")]
    internal sealed class Injector : UnityEngine.MonoBehaviour
    {
        [UnityEngine.SerializeField]
        private UnityEngine.Object[] _Objects;

#if !UNITY_EDITOR // Runtime.

        private void Awake()
        {
            // [Weaver.AssetInstance] Weaver.Examples.FrameRate.Instance (Weaver.Examples.FrameRate) { Optional }.
            try
            {
                var obj = _Objects[0];
                DontDestroyOnLoad(obj);
                typeof(Weaver.Examples.FrameRate).GetField("Instance", StaticBindings).SetValue(null, obj);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetReference] Weaver.Examples.GameOverScreen.Prefab (Weaver.Asset<Weaver.Examples.GameOverScreen>).
            try
            {
                var asset = new Weaver.Asset<Weaver.Examples.GameOverScreen>("Game Over Screen");
                typeof(Weaver.Examples.GameOverScreen).GetField("Prefab", StaticBindings).SetValue(null, asset);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.Score.Text (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[1] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.Score).GetField("Text", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.TextManager.ColdDamageText (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[2] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.TextManager).GetField("ColdDamageText", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.TextManager.FireDamageText (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[3] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.TextManager).GetField("FireDamageText", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.TextManager.PhysicalDamageText (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[4] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.TextManager).GetField("PhysicalDamageText", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.TextManager.SpeechText (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[5] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.TextManager).GetField("SpeechText", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

            // [Weaver.AssetPool] Weaver.Examples.TextManager.StatusText (Kybernetik.ObjectPool<Weaver.Examples.FloatingText>).
            try
            {
                var original = _Objects[6] as Weaver.Examples.FloatingText;
                var pool = Kybernetik.ObjectPool.CreateComponentPool(original, 0, true);
                pool.OnRelease = (item) => item.OnRelease();
                typeof(Weaver.Examples.TextManager).GetField("StatusText", StaticBindings).SetValue(null, pool);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }
        }

#endif

        [UnityEngine.RuntimeInitializeOnLoadMethodAttribute(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)]
        private static void OnApplicationStart()
        {
#if !UNITY_EDITOR // Runtime.

            UnityEngine.Application.quitting += SavePrefs;
            Weaver.PrefAttribute.OnSave += SavePrefs;

            // [Weaver.Pref] Weaver.Examples.Score.HighScore (int).
            try
            {
                var value = UnityEngine.PlayerPrefs.GetInt("Weaver.Examples.Score.HighScore", 0);
                typeof(Weaver.Examples.Score).GetProperty("HighScore", StaticBindings).SetValue(null, value, null);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }

#endif
        }

#if !UNITY_EDITOR // Runtime.

        private static void SavePrefs()
        {
            // [Weaver.Pref] Weaver.Examples.Score.HighScore (int).
            try
            {
                var value = Weaver.Examples.Score.HighScore;
                UnityEngine.PlayerPrefs.SetInt("Weaver.Examples.Score.HighScore", value);
            }
            catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); }
        }

#endif

#if UNITY_EDITOR // Editor.

        /// <summary>This property is here to give a compile error if you delete Weaver so you delete this script as well.</summary>
        [Weaver.AssetReferenceAttribute]
        private bool Dummy
        {
            set
            {
            }
        }

#endif

#if !UNITY_EDITOR // Runtime.

        private const System.Reflection.BindingFlags StaticBindings = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;

#endif
    }
}