From 2ad09fc7c2c301ca1657930b704949bd248582e4 Mon Sep 17 00:00:00 2001 From: uurha Date: Fri, 3 May 2024 20:20:42 +0000 Subject: [PATCH] Merge pull request #6 from techno-dwarf-works/bug/monoservice_fix Fix MonoService Token NullReference --- Runtime/Services/MonoService.cs | 31 +++++++++++++++++++++++++++++-- package.json | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Runtime/Services/MonoService.cs b/Runtime/Services/MonoService.cs index b1e31e8..532eb6b 100644 --- a/Runtime/Services/MonoService.cs +++ b/Runtime/Services/MonoService.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; using Better.Services.Runtime.Interfaces; @@ -11,11 +12,32 @@ public abstract class MonoService : MonoBehaviour, IService private CancellationTokenSource _destroyCancellationToken; public bool Initialized { get; private set; } - protected CancellationToken DestroyCancellationToken => _destroyCancellationToken.Token; + + protected CancellationToken DestroyCancellationToken + { + get + { + if (_destroyCancellationToken == null) + { + _destroyCancellationToken = new CancellationTokenSource(); + } + return _destroyCancellationToken.Token; + } + } protected virtual void Awake() { - _destroyCancellationToken = new(); + + } + + protected virtual void OnEnable() + { + + } + + protected virtual void Start() + { + } async Task IService.InitializeAsync(CancellationToken cancellationToken) @@ -56,6 +78,11 @@ Task IService.PostInitializeAsync(CancellationToken cancellationToken) protected abstract Task OnInitializeAsync(CancellationToken cancellationToken); protected abstract Task OnPostInitializeAsync(CancellationToken cancellationToken); + protected virtual void OnDisable() + { + + } + protected virtual void OnDestroy() { _destroyCancellationToken?.Cancel(); diff --git a/package.json b/package.json index ce19b80..a843191 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.services", "displayName": "Better Services", - "version": "0.1.0", + "version": "0.1.1", "unity": "2021.3", "description": " ", "dependencies": {