question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How do you pass parameter types to a "UniTask.Action"?

See original GitHub issue

How do you pass parameter types to a ā€œUniTask.Actionā€?

  void OnEnable() {
    _loadSceneEvent.Register(UniTask.Action(OnLoadScene)); // this doesnt work, need to use regular "async void" callback or delegate with no parameters
  }

  async UniTaskVoid OnLoadScene(GameScene gameScene) {
    await LoadScene(gameScene);
  }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
danielmahoncommented, Mar 3, 2022

OK, I was completely overthinking this, I just need to store a reference to the action returned by my UniTaskHelper.Action for proper unsubscribing. I should really go to bed early šŸ˜¬ I do think adding the generic overrides into UniTask would be useful, but thatā€™s up to you. Thank you for your time.


  public static class UniTaskHelper {
    public static Action<T> Action<T>(Func<T, UniTaskVoid> asyncAction) {
      return (t1) => asyncAction(t1).Forget();
    }
  }

    Action<GameScene> _fooAction;

    void Awake() {
      _fooAction = UniTaskHelper.Action<GameScene>(OnSceneReady);
    }

    void OnEnable() {
      _sceneReadyEvent += _fooAction;
    }

    void OnDisable() {
      _sceneReadyEvent -= _fooAction;
    }
    
    async UniTaskVoid OnSceneReady(GameScene scene) {
      _logger.Info("Scene ready: " + scene);
      // await something
      await UniTask.Yield();
    }

0reactions
danielmahoncommented, Mar 3, 2022

Sorry, yea I just realized I put the Register method in the example instead of the event

  void OnEnable() {
    _loadSceneEvent.OnChanged += OnLoadScene;
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cysharp/UniTask: Provides an efficient allocation ...
Tasks; // You can return type as struct UniTask<T>(or UniTask), ... They return value tuples so you can deconstruct each result and pass...
Read more >
How to call async-wait method in any function by using ...
I am using UniTask in Unity 2020.3. And I want to call an async method which returns a UniTask in Unity Event Function....
Read more >
UniTask, a new async/await library for Unity. - Yoshifumi Kawai
C#/Unity asynchronous processing for async/await Ā· It combines with MonoBehaviour during startup Ā· Transmits the return value Ā· TransmitsĀ ...
Read more >
6ē‚¹/UniTask
You can pass CancellationToken to parameter by standard CancellationTokenSource . var cts = new CancellationTokenSource(); cancelButtonĀ ...
Read more >
UniTaskCompletionSourceCore<TResult> Struct | UniTask
Type Parameters. TResult ... Schedules the continuation action for this operation. ... Opaque value that was provided to the UniTask's constructor.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found