Skip to content

Commit d703ed5

Browse files
committed
- add core
1 parent 9ced1f9 commit d703ed5

34 files changed

+995
-3
lines changed

Runtime/Core.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.

Runtime/Core/App.cs

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
using System;
2+
using System.Collections;
3+
using UnityEngine;
4+
using UnityEngine.Internal;
5+
6+
namespace VirtueSky.Core
7+
{
8+
public struct App
9+
{
10+
private static MonoGlobal _monoGlobal;
11+
12+
public static void InitMonoGlobalComponent(MonoGlobal monoGlobal)
13+
{
14+
App._monoGlobal = monoGlobal;
15+
}
16+
17+
public static void AddPauseCallback(Action<bool> callback)
18+
{
19+
_monoGlobal.OnGamePause -= callback;
20+
_monoGlobal.OnGamePause += callback;
21+
}
22+
23+
public static void RemovePauseCallback(Action<bool> callback)
24+
{
25+
_monoGlobal.OnGamePause -= callback;
26+
}
27+
28+
public static void AddFocusCallback(Action<bool> callback)
29+
{
30+
_monoGlobal.OnGameFocus -= callback;
31+
_monoGlobal.OnGameFocus += callback;
32+
}
33+
34+
public static void RemoveFocusCallback(Action<bool> callback)
35+
{
36+
_monoGlobal.OnGameFocus -= callback;
37+
}
38+
39+
public static void AddQuitCallback(Action callback)
40+
{
41+
_monoGlobal.OnGameQuit -= callback;
42+
_monoGlobal.OnGameQuit += callback;
43+
}
44+
45+
public static void RemoveQuitCallback(Action callback)
46+
{
47+
_monoGlobal.OnGameQuit -= callback;
48+
}
49+
50+
#region Update
51+
52+
internal static void SubTick(IEntity tick)
53+
{
54+
_monoGlobal.AddTick(tick);
55+
}
56+
57+
public static void SubTick(Action action)
58+
{
59+
_monoGlobal.AddTick(action);
60+
}
61+
62+
internal static void SubFixedTick(IEntity fixedTick)
63+
{
64+
_monoGlobal.AddFixedTick(fixedTick);
65+
}
66+
67+
public static void SubFixedTick(Action action)
68+
{
69+
_monoGlobal.AddFixedTick(action);
70+
}
71+
72+
internal static void SubLateTick(IEntity lateTick)
73+
{
74+
_monoGlobal.AddLateTick(lateTick);
75+
}
76+
77+
public static void SubLateTick(Action action)
78+
{
79+
_monoGlobal.AddLateTick(action);
80+
}
81+
82+
internal static void UnSubTick(IEntity tick)
83+
{
84+
_monoGlobal.RemoveTick(tick);
85+
}
86+
87+
public static void UnSubTick(Action action)
88+
{
89+
_monoGlobal.RemoveTick(action);
90+
}
91+
92+
internal static void UnSubFixedTick(IEntity fixedTick)
93+
{
94+
_monoGlobal.RemoveFixedTick(fixedTick);
95+
}
96+
97+
public static void UnSubFixedTick(Action action)
98+
{
99+
_monoGlobal.RemoveFixedTick(action);
100+
}
101+
102+
internal static void UnSubLateTick(IEntity lateTick)
103+
{
104+
_monoGlobal.RemoveLateTick(lateTick);
105+
}
106+
107+
public static void UnSubLateTick(Action action)
108+
{
109+
_monoGlobal.RemoveLateTick(action);
110+
}
111+
112+
#endregion
113+
114+
#region DelayHandle
115+
116+
/// <summary>
117+
/// Delay call
118+
/// </summary>
119+
/// <param name="duration">The duration to wait before the DelayHandle fires.</param>
120+
/// <param name="onComplete">The action to run when the DelayHandle elapses.</param>
121+
/// <param name="onUpdate">A function to call each tick of the DelayHandle. Takes the number of seconds elapsed since
122+
/// the start of the current cycle.</param>
123+
/// <param name="isLooped">Whether the DelayHandle should restart after executing.</param>
124+
/// <param name="useRealTime">Whether the DelayHandle uses real-time(not affected by slow-mo or pausing) or
125+
/// game-time(affected by time scale changes).</param>
126+
/// <returns></returns>
127+
public static DelayHandle Delay(float duration, Action onComplete, Action<float> onUpdate = null,
128+
bool isLooped = false, bool useRealTime = false)
129+
{
130+
var timer = new DelayHandle(duration,
131+
onComplete,
132+
onUpdate,
133+
isLooped,
134+
useRealTime,
135+
null);
136+
_monoGlobal.RegisterDelayHandle(timer);
137+
return timer;
138+
}
139+
140+
141+
/// <summary>
142+
/// Safe Delay call when it had target, progress delay will be cancel when target was destroyed
143+
/// </summary>
144+
/// <param name="duration">The duration to wait before the DelayHandle fires.</param>
145+
/// <param name="onComplete">The action to run when the DelayHandle elapses.</param>
146+
/// <param name="onUpdate">A function to call each tick of the DelayHandle. Takes the number of seconds elapsed since
147+
/// the start of the current cycle.</param>
148+
/// <param name="isLooped">Whether the DelayHandle should restart after executing.</param>
149+
/// <param name="useRealTime">Whether the DelayHandle uses real-time(not affected by slow-mo or pausing) or
150+
/// game-time(affected by time scale changes).</param>
151+
/// <param name="target">The target (behaviour) to attach this DelayHandle to.</param>
152+
public static DelayHandle Delay(
153+
MonoBehaviour target,
154+
float duration,
155+
Action onComplete,
156+
Action<float> onUpdate = null,
157+
bool isLooped = false,
158+
bool useRealTime = false)
159+
{
160+
var timer = new DelayHandle(duration,
161+
onComplete,
162+
onUpdate,
163+
isLooped,
164+
useRealTime,
165+
target);
166+
_monoGlobal.RegisterDelayHandle(timer);
167+
return timer;
168+
}
169+
170+
public static void CancelDelay(DelayHandle delayHandle)
171+
{
172+
delayHandle?.Cancel();
173+
}
174+
175+
public static void PauseDelay(DelayHandle delayHandle)
176+
{
177+
delayHandle?.Pause();
178+
}
179+
180+
public static void ResumeDelay(DelayHandle delayHandle)
181+
{
182+
delayHandle?.Resume();
183+
}
184+
185+
public static void CancelAllDelay()
186+
{
187+
_monoGlobal.CancelAllDelayHandle();
188+
}
189+
190+
public static void PauseAllDelay()
191+
{
192+
_monoGlobal.PauseAllDelayHandle();
193+
}
194+
195+
public static void ResumeAllDelay()
196+
{
197+
_monoGlobal.ResumeAllDelayHandle();
198+
}
199+
200+
#endregion
201+
202+
#region Effective
203+
204+
[System.Runtime.CompilerServices.MethodImpl(
205+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
206+
public static Coroutine StartCoroutine(IEnumerator routine) => _monoGlobal.StartCoroutineImpl(routine);
207+
208+
[System.Runtime.CompilerServices.MethodImpl(
209+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
210+
public static Coroutine StartCoroutine(string methodName, [DefaultValue("null")] object value) =>
211+
_monoGlobal.StartCoroutineImpl(methodName, value);
212+
213+
[System.Runtime.CompilerServices.MethodImpl(
214+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
215+
public static Coroutine StartCoroutine(string methodName) => _monoGlobal.StartCoroutineImpl(methodName);
216+
217+
[System.Runtime.CompilerServices.MethodImpl(
218+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
219+
public static void StopCoroutine(IEnumerator routine) => _monoGlobal.StopCoroutineImpl(routine);
220+
221+
[System.Runtime.CompilerServices.MethodImpl(
222+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
223+
public static void StopCoroutine(Coroutine routine) => _monoGlobal.StopCoroutineImpl(routine);
224+
225+
[System.Runtime.CompilerServices.MethodImpl(
226+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
227+
public static void StopCoroutine(string methodName) => _monoGlobal.StopCoroutineImpl(methodName);
228+
229+
[System.Runtime.CompilerServices.MethodImpl(
230+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
231+
public static void StopAllCoroutine() => _monoGlobal.StopAllCoroutinesImpl();
232+
233+
[System.Runtime.CompilerServices.MethodImpl(
234+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
235+
public static Action ToMainThread(Action action) => _monoGlobal.ToMainThreadImpl(action);
236+
237+
[System.Runtime.CompilerServices.MethodImpl(
238+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
239+
public static Action<T> ToMainThread<T>(Action<T> action) => _monoGlobal.ToMainThreadImpl(action);
240+
241+
[System.Runtime.CompilerServices.MethodImpl(
242+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
243+
public static Action<T1, T2> ToMainThread<T1, T2>(Action<T1, T2> action) =>
244+
_monoGlobal.ToMainThreadImpl(action);
245+
246+
[System.Runtime.CompilerServices.MethodImpl(
247+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
248+
public static Action<T1, T2, T3> ToMainThread<T1, T2, T3>(Action<T1, T2, T3> action) =>
249+
_monoGlobal.ToMainThreadImpl(action);
250+
251+
[System.Runtime.CompilerServices.MethodImpl(
252+
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
253+
public static void RunOnMainThread(Action action) => _monoGlobal.RunOnMainThreadImpl(action);
254+
255+
#endregion
256+
}
257+
}

Runtime/Core/App.cs.meta

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

Runtime/Core/BaseMono.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using UnityEngine;
2+
3+
namespace VirtueSky.Core
4+
{
5+
public class BaseMono : MonoBehaviour, IEntity
6+
{
7+
public virtual void OnEnable()
8+
{
9+
SubTick();
10+
}
11+
12+
public virtual void OnDisable()
13+
{
14+
UnSubTick();
15+
}
16+
17+
18+
public virtual void Initialize()
19+
{
20+
}
21+
22+
public virtual void Tick()
23+
{
24+
}
25+
26+
public virtual void LateTick()
27+
{
28+
}
29+
30+
public virtual void FixedTick()
31+
{
32+
}
33+
34+
public virtual void CleanUp()
35+
{
36+
}
37+
38+
void SubTick()
39+
{
40+
App.SubTick(this);
41+
App.SubLateTick(this);
42+
App.SubFixedTick(this);
43+
}
44+
45+
void UnSubTick()
46+
{
47+
App.UnSubTick(this);
48+
App.UnSubLateTick(this);
49+
App.UnSubFixedTick(this);
50+
}
51+
}
52+
}

Runtime/Core/BaseMono.cs.meta

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

Runtime/Core/CacheComponent.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using UnityEngine;
2+
3+
namespace VirtueSky.Core
4+
{
5+
public class CacheComponent<T> : BaseMono
6+
{
7+
public T component;
8+
public Transform CacheTransform { get; private set; }
9+
10+
protected virtual void Awake()
11+
{
12+
if (CacheTransform == null) CacheTransform = transform;
13+
GetCacheComponent();
14+
}
15+
16+
void GetCacheComponent()
17+
{
18+
if (component == null)
19+
{
20+
component = GetComponent<T>();
21+
}
22+
}
23+
#if UNITY_EDITOR
24+
protected virtual void Reset()
25+
{
26+
GetCacheComponent();
27+
}
28+
#endif
29+
}
30+
}

Runtime/Core/CacheComponent.cs.meta

Lines changed: 11 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)