当前位置:   article > 正文

unity3d:单例模式,Mono场景唯一,不销毁;C# where T:new(),泛型约束;Lua单例模式,table ,self_unity 场景中只有唯一组件的标签

unity 场景中只有唯一组件的标签

Mono单例

  1. 场景里挂载了,先找场景里有的
  2. DontDestroyOnLoad
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Singleton
{
    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        protected static T m_instance = null;


        public static T instance
        {
            get
            {
                if (m_instance == null) 
                {
                    string name = typeof(T).ToString();
                    GameObject gameEntryInstance = GameObject.Find(name); //单例的名字都唯一,防止场景里已经有了
                    if (gameEntryInstance == null)
                    {
                        gameEntryInstance = new GameObject(name);
                        DontDestroyOnLoad(gameEntryInstance);
                    }
                    if (gameEntryInstance != null)
                    {
                        m_instance = gameEntryInstance.GetComponent<T>();
                    }
                    if (m_instance == null)
                    {
                        m_instance = gameEntryInstance.AddComponent<T>();
                    }
                }

                return m_instance;
            }
        }

        public void StartUp()
        {

        }
        protected void OnApplicationQuit()
        {
            m_instance = null;
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

c#单例

在对泛型的约束中,最常使用的关键字有where 和 new。
其中where关键字是约束所使用的泛型,该泛型必须是where后面的类,或者继承自该类。
new()说明所使用的泛型,必须具有无参构造函数,这是为了能够正确的初始化对象

/// <summary>
 /// C#单例模式
 /// </summary>
 public abstract class Singleton<T> where T : class,new()
 {
     private static T instance;
     private static object syncRoot = new Object();
     public static T Instance
     {
         get
         {
             if (instance == null)
             {
                 lock (syncRoot)
                 {
                     if (instance == null)
                         instance = new T();
                 }
             }
             return instance;
         }
     }

     protected Singleton()
     {
         Init();
     }

     public virtual void Init() { }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Lua单例

【腾讯文档】XLua中面向对象,多态,重载,私有,单例
https://docs.qq.com/doc/DWlBsSUljbGZOVFZN
使用GetInstance访问,每次只返回 唯一的new 的table
在lua中,表拥有一个标识:self。self类似于this指针,大多数面向对象语言都隐藏了这个机制,在编码时不需要显示的声明这个参数,就可以在方法内使用this(例如C++和C#)。在lua中,提供了冒号操作符来隐藏这个参数
Singleton.lua

local function __init(self)
   assert(rawget(self._class_type, "Instance") == nil, self._class_type.__cname.." to create singleton twice!")
   rawset(self._class_type, "Instance", self)
end

local function __delete(self)
   rawset(self._class_type, "Instance", nil)
end

local function GetInstance(self)
   if rawget(self, "Instance") == nil then
      rawset(self, "Instance", self.New())
   end
   assert(self.Instance ~= nil)
   return self.Instance
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

如何创建

local ResourcesManager = BaseClass("ResourcesManager", Singleton)
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/332847
推荐阅读
相关标签
  

闽ICP备14008679号