Skip to content

Instantly share code, notes, and snippets.

@apkd
Last active April 4, 2024 09:36
Show Gist options
  • Save apkd/406df729fa2d8ba78a50a01d4d4b4468 to your computer and use it in GitHub Desktop.
Save apkd/406df729fa2d8ba78a50a01d4d4b4468 to your computer and use it in GitHub Desktop.
Medicine IL post-processing comparison
using UnityEngine;
using Medicine;
class Player : MonoBehaviour
{
[Inject.Single]
LevelManager levelManager { get; }
[Inject.Single]
Camera mainCamera { get; }
[Inject.All]
Enemy[] enemies { get; }
[Inject.All]
IPickup[] pickups { get; }
[Inject.FromChildren]
Transform[] children { get; }
[Inject]
new Renderer renderer { get; }
[Inject.FromChildren.Lazy]
Collider[] childCollidersLazy { get; }
void Awake()
{
Debug.Log("If an Awake method already exists, IL will be injected above user code.");
}
}
[Register.All]
class Enemy : MonoBehaviour { }
[Register.Single]
class LevelManager : MonoBehaviour { }
[Register.All]
interface IPickup
{
bool IsInRange(Vector3 position);
void Activate(Player player);
}
using System.Runtime.CompilerServices;
using UnityEngine;
using Medicine;
class Player : MonoBehaviour, IMedicineInjection
{
[Inject.Single]
LevelManager levelManager
{
[CompilerGenerated]
get => Medicine.RuntimeHelpers.Singleton<LevelManager>.GetInstance();
}
[Inject.Single]
Camera mainCamera
{
[CompilerGenerated]
get => Medicine.RuntimeHelpers.GetMainCamera();
}
[Inject.All]
Enemy[] enemies
{
[CompilerGenerated]
get => Medicine.RuntimeHelpers.Collection<Enemy>.GetInstances();
}
[Inject.All]
IPickup[] pickups
{
[CompilerGenerated]
get => Medicine.RuntimeHelpers.Collection<IPickup>.GetInstances();
}
[Inject.FromChildren]
Transform[] children { get; }
[Inject]
Renderer renderer { get; }
[Inject.FromChildren.Lazy]
Collider[] childCollidersLazy
{
[CompilerGenerated]
get => Medicine.RuntimeHelpers.Lazy.InjectFromChildrenArray<Collider>(this.gameObject);
}
void Awake()
{
<Medicine>Initialize();
Debug.Log("If an Awake method already exists, IL will be injected above user code.");
}
void <Medicine>Initialize()
{
var gameObject = this.gameObject;
children = Medicine.RuntimeHelpers.InjectFromChildrenArray<Transform>(gameObject);
if (!Medicine.RuntimeHelpers.ValidateArray(children))
Debug.LogError("Failed to initialize <b>Transform[] <i>children</i></b> in component <b>Player</b>.\n<i><b>[Medicine.Inject.FromChildren]</b></i> Init() (at Assets/Sample.cs:19)\n", this);
renderer = Medicine.RuntimeHelpers.Inject<Renderer>(gameObject);
if (!renderer)
Debug.LogError("Failed to initialize <b>Renderer <i>renderer</i></b> in component <b>Player</b>.\n<i><b>[Medicine.Inject]</b></i> Init() (at Assets/Sample.cs:22)\n", this);
}
void IMedicineInjection.Inject()
=> this.<Medicine>Initialize();
}
[Register.All]
class Enemy : MonoBehaviour
{
void OnEnable()
=> Medicine.RuntimeHelpers.Collection<Enemy>.RegisterInstance(this);
void OnDisable()
=> Medicine.RuntimeHelpers.Collection<Enemy>.UnregisterInstance(this);
}
[DefaultExecutionOrder(-1)]
[Register.Single]
class LevelManager : MonoBehaviour
{
void OnEnable()
=> Medicine.RuntimeHelpers.Singleton<LevelManager>.RegisterInstance(this);
void OnDisable()
=> Medicine.RuntimeHelpers.Singleton<LevelManager>.UnregisterInstance(this);
}
[Register.All]
interface IPickup
{
bool IsInRange(Vector3 position);
void Activate(Player player);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment