ScreenBlackController.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using UnityEngine;
  3. using FairyGUI;
  4. using System;
  5. namespace GFGGame
  6. {
  7. public class ScreenBlackController : SingletonBase<ScreenBlackController>
  8. {
  9. private const float fadeDuration = 0.2f;
  10. private bool isShowing;
  11. private GGraph _gGraph;
  12. private GTweener _gTween;
  13. private float _duration;
  14. public void ShowBlack(float durationMilliSecs, GComponent parent = null, int index = -1)
  15. {
  16. if(isShowing)
  17. {
  18. return;
  19. }
  20. _duration = durationMilliSecs/1000f - fadeDuration * 2;
  21. _duration = Mathf.Max(fadeDuration * 2, _duration);
  22. isShowing = true;
  23. if(_gGraph == null)
  24. {
  25. _gGraph = new GGraph();
  26. _gGraph.DrawRect(GRoot.inst.width, GRoot.inst.height, 0, Color.black, Color.black);
  27. _gGraph.AddRelation(GRoot.inst, RelationType.Size);
  28. }
  29. if (parent == null)
  30. {
  31. ViewManager.AddChildToTopLayer(_gGraph);
  32. }
  33. else
  34. {
  35. if(index < 0)
  36. {
  37. parent.AddChild(_gGraph);
  38. }
  39. else
  40. {
  41. parent.AddChildAt(_gGraph, index);
  42. }
  43. }
  44. _gGraph.x = -(_gGraph.width - GRoot.inst.width) / 2;
  45. _gGraph.y = -(_gGraph.height - GRoot.inst.height) / 2;
  46. _gGraph.alpha = 0;
  47. _gTween = _gGraph.TweenFade(1, fadeDuration);
  48. _gTween.OnComplete(OnFadeInComplete);
  49. //_gTween.OnUpdate(() => { Debug.LogFormat("alpha{0}", _gGraph.alpha); });
  50. }
  51. public void HideBlack()
  52. {
  53. isShowing = false;
  54. Timers.inst.Remove(OnBlackComplete);
  55. if(_gTween != null)
  56. {
  57. _gTween.Kill();
  58. }
  59. if(_gGraph != null)
  60. {
  61. _gGraph.RemoveFromParent();
  62. }
  63. }
  64. private void OnFadeInComplete()
  65. {
  66. Timers.inst.Add(_duration, 1, OnBlackComplete);
  67. }
  68. private void OnBlackComplete(object param)
  69. {
  70. _gTween = _gGraph.TweenFade(0, fadeDuration);
  71. _gTween.OnComplete(OnFadeOutComplete);
  72. }
  73. private void OnFadeOutComplete()
  74. {
  75. HideBlack();
  76. }
  77. }
  78. }