Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1.    
  2.     //Essa é a função origem, Um controllador que deve ser trancado e executar uma árvore de ações que vão ramificando...
  3.     //Minha intenção era pra ele esperar que todos os nodos fossem completos pra seguir a execução
  4.     //Essa função é chamada por um botão na UI, usando a classe Button da unity mesmo, cadastrada no OnClick pelo Editor
  5.      public async void PlayLevel()
  6.     {
  7.         if (!@lock)
  8.         {
  9.             @lock = true;
  10.  
  11.             SpriteSceneController sceneController = transform.GetChild(0).GetComponent<SpriteSceneController>();  
  12.  
  13.  
  14.             // Pra exemplificar, esse seria um uso que gostaria de fazer, embaixo segue a função CanvasController.Singleton.FadeOut()
  15.             Task.WaitAll(CanvasController.Singleton.FadeOut(), cameraController.SetCameraContext(Context.INGAME, sceneController.LinkedVase));
  16.             await CanvasController.Singleton.SwitchContext(Context.INGAME);
  17.  
  18.             sceneController.StartLevel();
  19.             InputController.Singleton.Initialize(cameraController.MainCamera, sceneController.GetComponent<VaseRendererController>().GetTranslatedUVPosition);
  20.              
  21.             @lock = false;
  22.         }
  23.     }
  24.  
  25.  
  26.  
  27.     public async Task FadeOut()
  28.     {
  29.         switch (context)
  30.         {
  31.             case Context.INGAME:
  32.             case Context.MAIN_MENU:
  33.             case Context.LVL_SELECTION:
  34.                 Image[] fadeImages;
  35.                 SpriteAnimator[] animators;
  36.                 SplitFadeAndAnimation(GetComponentsInChildren<Image>(), out fadeImages, out animators);
  37.  
  38.  
  39.                 //Nesse contexto eu gostaria de disparar duas corrotinas "AsyncWaitFade" e "AsyncWaitAnimationOnAnimators"
  40.                 //Vou seguir só o caminho da AsyncWaitFade que é onde tranca, e é, em princípio, a mesma ideia da outra corrotina
  41.                 Task.WaitAll(AsyncWaitFade(fadeImages, false), AsyncWaitAnimationOnAnimators(animators, StringTable.FadeOut));
  42.  
  43.                 break;
  44.             case Context.FOCUS:
  45.                 await GetComponentInChildren<ScrollController>().SetActiveRoutine(false);
  46.                 //yield return GetComponentInChildren<ScrollController>().Close();
  47.                 break;
  48.             case Context.OFF:
  49.             default:
  50.                 await Task.CompletedTask;
  51.                 break;
  52.         }
  53.  
  54.         SetContext(Context.OFF);
  55.  
  56.     }
  57.  
  58.     async Task AsyncWaitFade(Image[] images, bool fadeIn)
  59.     {
  60.         //Aqui que eu acho que estou errando, o que queria era "converter" a corrotina e uma task, pra poder usar o Task.WaitAll
  61.         await FadeImagesRoutine(images, fadeIn);
  62.     }
  63.    
  64.  
  65.  
  66.     // E essa aqui é a corrotina, que é só um fade nas imagens fornecidas.
  67.     Enumerator FadeImagesRoutine(Image[] images, bool fadeIn)
  68.     {
  69.         if (images.Length > 0)
  70.         {
  71.             WaitForEndOfFrame wait = new WaitForEndOfFrame();  
  72.             AnimationCurve curve = AnimationCurve.EaseInOut(1f, 1f, 0f, 0f);
  73.             float deltaScaler = 1f / 0.3f;
  74.             float timer = 0;
  75.             float eval;
  76.             Color color;
  77.             do
  78.             {
  79.                 timer += Time.deltaTime * deltaScaler;
  80.                 eval = fadeIn ? curve.Evaluate(timer) : 1f - curve.Evaluate(timer);
  81.                 for (int i = 0; i < images.Length; i++)
  82.                 {
  83.                     color = images[i].color;
  84.                     color.a = eval;
  85.                     images[i].color = color;
  86.                 }
  87.                 yield return new WaitForEndOfFrame();
  88.             } while (timer < 1f);
  89.         }
  90.         else
  91.         {
  92.             yield return null;
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement