Sample.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAnimation
  3. {
  4. [TaskCategory("Unity/Animation")]
  5. [TaskDescription("Samples animations at the current state. Returns Success.")]
  6. public class Sample : Action
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. // cache the animation component
  11. private Animation animation;
  12. private GameObject prevGameObject;
  13. public override void OnStart()
  14. {
  15. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  16. if (currentGameObject != prevGameObject) {
  17. animation = currentGameObject.GetComponent<Animation>();
  18. prevGameObject = currentGameObject;
  19. }
  20. }
  21. public override TaskStatus OnUpdate()
  22. {
  23. if (animation == null) {
  24. Debug.LogWarning("Animation is null");
  25. return TaskStatus.Failure;
  26. }
  27. animation.Sample();
  28. return TaskStatus.Success;
  29. }
  30. public override void OnReset()
  31. {
  32. targetGameObject = null;
  33. }
  34. }
  35. }