Skip to content

Instantly share code, notes, and snippets.

@sfider
Last active December 5, 2024 12:07
Show Gist options
  • Save sfider/b35aa90994f77e891e664be7721bff42 to your computer and use it in GitHub Desktop.
Save sfider/b35aa90994f77e891e664be7721bff42 to your computer and use it in GitHub Desktop.
UniTaskOnDisableWrapper helps with tying UniTask execution with MonoBehaviour enabled state
/*
* Copyright 2024 Marcin Swiderski
*
* The MIT Licence (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Cysharp.Threading.Tasks;
using UnityEngine;
public readonly struct UniTaskOnDisableWrapper
{
private readonly MonoBehaviour _parent;
private readonly List<Action> _disabled;
public UniTaskOnDisableWrapper(MonoBehaviour parent, int initCapacity = 4)
{
_parent = parent;
_disabled = new(initCapacity);
}
public AwaitContext Wrap(UniTask uniTask)
{
return AwaitContext.Create(_parent, _disabled, uniTask);
}
public void OnEnable()
{
foreach (Action continuation in _disabled) {
continuation();
}
_disabled.Clear();
}
public class AwaitContext
{
private static readonly Stack<AwaitContext> Pool = new(64);
private readonly Action _continuation;
private MonoBehaviour _parent;
private List<Action> _disabled;
private UniTask.Awaiter _uniTaskAwaiter;
private Action _uniTaskContinuation;
public static AwaitContext Create(MonoBehaviour parent, List<Action> disabled, UniTask uniTask)
{
if (!Pool.TryPop(out AwaitContext context)) {
context = new AwaitContext();
}
context.Init(parent, disabled, uniTask);
return context;
}
private AwaitContext()
{
_continuation = OnCompleted;
}
private void OnCompleted()
{
if (_parent != null && _parent.enabled) {
_uniTaskContinuation();
} else {
_disabled.Add(_uniTaskContinuation);
}
Deinit();
}
private void Init(MonoBehaviour parent, List<Action> disabled, UniTask uniTask)
{
_parent = parent;
_disabled = disabled;
_uniTaskAwaiter = uniTask.GetAwaiter();
}
private void Deinit()
{
_parent = null;
_disabled = null;
_uniTaskAwaiter = default;
_uniTaskContinuation = null;
Pool.Push(this);
}
public Awaiter GetAwaiter()
{
return new Awaiter(this);
}
public readonly struct Awaiter : ICriticalNotifyCompletion
{
private readonly AwaitContext _context;
public Awaiter(AwaitContext context)
{
_context = context;
}
public bool IsCompleted => _context._uniTaskAwaiter.IsCompleted;
public void GetResult()
{
_context._uniTaskAwaiter.GetResult();
}
public void OnCompleted(Action continuation)
{
_context._uniTaskContinuation = continuation;
_context._uniTaskAwaiter.OnCompleted(_context._continuation);
}
public void UnsafeOnCompleted(Action continuation)
{
_context._uniTaskContinuation = continuation;
_context._uniTaskAwaiter.OnCompleted(_context._continuation);
}
}
}
}
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
public class UniTaskOnDisableWrapperExampleUsage : MonoBehaviour
{
private UniTaskOnDisableWrapper _uniTaskOnDisable;
private void Awake()
{
_uniTaskOnDisable = new UniTaskOnDisableWrapper(this);
}
private void Start()
{
_ = WorkUpdate(destroyCancellationToken);
}
private void OnEnable()
{
_uniTaskOnDisable.OnEnable();
}
private async UniTaskVoid WorkUpdate(CancellationToken cancel)
{
while (true) {
await _uniTaskOnDisable.Wrap(UniTask.WaitForFixedUpdate(cancel));
// Do some work
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment