Search Unity

Help Wanted System.Threading.Tasks.Task callback not changing style on VisualElement

Discussion in 'UI Toolkit' started by Nomadjackalope, Sep 24, 2021.

  1. Nomadjackalope

    Nomadjackalope

    Joined:
    Jul 12, 2016
    Posts:
    16
    I show a waiting animation, log the user into their account, then hiding the waiting animation element. But it's not applying the style when the callback is coming from a Task. I am seeing "called" show up in the log though.

    I would rather not queue up callbacks and send them during Update() on the FirebaseManager but I think that would work.

    I tried MarkDirtyRepaint() but that didn't work.

    What do I need to do to get style to apply in a callback from a Task?

    Code (CSharp):
    1. public class FirebaseManager : MonoBehaviour
    2. {
    3.     ...
    4.     public void TestLogin(string email, string password, Action<bool> callback) {
    5.         System.Threading.Tasks.Task.Run(() => FakeSignIn()).ContinueWith(task => {
    6.             callback(false);
    7.         });
    8.     }
    9.  
    10.     void FakeSignIn() {
    11.         Debug.Log("fake signed in");
    12.     }
    13. }
    Code (CSharp):
    1. public class LoginPanel : VisualElement
    2. {
    3.     ...
    4.     void OnSubmitClick() {
    5.         ShowWaitState();
    6.  
    7.         FirebaseManager.instance.TestLogin(email, password, (x) => HideWaitState());
    8.     }
    9.  
    10.     void HideWaitState() {
    11.         Debug.Log("called");
    12.         this.Q("submit-button").SetEnabled(true);
    13.         this.Q("rotate-circle-panel").style.display = DisplayStyle.None;
    14.     }
    15. }
     
  2. hugobd

    hugobd

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    123
    Hi, @Nomadjackalope,

    I want to make sure the issue comes from using a task. Could you try to call HideWaitState() without using a task ? For instance, could you test if calling HideWaitState() from a MouseDown event works?
     
  3. Nomadjackalope

    Nomadjackalope

    Joined:
    Jul 12, 2016
    Posts:
    16
    It does work from a mousedownevent.

    I am on 2021.0b10 and will try in the most up-to-date version of beta and alpha later today just to check that.
     
  4. Nomadjackalope

    Nomadjackalope

    Joined:
    Jul 12, 2016
    Posts:
    16
    I've made a test project for this as it doesn't work on 2022.1.0a9 or 2021.2.0b13
    Open test scene and run it. Click on the green object. It should turn red. In the heirarchy select ThreadingTest and make sure use task is enabled. Then click on the green object again. It should turn yellow and two seconds later turn red.
     

    Attached Files:

  5. hugobd

    hugobd

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    123
    Thanks for bringing this up. This indeed looks like a bug to me. Could you open a bug through the Unity bug reporter (Help/Report a bug...) ?
     
  6. Nomadjackalope

    Nomadjackalope

    Joined:
    Jul 12, 2016
    Posts:
    16
    Sure thing! Case 1368791
     
    hugobd likes this.
unityunity