unity3d简单的震屏效果
  1. using UnityEngine;
  2. using System.Collections;
  3. public class SC_shakeCamera : MonoBehaviour {

  4. private float shakeTime = 0.0f;
  5. private float fps= 20.0f;
  6. private float frameTime =0.0f;
  7. private float shakeDelta =0.005f;
  8. public Camera cam ;
  9. public static bool isshakeCamera =false;
  10. // Use this for initialization
  11. void Start ()
  12. {
  13. shakeTime = 1.0f;
  14. fps= 20.0f;
  15. frameTime =0.03f;
  16. shakeDelta =0.005f;

  17. }

  18. // Update is called once per frame
  19. void Update ()
  20. {
  21. if (isshakeCamera)
  22. {
  23. if(shakeTime > 0)
  24. {
  25. shakeTime -= Time.deltaTime;
  26. if(shakeTime <= 0)
  27. {
  28. cam.rect = new Rect(0.0f,0.0f,1.0f,1.0f);
  29. isshakeCamera =false;
  30. shakeTime = 1.0f;
  31. fps= 20.0f;
  32. frameTime =0.03f;
  33. shakeDelta =0.005f;
  34. }
  35. else
  36. {
  37. frameTime += Time.deltaTime;

  38. if(frameTime > 1.0 / fps)
  39. {
  40. frameTime = 0;
  41. cam.rect = new Rect(shakeDelta * ( -1.0f + 2.0f * Random.value),shakeDelta * ( -1.0f + 2.0f * Random.value), 1.0f, 1.0f);

  42. }
  43. }
  44. }
  45. }

  46. }

  47. public static void shakeCamera()
  48. {
  49. isshakeCamera =true;
  50. }
  51. }
复制代码