using System;
using System.Collections;
using System.Collections.Generic;
public class Singleton : MonoBehaviour
{
private static GameObject m_Container = null;
private static string m_Name = "Singleton";
private static Dictionary<string, object> m_SingletonMap = new Dictionary<string, object>();
private static bool m_IsDestroying = false;
public static bool IsDestroying
{
get { return m_IsDestroying; }
}
public static bool IsCreatedInstance(string Name)
{
if(m_Container == null)
{
return false;
}
if (m_SingletonMap!=null && m_SingletonMap.ContainsKey(Name))
{
return true;
}
return false;
}
public static object getInstance (string Name)
{
if(m_Container == null)
{
Debug.Log("Create Singleton.");
m_Container = new GameObject ();
m_Container.name = m_Name;
m_Container.AddComponent (typeof(Singleton));
}
if (!m_SingletonMap.ContainsKey(Name)) {
if(System.Type.GetType(Name) != null)
{
m_SingletonMap.Add(Name, m_Container.AddComponent (System.Type.GetType(Name)));
}
else
{
Debug.LogWarning("Singleton Type ERROR! (" + Name + ")");
}
}
return m_SingletonMap[Name];
}
public void RemoveInstance(string Name)
{
if (m_Container != null && m_SingletonMap.ContainsKey(Name))
{
UnityEngine.Object.Destroy((UnityEngine.Object)(m_SingletonMap[Name]));
m_SingletonMap.Remove(Name);
Debug.LogWarning("Singleton REMOVE! (" + Name + ")");
}
}
void Awake ()
{
Debug.Log("Awake Singleton.");
DontDestroyOnLoad (gameObject);
}
void Start()
{
Debug.Log("Start Singleton.");
}
void Update()
{
}
void OnApplicationQuit()
{
Debug.Log("Destroy Singleton");
if(m_Container != null)
{
GameObject.Destroy(m_Container);
m_Container = null;
m_IsDestroying = true;
}
}
}
// 该类不是一个公开类
// 但是该类的实现应用了单例模式
internal sealed class SR
{
private static SR loader;
internal SR()
{
}
// 主要是因为该类不是公有,所以这个全部访问点也定义为私有的了
// 但是思想还是用到了单例模式的思想的
private static SR GetLoader()
{
if (loader == null)
{
SR sr = new SR();
Interlocked.CompareExchange<SR>(ref loader, sr, null);
}
return loader;
}
// 这个公有方法中调用了GetLoader方法的
public static object GetObject(string name)
{
SR loader = GetLoader();
if (loader == null)
{
return null;
}
return loader.resources.GetObject(name, Culture);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有