Created
August 27, 2019 22:25
-
-
Save abeldantas/e076048426f8e64eab3e53fc5e6025de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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