zoukankan      html  css  js  c++  java
  • c#及unity单例模板

    c#:使用泛型实现单例

    using System; 
    using System.Collections.Generic; 
    using System.Text; using System.Reflection;
     /// <summary> /// 1.泛型 /// 2.反射 /// 3.抽象类 /// 4.命名空间 /// </summary>
     namespace QFramework 
    {
      public abstract class QSingleton<T> where T : QSingleton<T> 
      {
        protected static T instance = null; protected QSingleton() { } 
          public static T Instance() 
          {
          if (instance == null) 
          {
                 // 先获取所有非public的构造方法
                 ConstructorInfo[] ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); 
                // 从ctors中获取无参的构造方法 
                ConstructorInfo ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
                 if (ctor == null)
                     throw new Exception("Non-public ctor() not found!"); 
                // 调用构造方法
                 instance = ctor.Invoke(null) as T; 
          } 
        return instance; 
        } 
      } 
    }

    unity实现MonoBehaviour 单例,约束GameObject的个数,这个需求,还没有思路,只好在游戏运行时判断有多少个GameObject已经挂上了该脚本,然后如果个数大于1抛出错误即可。在脚本销毁时,把静态实例置空。 

    using UnityEngine;
     /// <summary> /// 需要使用Unity生命周期的单例模式 /// </summary> 
    namespace QFramework
    { 
      public abstract class QMonoSingleton<T> : MonoBehaviour where T : QMonoSingleton<T> 
      { 
          protected static T instance = null;
        public static T Instance() 
        { 
              if (instance == null) 
              { 
                 instance = FindObjectOfType<T>();
            if (FindObjectsOfType<T>().Length > 1)
                 { 
                    QPrint.FrameworkError ("More than 1!"); 
                    return instance; 
            }
                 if (instance == null) 
            {
                     string instanceName = typeof(T).Name; QPrint.FrameworkLog ("Instance Name: " + instanceName); 
                    GameObject instanceGO = GameObject.Find(instanceName);
                    if (instanceGO == null) instanceGO = new GameObject(instanceName); 
                    instance = instanceGO.AddComponent<T>(); 
                    DontDestroyOnLoad(instanceGO);
                     //保证实例不会被释放 
                    QPrint.FrameworkLog ("Add New Singleton " + instance.name + " in Game!"); 
                }
                 else {
                     QPrint.FrameworkLog ("Already exist: " + instance.name); 
                }
             } 
            return instance; 
        }
       protected virtual void OnDestroy() 
      { instance
    = null;   } }
    一直想把之前工作、学习时记录的文档整理到博客上,一方面温故而知新,一方面和大家一起学习 -程序小白
  • 相关阅读:
    数据库模式
    数据流模式、转换、格式与操作
    桥接模式=抽象层协作关系+继承体系注入
    php 中更简洁的三元运算符 ?:
    larave5.6 将Excel文件数据导入数据库代码实例
    Laravel获取所有的数据库表及结构
    Laravel框架数据库CURD操作、连贯操作总结
    insert into 语句的三种写法
    Laravel 上传excel,读取并写入数据库 (实现自动建表、存记录值
    laravel5.4将excel表格中的信息导入到数据库中
  • 原文地址:https://www.cnblogs.com/wang-jin-fu/p/8313311.html
Copyright © 2011-2022 走看看