Skip to content

Commit

Permalink
Add GlobalTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
IQzhan committed Nov 25, 2021
1 parent 45a50f3 commit 9fe059f
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 177 deletions.
Binary file added Documentation~/Global System.pdf
Binary file not shown.
Binary file added Documentation~/Global System.xmind
Binary file not shown.
Binary file removed Documentation~/GlobalBehaviour.pdf
Binary file not shown.
Binary file removed Documentation~/GlobalBehaviour.xmind
Binary file not shown.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# Global module
# Global System

## class GlobalBehavioour & BehaviourManager
### Descript
Simulate monobehaviour's methods like 'Awake/OnEnable/Update/OnDisable/OnDestroy'.
### Usage
See Assets/Example and source code.
## class GlobalTime
### Descript
Global system time information.

## class ScriptableObjectAssetsCreator
### Descript
Create ScriptableObject asset.
### Usage
Right click ScriptableObject script file and click menu "Create -> ScriptableObject To Asset".
## class Singleton
### Descript
Makes MonoBehaviour singleton.
### Usage
See class BehaviourUpdater.
Right click ScriptableObject script file and click menu "Create -> ScriptableObject To Asset".
168 changes: 31 additions & 137 deletions Runtime/BehaviourManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace E
Expand All @@ -25,21 +24,9 @@ public sealed partial class BehaviourManager
public static bool IsReady { get => instance.m_IsReady; }

/// <summary>
/// Use which method to update.
/// see <see cref="GlobalSettings.Method"/>
/// </summary>
public static GlobalSettings.UpdateMethod UpdateMethod { get => GlobalSettings.Method; }

/// <summary>
/// Update delta time.
/// see <see cref="GlobalSettings.DeltaTime"/>
/// </summary>
public static float DeltaTime { get => GlobalSettings.DeltaTime; }

/// <summary>
/// Get instance of <see cref="BehaviourUpdater"/>.
/// Get instance of <see cref="GlobalUpdater"/>.
/// </summary>
public static BehaviourUpdater MonoBehaviour { get => BehaviourUpdater.Instance; }
public static GlobalUpdater Updater { get => GlobalUpdater.Instance; }

/// <summary>
/// Call before auto create <see cref="GlobalBehaviour"/> instances,
Expand All @@ -61,23 +48,23 @@ public sealed partial class BehaviourManager
public static event Func<TypeInfo, GlobalBehaviour> OverrideCreateInstanceCallback;

/// <summary>
/// Call by <see cref="BehaviourUpdater.FixedUpdate"/>.
/// Call by <see cref="GlobalUpdater.FixedUpdate"/>.
/// </summary>
public static event Action FixedUpdateCallback;

/// <summary>
/// Call <see cref="BehaviourUpdater.Update"/>.
/// Call <see cref="GlobalUpdater.Update"/>.
/// </summary>
public static event Action UpdateCallback;

/// <summary>
/// Call by <see cref="BehaviourUpdater.LateUpdate"/>.
/// Call by <see cref="GlobalUpdater.LateUpdate"/>.
/// </summary>
public static event Action LateUpdateCallback;

#if UNITY_EDITOR
/// <summary>
/// Call by <see cref="BehaviourUpdater.OnDrawGizmos"/>.
/// Call by <see cref="GlobalUpdater.OnDrawGizmos"/>.
/// </summary>
public static event Action OnDrawGizmosCallback;
#endif
Expand All @@ -92,6 +79,8 @@ public sealed partial class BehaviourManager

private bool m_IsReady;

private bool m_FirstUpdated;

/// <summary>
/// All <see cref="GlobalBehaviour"/>'s <see cref="TypeInfo"/>
/// </summary>
Expand All @@ -102,16 +91,6 @@ public sealed partial class BehaviourManager
/// </summary>
private BehaviourCollection m_Collection;

/// <summary>
/// For calculate time.
/// </summary>
private System.Diagnostics.Stopwatch m_Stopwatch;

/// <summary>
/// Last update time.
/// </summary>
private double m_LastTime;

/// <summary>
/// Use to queue <see cref="GlobalBehaviour"/>'s id need to enable.
/// </summary>
Expand Down Expand Up @@ -147,6 +126,7 @@ private enum StateToCheck
/// <returns></returns>
public static T CreateInstance<T>() where T : GlobalBehaviour
{
if (!instance.m_IsReady) return default;
return instance.InternalCreateInstance<T>();
}

Expand All @@ -157,6 +137,7 @@ public static T CreateInstance<T>() where T : GlobalBehaviour
/// <returns></returns>
public static GlobalBehaviour CreateInstance(in Type type)
{
if (!instance.m_IsReady) return default;
return instance.InternalCreateInstance(type);
}

Expand All @@ -167,6 +148,7 @@ public static GlobalBehaviour CreateInstance(in Type type)
/// <returns></returns>
public static T GetInstance<T>() where T : GlobalBehaviour
{
if (!instance.m_IsReady) return default;
return instance.InternalGetInstance<T>();
}

Expand All @@ -177,6 +159,7 @@ public static T GetInstance<T>() where T : GlobalBehaviour
/// <returns></returns>
public static GlobalBehaviour GetInstance(in Type type)
{
if (!instance.m_IsReady) return default;
return instance.InternalGetInstance(type);
}

Expand All @@ -187,6 +170,7 @@ public static GlobalBehaviour GetInstance(in Type type)
/// <returns></returns>
public static T[] GetInstances<T>() where T : GlobalBehaviour
{
if (!instance.m_IsReady) return default;
return instance.InternalGetInstances<T>();
}

Expand All @@ -197,6 +181,7 @@ public static T[] GetInstances<T>() where T : GlobalBehaviour
/// <returns></returns>
public static GlobalBehaviour[] GetInstances(in Type type)
{
if (!instance.m_IsReady) return default;
return instance.InternalGetInstances(type);
}

Expand All @@ -206,6 +191,7 @@ public static GlobalBehaviour[] GetInstances(in Type type)
/// <param name="behaviour"></param>
public static void DestroyInstance(in GlobalBehaviour behaviour)
{
if (!instance.m_IsReady) return;
instance.InternalDestroyInstance(behaviour);
}

Expand All @@ -222,91 +208,21 @@ private BehaviourManager() { }

~BehaviourManager() { Destroy(); }

#if UNITY_EDITOR
// Execute these editor methods by <1> <2> <3> order.

[InitializeOnLoadMethod]
private static void InitializeOnLoadInEditor()
{
// <1> Execute when
// <open the editor>
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
EditorApplication.update -= CheckBehaviourUpdater;
EditorApplication.update += CheckBehaviourUpdater;
}
internal static void InitializeOnLoad() => instance.Initialize();

[UnityEditor.Callbacks.DidReloadScripts]
private static void DidReloadScripts()
{
// <2> Execute when
// <open the editor>
// <enter play mode from editor mode>
// <enter editor mode from play mode>
// <reload assemblies>
DestroyOnExit();
InitializeOnLoad();
}

private static void OnPlayModeStateChanged(PlayModeStateChange stateChange)
{
// <3> Execute when
// <enter editor mode from play mode>
// <enter play mode from editor mode>
// <exit editor mode>
// <exit play mode>
switch (stateChange)
{
case PlayModeStateChange.EnteredEditMode:
case PlayModeStateChange.EnteredPlayMode:
InitializeOnLoad();
break;
case PlayModeStateChange.ExitingEditMode:
case PlayModeStateChange.ExitingPlayMode:
DestroyOnExit();
break;
}
}

private static void CheckBehaviourUpdater()
{
BehaviourUpdater.CreateInstance();
}
#else
// Execute at runtime after builded

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
private static void InitializeOnLoadAtRuntime()
{
InitializeOnLoad();
Application.quitting -= DestroyOnExit;
Application.quitting += DestroyOnExit;
}
#endif

private static void InitializeOnLoad()
{
instance.Initialize();
// Create BehaviourUpdater for update.
BehaviourUpdater.CreateInstance();
}

private static void DestroyOnExit()
{
instance.Destroy();
}
internal static void DestroyOnExit() => instance.Destroy();

private void Initialize()
{
try
{
// make sure Initialize only once
if (m_IsReady) return;
CreateStopwatch();
CreateLifeCycleQueues();
IEnumerable<Type> types = GetAllTypes();
ExecuteBefore(types);
CreateTypeInfos(types);
m_FirstUpdated = false;
m_IsReady = true;
}
catch (Exception e)
Expand All @@ -324,11 +240,11 @@ private void Destroy()
{
// make sure Destroy only once
if (!m_IsReady) return;
m_FirstUpdated = false;
ReleaseCollection();
ReleaseTypeInfos();
ReleaseLifeCycleQueues();
ClearCallbacks();
ClearStopwatch();
m_IsReady = false;
}
catch (Exception e)
Expand All @@ -341,20 +257,6 @@ private void Destroy()
}
}

private void CreateStopwatch()
{
m_LastTime = 0;
m_Stopwatch = new System.Diagnostics.Stopwatch();
m_Stopwatch.Start();
}

private void ClearStopwatch()
{
m_Stopwatch.Stop();
m_Stopwatch = null;
m_LastTime = 0;
}

private IEnumerable<Type> GetAllTypes()
{
return AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes());
Expand Down Expand Up @@ -579,20 +481,17 @@ private bool GetTypeInfo(int typeHashCode, out TypeInfo typeInfo)
return true;
}

internal void FixedUpdate()
{ CallUpdate(GlobalSettings.UpdateMethod.FixedUpdate, FixedUpdateCallback); }
internal void FixedUpdate(bool allowUpdate) => CallUpdate(allowUpdate, FixedUpdateCallback);

internal void Update()
{ CallUpdate(GlobalSettings.UpdateMethod.Update, UpdateCallback); }
internal void Update(bool allowUpdate) => CallUpdate(allowUpdate, UpdateCallback);

internal void LateUpdate()
{ CallUpdate(GlobalSettings.UpdateMethod.LateUpdate, LateUpdateCallback); }
internal void LateUpdate(bool allowUpdate) => CallUpdate(allowUpdate, LateUpdateCallback);

private void CallUpdate(GlobalSettings.UpdateMethod updateMethod, in Action updateCallback)
private void CallUpdate(bool allowUpdate, in Action updateCallback)
{
if (m_IsReady)
{
if (UpdateMethod == updateMethod) UpdateLifeCycle();
if (allowUpdate) UpdateLifeCycle();
LogTryCatchEvent(updateCallback);
}
}
Expand All @@ -619,19 +518,14 @@ private void LogTryCatchEvent(in Action action)

private void UpdateLifeCycle()
{
double seconds = m_Stopwatch.Elapsed.TotalSeconds;
if (seconds - m_LastTime >= DeltaTime)
if (!m_FirstUpdated)
{
if(m_LastTime == 0)
{
//first frame.
AutoCreateInstances();
}
m_LastTime = seconds;
ClearLifeCycleQueues();
CheckAllLifeCycleState();
InternalLifeCycleBody();
AutoCreateInstances();
m_FirstUpdated = true;
}
ClearLifeCycleQueues();
CheckAllLifeCycleState();
InternalLifeCycleBody();
}

private void CheckAllLifeCycleState()
Expand Down
2 changes: 1 addition & 1 deletion Runtime/GlobalBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
/// <summary>
/// Simulate <see cref="UnityEngine.MonoBehaviour"/>'s life circle,
/// all <see cref="GlobalBehaviour"/>s are in one single <see cref="BehaviourUpdater"/>.
/// all <see cref="GlobalBehaviour"/>s are in one single <see cref="GlobalUpdater"/>.
/// <para>See also:
/// <seealso cref="BehaviourManager"/>,
/// <seealso cref="TypeInfo"/>,
Expand Down
7 changes: 5 additions & 2 deletions Runtime/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace E
{
/// <summary>
/// Settings of global system.
/// </summary>
public class GlobalSettings : ScriptableObject
{
public enum UpdateMethod
Expand Down Expand Up @@ -31,7 +34,7 @@ private static GlobalSettings Instance
/// <summary>
/// Update delta time.
/// </summary>
public static float DeltaTime { get => Instance.m_DeltaTime; set => Instance.m_DeltaTime = value; }
public static double DeltaTime { get => Instance.m_DeltaTime; set => Instance.m_DeltaTime = value; }

/// <summary>
/// Use which method to update.
Expand All @@ -43,7 +46,7 @@ private static GlobalSettings Instance
public static bool AllowLogError { get => Instance.m_AllowLogError; set => Instance.m_AllowLogError = value; }

[SerializeField, Min(0.001f)]
private float m_DeltaTime = 1f / 130f;
private double m_DeltaTime = 1f / 130f;

[SerializeField]
private UpdateMethod m_Method = UpdateMethod.Update;
Expand Down
Loading

0 comments on commit 9fe059f

Please sign in to comment.