Skip to content

Instantly share code, notes, and snippets.

@abeldantas
Created August 27, 2019 22:25
Show Gist options
  • Save abeldantas/e076048426f8e64eab3e53fc5e6025de to your computer and use it in GitHub Desktop.
Save abeldantas/e076048426f8e64eab3e53fc5e6025de to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Animal<T> : MonoBehaviour
{
public new T Injector;
}
public class Horse : Animal<ISteroidsInjector>
{
}
public interface IDrugInjector
{
void Inject();
}
public interface ISteroidsInjector : IDrugInjector
{
void InjectHarder();
}
public class TrienoloneInjector : ISteroidsInjector
{
public void Inject()
{
Debug.LogError( "A pinch" );
}
public void InjectHarder()
{
Debug.LogError( "Ouch, that hurts" );
}
}
public class HorseComponentSetup
{
[RuntimeInitializeOnLoadMethod] public static void Setup()
{
SetupComponent<Horse, ISteroidsInjector>( new GameObject( "Horse Owner" ), new TrienoloneInjector() );
}
static void SetupComponent<T, TU>( GameObject owner, TU injector )
where T : Animal<TU>
where TU : IDrugInjector
{
var c = owner.AddComponent<T>();
c.Injector = injector;
c.Injector.Inject();
((ISteroidsInjector)c.Injector).InjectHarder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment