LoginSignup
6
2

More than 3 years have passed since last update.

Unity C# シングルトンを継承した抽象クラスの実装

Posted at

シングルトンを継承しようと思うと、意外とどう書いたら良いか分からなかったりするのでメモします。
用途としてはプロジェクトごとに使うライブラリとして用意する場合などです。

Monobehaviourのシングルトン(存在しない場合に生成しても良い)

    public abstract class SingletonObject<T> : MonoBehaviour where T : SingletonObject<T>
    {
        static protected T _instance = null;
        static public T instance => _instance ?? (_instance = FindObjectOfType<T>());
    }

シングルトンを継承した抽象クラス

    public abstract class ManagerBase<T> : SingletonObject<T> where T : ManagerBase<T>
    {
    }

シングルトンを継承した抽象クラスを継承したクラス

    public class Manager : ManagerBase<Manager>
    {
    }
6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2