You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.CinematicEffects;
  5. public class ScreenEffects : MonoBehaviour
  6. {
  7. public DepthOfField depthOfField;
  8. public float strength = 0f;
  9. public CanvasGroup flashCanvas;
  10. private float originalNearRadius;
  11. private float originalFarRadius;
  12. private int flashID;
  13. void Start ()
  14. {
  15. depthOfField = GetComponent<DepthOfField>();
  16. if (depthOfField == null)
  17. depthOfField = gameObject.AddComponent<DepthOfField>();
  18. originalNearRadius = depthOfField.focus.nearBlurRadius;
  19. originalFarRadius = depthOfField.focus.farBlurRadius;
  20. NotificationServer.register("show GameUI", showGame);
  21. NotificationServer.register("hide GameUI", hideGame);
  22. NotificationServer.register("flash damage", flashDamage);
  23. }
  24. public void showGame()
  25. {
  26. LeanTween.cancel(gameObject, false);
  27. float startNear = depthOfField.focus.nearBlurRadius;
  28. float startFar = depthOfField.focus.farBlurRadius;
  29. DepthOfField.FocusSettings focus = depthOfField.focus;
  30. LeanTween.value(gameObject, 1f, 1f*strength, 0.5f).setEaseInOutQuad().setOnUpdate((float val)=>{
  31. focus.nearBlurRadius = startNear * val;
  32. focus.farBlurRadius = startFar * val;
  33. }).setOnComplete(()=>{
  34. if (strength == 0f)
  35. depthOfField.enabled = false;
  36. }).setIgnoreTimeScale(true);
  37. }
  38. public void hideGame()
  39. {
  40. LeanTween.cancel(gameObject, false);
  41. depthOfField.enabled = true;
  42. float startNear = depthOfField.focus.nearBlurRadius;
  43. float startFar = depthOfField.focus.farBlurRadius;
  44. DepthOfField.FocusSettings focus = depthOfField.focus;
  45. LeanTween.value(gameObject, 1f*strength, 1f, 0.5f).setEaseInOutQuad().setOnUpdate((float val)=>{
  46. focus.nearBlurRadius = startNear + (originalNearRadius-startNear) * val;
  47. focus.farBlurRadius = startFar + (originalFarRadius-startFar) * val;
  48. }).setIgnoreTimeScale(true);
  49. }
  50. public void flashDamage()
  51. {
  52. LeanTween.cancel(flashID);
  53. flashID = LeanTween.alphaCanvas(flashCanvas, 1f, 0.2f).setEaseInOutCubic().setOnComplete(()=>{
  54. LeanTween.alphaCanvas(flashCanvas, 0f, 0.6f).setEaseInOutCubic();
  55. }).uniqueId;
  56. }
  57. }