TimerExample.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /****************************************************************************
  2. * Copyright (c) 2021.3 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. using UnityEngine;
  26. using UnityEngine.UI;
  27. namespace QFramework.TimeExtend
  28. {
  29. public class TimerExample : MonoBehaviour
  30. {
  31. Image image;
  32. Text text;
  33. Text text2;
  34. Text text3;
  35. Timer timer;
  36. void Start()
  37. {
  38. image = GetComponent<Image>();
  39. image.type = Image.Type.Filled;
  40. image.fillAmount = 0;
  41. text = GetComponentInChildren<Text>();
  42. text2 = GameObject.Find("Text2").GetComponent<Text>();
  43. text3 = GameObject.Find("Text3").GetComponent<Text>();
  44. }
  45. private void OnGUI()
  46. {
  47. if (GUILayout.Button("开启定时"))
  48. {
  49. text3.text = "";
  50. if (!Timer.Exist(timer))
  51. {
  52. timer = Timer.AddTimer(5, "12138").OnUpdated((v) =>
  53. {
  54. image.fillAmount = v;
  55. text.text = (v * timer.Duration).ToString();
  56. }).OnCompleted(
  57. () => { text3.text = "计时完成!"; }
  58. );
  59. text2.text = "当前设定时间为:" + timer.Duration;
  60. }
  61. }
  62. if (GUILayout.Button("定时2秒"))
  63. {
  64. if (Timer.Exist("12138"))
  65. {
  66. Timer.GetTimer("12138").SetTime(2); //当定时器走了大于2秒时按下,会提示错误,并瞬间完成事件
  67. text2.text = "当前设定时间为:" + timer.Duration; //当定时器走了小于2秒时按下,会加速完成
  68. }
  69. }
  70. if (GUILayout.Button("定时-2秒")) //当定时器走了大于2秒时按下,会提示参数必须为正值,并瞬间完成事件
  71. {
  72. //当定时器走了小于2秒时按下,会提示参数必须为正值,会加速完成
  73. if (Timer.Exist("12138"))
  74. {
  75. Timer.GetTimer("12138").SetTime(-2);
  76. text2.text = "当前设定时间为:" + timer.Duration;
  77. }
  78. }
  79. if (GUILayout.Button("定时10秒")) //当定时器走了小于2秒时按下,会提示参数必须为正值,会加速完成
  80. {
  81. if (null != timer)
  82. {
  83. timer.SetTime(10);
  84. text2.text = "当前设定时间为:" + timer.Duration;
  85. }
  86. }
  87. }
  88. }
  89. }