Advertisement
Guest User

Untitled

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