Skip to content

Commit

Permalink
feat: Add data transfer to instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jan 11, 2025
1 parent 3c65506 commit 922be71
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions Assets/JCSUnity/Scripts/Interfaces/JCS_Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class JCS_Instance<T> : MonoBehaviour
/// <summary>
/// Singleton instance interface to keep the old instance.
/// </summary>
public class JCS_InstanceOld<T> : JCS_Instance<T>
public abstract class JCS_InstanceOld<T> : JCS_Instance<T>
where T : MonoBehaviour
{
/* Variables */
Expand All @@ -40,25 +40,43 @@ public class JCS_InstanceOld<T> : JCS_Instance<T>
/// <summary>
/// Check singleton for keep the old one.
/// </summary>
/// <param name="_new"> Current instance. </param>
protected void CheckInstance(T _new)
/// <param name="_old"> old instance </param>
/// <param name="_new"> new instance </param>
protected void CheckInstance(T _old, T _new)
{
// Destory the new one; and keep the old one.
if (instance != null)
{
// only if needed
TransferData(_old, _new);

// Destory the new one; and keep the old one.
Destroy(_new.gameObject);

return;
}

// Only assign once!
instance = _new;
}

/// <summary>
/// Instead of Unity Engine's scripting layer's DontDestroyOnLoad.
/// I would like to use own define to transfer the old instance
/// to the newer instance.
///
/// Every time when unity load the scene. The script have been
/// reset, in order not to lose the original setting.
/// transfer the data from old instance to new instance.
/// </summary>
/// <param name="_old"> old instance </param>
/// <param name="_new"> new instance </param>
protected abstract void TransferData(T _old, T _new);
}

/// <summary>
/// Singleton instance interface to keep the new instance.
/// </summary>
public class JCS_InstanceNew<T> : JCS_Instance<T>
public abstract class JCS_InstanceNew<T> : JCS_Instance<T>
where T : MonoBehaviour
{
/* Variables */
Expand All @@ -70,15 +88,34 @@ public class JCS_InstanceNew<T> : JCS_Instance<T>
/// <summary>
/// Check singleton for keep the new one.
/// </summary>
/// <param name="_new"> Current instance. </param>
protected void CheckInstance(T _new)
/// <param name="_old"> old instance </param>
/// <param name="_new"> new instance </param>
protected void CheckInstance(T _old, T _new)
{
// Destory the old one!
if (instance != null)
{
// only if needed
TransferData(_old, _new);

// Destory the old one!
Destroy(instance);
}

// Assign the new one!
instance = _new;
}

/// <summary>
/// Instead of Unity Engine's scripting layer's DontDestroyOnLoad.
/// I would like to use own define to transfer the old instance
/// to the newer instance.
///
/// Every time when unity load the scene. The script have been
/// reset, in order not to lose the original setting.
/// transfer the data from old instance to new instance.
/// </summary>
/// <param name="_old"> old instance </param>
/// <param name="_new"> new instance </param>
protected abstract void TransferData(T _old, T _new);
}
}

0 comments on commit 922be71

Please sign in to comment.