-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingleton.cs
97 lines (94 loc) · 3.28 KB
/
Singleton.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* File: Singleton.cs
* ==================
*
* Version : 1.1
* Author : Kleber Lopes da Silva (@kleber_swf / www.linkedin.com/in/kleberswf)
* E-Mail : Not Available
* Copyright : Not Available
* Company : Not Available
* Script Location : Plugins/INICPlugins/Other
*
*
* Created By : Kleber Lopes da Silva
* Created Date : 2014.11.24
*
* Last Modified by : Ankur Ranpariya on 2014.11.28 ([email protected] / @PA_HeartBeat)
* Last Modified : 2014.11.28
*
* Contributors :
* Curtosey By : Kleber Lopes da Silva (http://kleber-swf.com/singleton-monobehaviour-unity-projects/)
*
* Purpose
* ====================================================================================================================
*
*
*
*
* ====================================================================================================================
* LICENCE / WARRENTY
* ==================
* Copyright (c) 2014 <<Developer Name / Company name>>
* Please direct any bugs/comments/suggestions to <<support email id>>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
*
* Change Log
* ====================================================================================================================
* v1.0
* ====
* 1. Initial version by "Kleber Lopes da Silva"
*
* v1.1
* ====
* 1. Change DestroyImmediate to Destroy for removing Extra copies of singleton instance, DestroyImmediate wiil remove
* copy imeediately and effect in same frame. It should cause null excption error if destroyed copy is referanced.
* whether Destroy will remove instance end of the frame so it will not cause Null exception in same frame, and next
* frame can identify used referance are removed or not.
* 2. Now it will throw Exception insted of logging error.
* ====================================================================================================================
*/
using UnityEngine;
using System;
/// <summary>
/// <para version="1.1.0.0" />
/// <para author="Kleber Lopes da Silva, Ranpariya Ankur" />
/// <para support="" />
/// <para>
/// Description:
/// </para>
/// </summary>
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
private static T _instance;
public static T Me {
get {
if(_instance == null) {
Type type = typeof(T);
throw new System.NullReferenceException(type.ToString() + " not present in scene");
}
return _instance;
}
protected internal set {
if(value == null) {
if(_instance && _instance.gameObject) {
Destroy(_instance.gameObject);
}
_instance = null;
} else {
_instance = value;
var type = typeof(T);
var attribute = Attribute.GetCustomAttribute(type, typeof(SignletonSetting)) as SignletonSetting;
if(attribute != null && attribute.Persistent) {
DontDestroyOnLoad(_instance.gameObject);
}
}
}
}
}