Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8467fd9

Browse files
committed
Add Singleton and MonoSingleton
1 parentca0f8c7 commit8467fd9

File tree

7 files changed

+165
-63
lines changed

7 files changed

+165
-63
lines changed

‎Assets/Scripts/GameManager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
usingUnityEngine;
44
usingUnityEngine.SceneManagement;
55

6-
publicclassGameManager:Singleton<GameManager>
6+
publicclassGameManager:MonoSingleton<GameManager>
77
{
88

99
[SerializeField]

‎Assets/Scripts/MonoSingleton.cs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
usingSystem.Collections;
2+
usingSystem.Collections.Generic;
3+
usingUnityEngine;
4+
5+
publicabstractclassMonoSingleton<T>:MonoBehaviourwhereT:Component
6+
{
7+
8+
#region Fields
9+
10+
/// <summary>
11+
/// The instance.
12+
/// </summary>
13+
privatestaticTinstance;
14+
15+
#endregion
16+
17+
#region Properties
18+
19+
/// <summary>
20+
/// Gets the instance.
21+
/// </summary>
22+
/// <value>The instance.</value>
23+
publicstaticTInstance
24+
{
25+
get
26+
{
27+
if(instance==null)
28+
{
29+
instance=FindObjectOfType<T>();
30+
if(instance==null)
31+
{
32+
GameObjectobj=newGameObject();
33+
obj.name=typeof(T).Name;
34+
instance=obj.AddComponent<T>();
35+
}
36+
}
37+
returninstance;
38+
}
39+
}
40+
41+
#endregion
42+
43+
#region Methods
44+
45+
/// <summary>
46+
/// Use this for initialization.
47+
/// </summary>
48+
protectedvirtualvoidAwake()
49+
{
50+
if(instance==null)
51+
{
52+
instance=thisasT;
53+
DontDestroyOnLoad(gameObject);
54+
}
55+
else
56+
{
57+
Destroy(gameObject);
58+
}
59+
}
60+
61+
#endregion
62+
63+
}

‎Assets/Scripts/MonoSingleton.cs.meta‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Assets/Scripts/SceneSingleton.meta‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Assets/Scripts/Singleton.cs‎

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,82 @@
1-
usingSystem.Collections;
1+
usingSystem.Collections;
22
usingSystem.Collections.Generic;
33
usingUnityEngine;
44

5-
publicabstractclassSingleton<T>:MonoBehaviourwhereT:Component
5+
publicabstractclassSingleton<T>whereT:Singleton<T>,new()
66
{
7-
8-
#region Fields
9-
10-
/// <summary>
11-
/// The instance.
12-
/// </summary>
13-
privatestaticTinstance;
14-
15-
#endregion
16-
17-
#region Properties
18-
19-
/// <summary>
20-
/// Gets the instance.
21-
/// </summary>
22-
/// <value>The instance.</value>
23-
publicstaticTInstance
24-
{
25-
get
26-
{
27-
if(instance==null)
28-
{
29-
instance=FindObjectOfType<T>();
30-
if(instance==null)
31-
{
32-
GameObjectobj=newGameObject();
33-
obj.name=typeof(T).Name;
34-
instance=obj.AddComponent<T>();
35-
}
36-
}
37-
returninstance;
38-
}
39-
}
40-
41-
#endregion
42-
43-
#region Methods
44-
45-
/// <summary>
46-
/// Use this for initialization.
47-
/// </summary>
48-
protectedvirtualvoidAwake()
49-
{
50-
if(instance==null)
51-
{
52-
instance=thisasT;
53-
DontDestroyOnLoad(gameObject);
54-
}
55-
else
56-
{
57-
Destroy(gameObject);
58-
}
59-
}
60-
61-
#endregion
62-
7+
8+
#region Fields
9+
10+
/// <summary>
11+
/// The instance.
12+
/// </summary>
13+
privatestaticTinstance;
14+
15+
#endregion
16+
17+
#region Properties
18+
19+
/// <summary>
20+
/// Gets the instance.
21+
/// </summary>
22+
/// <value>The instance.</value>
23+
publicstaticTInstance
24+
{
25+
get
26+
{
27+
if(Instance==null)
28+
{
29+
//ensure that only one thread can execute
30+
lock(typeof(T))
31+
{
32+
if(Instance==null)
33+
{
34+
instance=newT();
35+
instance.Init();
36+
}
37+
}
38+
}
39+
40+
returninstance;
41+
}
42+
}
43+
44+
45+
#endregion
46+
47+
#region Methods
48+
49+
publicstaticvoidCreateInstance()
50+
{
51+
DestroyInstance();
52+
instance=Instance;
53+
}
54+
55+
publicstaticvoidDestroyInstance()
56+
{
57+
if(instance==null)return;
58+
59+
instance.Clear();
60+
instance=default(T);
61+
}
62+
63+
protectedvoidInit()
64+
{
65+
OnInit();
66+
}
67+
68+
publicvoidClear()
69+
{
70+
OnClear();
71+
}
72+
73+
protectedvirtualvoidOnInit()
74+
{
75+
}
76+
77+
protectedvirtualvoidOnClear()
78+
{
79+
}
80+
#endregion
81+
6382
}

‎Assets/Scripts/Singleton.cs.meta‎

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎ProjectSettings/ProjectVersion.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
m_EditorVersion: 2017.1.0p4
1+
m_EditorVersion: 2021.3.6f1c1
2+
m_EditorVersionWithRevision: 2021.3.6f1c1 (07401303b748)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp