Here's a simple example. I haven't tested it, but it should work.
| public class SingletonClass : DrawableGameComponent { |
| |
| private SingletonClass(Game game) |
| : base(game) { |
| |
| } |
| |
| private static SingletonClass _Instance; |
| public static SingletonClass Instance { |
| get { return _Instance; } |
| private set { _Instance = value; } |
| } |
| |
| public static void Initialize(Game game) { |
| _Instance = new SingletonClass(game); |
| } |
| } |
The idea is, the class itself isn't static, but all methods inside the class excluding the ones from DrawableGameComponent are static. If you really need them to be static also though, you can make a static method that calls Instance.WhateverNonStaticMethod(). Initialize would have to be called in order to pass along a reference to Game (this is your main class, the one that derives from Game, the one created when you create a project, Game1 by default), so that it can get passed to the constructor for Instance. Alternatively, you could change that private set to be public and then just have a different class initialize it.
Also, since Instance is accessible, you don't actually have to make any methods static, you could just access them through SingletonClass.Instance.DoWhatever(), if DoWhatever isn't static. If it is static, just SingletonClass.DoWhatever(). There are advantages to both, though I think making them non-static and accessing them through Instance is the generally used way since it allows you to have more than one Instance if need be.