Skip to content

Commit 922be71

Browse files
committed
feat: Add data transfer to instance
1 parent 3c65506 commit 922be71

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

Assets/JCSUnity/Scripts/Interfaces/JCS_Instance.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public abstract class JCS_Instance<T> : MonoBehaviour
2828
/// <summary>
2929
/// Singleton instance interface to keep the old instance.
3030
/// </summary>
31-
public class JCS_InstanceOld<T> : JCS_Instance<T>
31+
public abstract class JCS_InstanceOld<T> : JCS_Instance<T>
3232
where T : MonoBehaviour
3333
{
3434
/* Variables */
@@ -40,25 +40,43 @@ public class JCS_InstanceOld<T> : JCS_Instance<T>
4040
/// <summary>
4141
/// Check singleton for keep the old one.
4242
/// </summary>
43-
/// <param name="_new"> Current instance. </param>
44-
protected void CheckInstance(T _new)
43+
/// <param name="_old"> old instance </param>
44+
/// <param name="_new"> new instance </param>
45+
protected void CheckInstance(T _old, T _new)
4546
{
46-
// Destory the new one; and keep the old one.
4747
if (instance != null)
4848
{
49+
// only if needed
50+
TransferData(_old, _new);
51+
52+
// Destory the new one; and keep the old one.
4953
Destroy(_new.gameObject);
54+
5055
return;
5156
}
5257

5358
// Only assign once!
5459
instance = _new;
5560
}
61+
62+
/// <summary>
63+
/// Instead of Unity Engine's scripting layer's DontDestroyOnLoad.
64+
/// I would like to use own define to transfer the old instance
65+
/// to the newer instance.
66+
///
67+
/// Every time when unity load the scene. The script have been
68+
/// reset, in order not to lose the original setting.
69+
/// transfer the data from old instance to new instance.
70+
/// </summary>
71+
/// <param name="_old"> old instance </param>
72+
/// <param name="_new"> new instance </param>
73+
protected abstract void TransferData(T _old, T _new);
5674
}
5775

5876
/// <summary>
5977
/// Singleton instance interface to keep the new instance.
6078
/// </summary>
61-
public class JCS_InstanceNew<T> : JCS_Instance<T>
79+
public abstract class JCS_InstanceNew<T> : JCS_Instance<T>
6280
where T : MonoBehaviour
6381
{
6482
/* Variables */
@@ -70,15 +88,34 @@ public class JCS_InstanceNew<T> : JCS_Instance<T>
7088
/// <summary>
7189
/// Check singleton for keep the new one.
7290
/// </summary>
73-
/// <param name="_new"> Current instance. </param>
74-
protected void CheckInstance(T _new)
91+
/// <param name="_old"> old instance </param>
92+
/// <param name="_new"> new instance </param>
93+
protected void CheckInstance(T _old, T _new)
7594
{
76-
// Destory the old one!
7795
if (instance != null)
96+
{
97+
// only if needed
98+
TransferData(_old, _new);
99+
100+
// Destory the old one!
78101
Destroy(instance);
102+
}
79103

80104
// Assign the new one!
81105
instance = _new;
82106
}
107+
108+
/// <summary>
109+
/// Instead of Unity Engine's scripting layer's DontDestroyOnLoad.
110+
/// I would like to use own define to transfer the old instance
111+
/// to the newer instance.
112+
///
113+
/// Every time when unity load the scene. The script have been
114+
/// reset, in order not to lose the original setting.
115+
/// transfer the data from old instance to new instance.
116+
/// </summary>
117+
/// <param name="_old"> old instance </param>
118+
/// <param name="_new"> new instance </param>
119+
protected abstract void TransferData(T _old, T _new);
83120
}
84121
}

0 commit comments

Comments
 (0)