using UnityEngine; namespace E7.Introloop { /// /// /// An abstract class that grants you a new set of convenient singleton instance just like the /// , but now under your own self-defined class name. /// (e.g. You can now call MyIntroloopPlayer.Instance and so on.) /// /// /// /// /// This class utilize C# trick where each unique combination of generic class gives you a /// unique instance of static variables. Putting your self-defined class itself inside T /// basically give you a personal set of static. /// /// /// Define your class like this : /// /// public class FieldBGMPlayer : IntroloopPlayer<FieldBGMPlayer> /// /// (Put your class itself into the generic variable.) /// /// public abstract class IntroloopPlayer : IntroloopPlayer where T : IntroloopPlayer { private static T instance; // ReSharper disable once StaticMemberInGenericType private static AudioSource singletonInstanceTemplateSource; /// /// /// Get a convenient singleton instance of from anywhere in your code. /// This singleton instant is different for each subclass of this abstract class. /// It has DontDestroyOnLoad applied. /// /// /// Before calling this for the first time, call /// first to setup its from script. /// (It does not exist until runtime, you cannot setup the template ahead of time /// unlike non-singleton instances.) /// /// public new static T Instance { get { if (instance != null) { return instance; } instance = InstantiateSingletonPlayer(singletonInstanceTemplateSource); instance.name = IntroloopSettings.singletonObjectPrefix + instance.name; return instance; } } /// /// Call this before the first use of to have the singleton instance /// copy fields from . /// /// /// /// Singleton instance is convenient but you cannot pre-connect /// like a regular instance because /// it does not exist until runtime. /// /// /// If you had already used the singleton instance before calling this, you can still call /// on the singleton instance to apply different /// settings of . /// /// public new static void SetSingletonInstanceTemplateSource(AudioSource templateSource) { singletonInstanceTemplateSource = templateSource; } } }