ExampleOccurrences.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEngine;
  2. namespace Chronos.Example
  3. {
  4. // Example for Chronos with Occurrences.
  5. // Will change color at scheduled time. This code is rewindable.
  6. // More information:
  7. // http://ludiq.io/chronos/documentation#Occurrence
  8. [RequireComponent(typeof(Renderer))]
  9. public class ExampleOccurrences : ExampleBaseBehaviour
  10. {
  11. // Subclass the occurrence class (see documentation)
  12. private class ChangeColorOccurrence : Occurrence
  13. {
  14. private Material material;
  15. private Color newColor;
  16. private Color previousColor;
  17. public ChangeColorOccurrence(Material material, Color newColor)
  18. {
  19. this.material = material;
  20. this.newColor = newColor;
  21. }
  22. // The action when time is going forward
  23. public override void Forward()
  24. {
  25. previousColor = material.color;
  26. material.color = newColor;
  27. }
  28. // The action when time is going backward
  29. public override void Backward()
  30. {
  31. material.color = previousColor;
  32. }
  33. }
  34. private void Start()
  35. {
  36. // Get the renderer's material
  37. Material material = GetComponent<Renderer>().material;
  38. // Change the color to yellow now
  39. time.Do(true, new ChangeColorOccurrence(material, Color.yellow));
  40. // Change the color to blue in 5 seconds
  41. time.Plan(5, true, new ChangeColorOccurrence(material, Color.blue));
  42. // Change the color to green in 7 seconds
  43. time.Plan(7, true, new ChangeColorOccurrence(material, Color.green));
  44. // Change the color to red in 10 seconds
  45. time.Plan(10, true, new ChangeColorOccurrence(material, Color.red));
  46. // Plan an occurrence, but cancel it.
  47. Occurrence changeToMagenta = time.Plan(3, true, new ChangeColorOccurrence(material, Color.magenta));
  48. time.Cancel(changeToMagenta);
  49. }
  50. }
  51. }