コード例 #1
0
        public static SingletonLog GetInstance()
        {
            if (Instance == null)
            {
                Instance = new SingletonLog();
            }

            return(Instance);
        }
コード例 #2
0
ファイル: Singletons.cs プロジェクト: blockspacer/iCarus
 public static void Remove(ISingleton singleton)
 {
     if (root.singletonInstances.Remove(singleton))
     {
         if (singleton is MonoBehaviour)
         {
             GameObject.Destroy(((MonoBehaviour)singleton).gameObject);
         }
         SingletonLog.InfoFormat("{0} Removed", singleton.GetType().FullName);
     }
 }
コード例 #3
0
ファイル: Singletons.cs プロジェクト: blockspacer/iCarus
        /// <summary>
        /// 增加一个单件
        /// </summary>
        /// <param name="type">单件类型</param>
        /// <param name="args">传递给单件构造函数的参数, 对MonoBehaviour不适用</param>
        public static ISingleton Add(Type type, params object[] args)
        {
            BaseType baseType = GetBaseType(type);

            if (baseType == BaseType.None ||
                !type.IsSealed ||
                type.IsAbstract ||
                type.IsGenericType)
            {
                Exception.Throw <SingletonException>("Type {0} is not qualify for a singleton", type.FullName);
            }

            ISingleton inst = null;

            if (BaseType.SingletonBehaviour == baseType)
            {
                GameObject go = new GameObject(type.Name, type);
                go.transform.parent = root.transform;
                go.transform.Reset();
                inst = (ISingleton)go.GetComponent(type);
            }
            else if (BaseType.Singleton == baseType)
            {
                inst = (ISingleton)Activator.CreateInstance(type, args);
            }

            if (null == inst)
            {
                Exception.Throw <SingletonException>("Failed to create singleton: " + type.FullName);
            }

            type.InvokeMember(
                "sInstance",
                BindingFlags.FlattenHierarchy | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
                null,
                null,
                new object[] { inst });

            root.singletonInstances.Add(inst);
            SingletonLog.InfoFormat("{0} Added", type.FullName);
            return(inst);
        }