I have two class
public class Singleton<T> : MonoBehaviour where T : Component
{
protected static T _instance;
public static T Instance
{
get
{
return _instance;
}
}
}
and
public class GameManager : Singleton<GameManager>
{
// Lots of Methods and Properties...
}
I want to create 3. class derived from GameManager for example ShooterGameManager. I can create ShooterGameManager with
public class ShooterGameManager : GameManager
{}
but Instance is still GameManager !!!, I tried something like to added extra class definition with generic class.
public class GameManager<T> : Singleton<T> where T : Component
{}
but this time there are 2 completely different classes available, nothing inherited from GameManager. What should I do ???
You could Create GameManager like this
public GameManger<T> : Singleton<T>
{}
then you could create GameManger
public GameManager : GameManager<GameManger>
{}
and ShooterGameManager
public ShooterGameManager : GameManager<ShooterGameManager>
{}
and repeat for the other two types you need to create
an example below proves this works
public class Program
{
public static void Main()
{
new GameManager(); //Prints GameManager
new ShooterGameManager(); //prints ShooterGameManager
}
}
public class Singleton<T>
{
private T _instance;
public Singleton()
{
Console.WriteLine(typeof(T).Name);
}
}
public class GameManager<T> : Singleton<T>
{
}
public class GameManager : GameManager<GameManager>
{
}
public class ShooterGameManager : GameManager<ShooterGameManager>
{
}