Skip to content

Commit 0720ce4

Browse files
author
Mogoson
committed
Adjust project structure
1 parent 7f15ca6 commit 0720ce4

40 files changed

+703
-107
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[TOC]
2+
13
# MGS.ObjectPool
24

35
## Summary
@@ -54,12 +56,12 @@
5456
{
5557
public void Reset()
5658
{
57-
//Reset the object.
59+
//TODO: Reset the object.
5860
}
5961

6062
public void Dispose()
6163
{
62-
//Dispose the object.
64+
//TODO: Dispose the object.
6365
}
6466
}
6567

Binary file not shown.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
[TOC]
2+
3+
# MGS.DesignPattern.dll
4+
5+
## Summary
6+
7+
- Design pattern code for C# project develop.
8+
9+
## Environment
10+
11+
- .Net Framework 3.5 or above.
12+
13+
## Dependence
14+
15+
- System.dll
16+
17+
## Demand
18+
19+
- Provide a single instance of the specified type T.
20+
- Generic object pool.
21+
22+
## Implemented
23+
24+
### ObjectPool
25+
26+
```C#
27+
/// <summary>
28+
/// Generic object pool.
29+
/// </summary>
30+
/// <typeparam name="T">Specified type of object.</typeparam>
31+
public abstract class ObjectPool<T>{}
32+
33+
/// <summary>
34+
/// Generic object pool.
35+
/// </summary>
36+
public class GenericPool<T> : ObjectPool<T> where T : IResettable, new()
37+
```
38+
39+
### Singleton
40+
41+
```C#
42+
/// <summary>
43+
/// Provide a single instance of the specified type T;
44+
/// Inheritance class should with the sealed access modifier
45+
/// and a private parameterless constructor to ensure singleton.
46+
/// </summary>
47+
/// <typeparam name="T">Specified type.</typeparam>
48+
public abstract class Singleton<T> where T : class{}
49+
50+
/// <summary>
51+
/// Provide a single instance with a timer to tick update for the specified type T;
52+
/// Inheritance class should with the sealed access modifier
53+
/// and a private parameterless constructor to ensure singleton.
54+
/// </summary>
55+
/// <typeparam name="T">Specified type.</typeparam>
56+
public abstract class SingleTimer<T> : Singleton<T> where T : class{}
57+
```
58+
59+
## Usage
60+
61+
### ObjectPool
62+
63+
```C#
64+
//Implement custom object.
65+
public class CustomObject : IResettable
66+
{
67+
public void Reset()
68+
{
69+
//TODO: Reset the object.
70+
}
71+
72+
public void Dispose()
73+
{
74+
//TODO: Dispose the object.
75+
}
76+
}
77+
78+
//Use GenericPool in your class.
79+
public class TestCase
80+
{
81+
public TestCase()
82+
{
83+
//Create pool for CustomObject.
84+
var pool = new GenericPool<CustomObject>();
85+
86+
//Take a instance of CustomObject from pool.
87+
var obj = pool.Take();
88+
89+
//Recycle object to pool if we do not need it.
90+
pool.Recycle(obj);
91+
}
92+
}
93+
```
94+
95+
### Singleton
96+
97+
```C#
98+
//Custom classs with a single instance.
99+
public sealed class TestSingleton : Singleton<TestSingleton>
100+
{
101+
public string testField = "Test Field";
102+
103+
//Private parameterless constructor to ensure singleton.
104+
private TestSingleton() { }
105+
}
106+
107+
//Use Instance to accessing fields, properties and methods.
108+
var testInfo = TestSingleton.Instance.testField;
109+
```
110+
111+
------
112+
113+
[Previous](../../README.md)
114+
115+
------
116+
117+
Copyright © 2021 Mogoson. mogoson@outlook.com

UnityProject/Assets/MGS.Packages/Common/Plugins/MGS.DesignPattern.md.meta

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

UnityProject/Assets/MGS.Packages/Common/Plugins/MGS.DesignPattern.xml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
[TOC]
2+
3+
# MGS.UDesignPattern.dll
4+
5+
## Summary
6+
7+
- Design pattern code for Unity project develop.
8+
9+
## Environment
10+
11+
- Unity 5.0 or above.
12+
- .Net Framework 3.5 or above.
13+
14+
## Dependence
15+
16+
- System.dll
17+
- UnityEngine.dll
18+
- MGS.Logger.dll
19+
- MGS.DesignPattern.dll
20+
21+
## Demand
22+
23+
- Provide a single instance of the specified MonoBehaviour.
24+
- Generic game object pool.
25+
26+
## Implemented
27+
28+
### ObjectPool
29+
30+
```C#
31+
/// <summary>
32+
/// Pool of gameobject.
33+
/// </summary>
34+
public class GOPool : ObjectPool<GameObject>{}
35+
36+
/// <summary>
37+
/// Manager of gameobject pool.
38+
/// </summary>
39+
public sealed class GOPoolManager : Singleton<GOPoolManager>
40+
```
41+
42+
### Singleton
43+
44+
```C#
45+
/// <summary>
46+
/// Generic base class for single Component.
47+
/// Inheritance class should with the sealed access modifier to ensure distinct singleton.
48+
/// </summary>
49+
[DisallowMultipleComponent]
50+
public abstract class SingleComponent<T> : MonoBehaviour where T : Component{}
51+
52+
/// <summary>
53+
/// Generic single behaviour.
54+
/// Auto create the generic instance, do not add this component to any GameObject by yourself.
55+
/// </summary>
56+
public sealed class SingleBehaviour : SingleComponent<SingleBehaviour>{}
57+
```
58+
59+
## Usage
60+
61+
### ObjectPool
62+
1. Create game object pool.
63+
64+
```c#
65+
//The prefab as template of reusable game object.
66+
var pool = GOPoolManager.Instance.CreatePool(poolName, prefab);
67+
```
68+
69+
1. Use pool to Take, Recycle game object.
70+
71+
```C#
72+
//Use pool name to find the instance of pool from manager if we do not hold it.
73+
var pool = GOPoolManager.Instance.FindPool(poolName);
74+
75+
//Take a game object same as prefab.
76+
var go = pool.Take();
77+
78+
//Recycle the game object to pool if we do not need it.
79+
pool.Recycle(go);
80+
81+
//Take a game object and get or add component.
82+
var cpnt = pool.Take<Bullet>();
83+
84+
//Recycle the game object of component to pool if we do not need it.
85+
pool.Recycle(cpnt);
86+
```
87+
88+
### Singleton
89+
90+
- SingleComponent
91+
92+
```C#
93+
//Derived custom single component.
94+
//Inheritance class should with the sealed access modifier to ensure distinct singleton.
95+
public sealed class UIManager : SingleComponent<UIManager>
96+
{
97+
private void Start()
98+
{
99+
//TODO:
100+
}
101+
102+
public RectTransfrom FindUI(string name)
103+
{
104+
//TODO:
105+
}
106+
}
107+
108+
//Use Instance to accessing fields, properties and methods.
109+
var ui = UIManager.Instance.FindUI("UI_Help");
110+
```
111+
112+
- SingleBehaviour
113+
114+
```C#
115+
//Use the properties and methods inherit from MonoBehaviour.
116+
SingleBehaviour.Instance.StartCoroutine(routine);
117+
118+
//Use the extended events.
119+
SingleBehaviour.Instance.OnApplicationQuitEvent += () =>
120+
{
121+
//TODO:
122+
};
123+
```
124+
125+
------
126+
127+
[Previous](../../README.md)
128+
129+
------
130+
131+
Copyright © 2021 Mogoson. mogoson@outlook.com

UnityProject/Assets/MGS.Packages/Common/Plugins/MGS.UDesignPattern.md.meta

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

UnityProject/Assets/MGS.Packages/Common/Plugins/MGS.UDesignPattern.xml

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

0 commit comments

Comments
 (0)