StopBehaviorTree.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks
  3. {
  4. [TaskDescription("Pause or disable a behavior tree and return success after it has been stopped.")]
  5. [TaskIcon("{SkinColor}StopBehaviorTreeIcon.png")]
  6. public class StopBehaviorTree : Action
  7. {
  8. [Tooltip("The GameObject of the behavior tree that should be stopped. If null use the current behavior")]
  9. public SharedGameObject behaviorGameObject;
  10. [Tooltip("The group of the behavior tree that should be stopped")]
  11. public SharedInt group;
  12. [Tooltip("Should the behavior be paused or completely disabled")]
  13. public SharedBool pauseBehavior = false;
  14. private Behavior behavior;
  15. public override void OnStart()
  16. {
  17. var behaviorTrees = GetDefaultGameObject(behaviorGameObject.Value).GetComponents<Behavior>();
  18. if (behaviorTrees.Length == 1) {
  19. behavior = behaviorTrees[0];
  20. } else if (behaviorTrees.Length > 1) {
  21. for (int i = 0; i < behaviorTrees.Length; ++i) {
  22. if (behaviorTrees[i].Group == group.Value) {
  23. behavior = behaviorTrees[i];
  24. break;
  25. }
  26. }
  27. // If the group can't be found then use the first behavior tree
  28. if (behavior == null) {
  29. behavior = behaviorTrees[0];
  30. }
  31. }
  32. }
  33. public override TaskStatus OnUpdate()
  34. {
  35. if (behavior == null) {
  36. return TaskStatus.Failure;
  37. }
  38. // Start the behavior and return success.
  39. behavior.DisableBehavior(pauseBehavior.Value);
  40. return TaskStatus.Success;
  41. }
  42. public override void OnReset()
  43. {
  44. // Reset the properties back to their original values
  45. behaviorGameObject = null;
  46. group = 0;
  47. pauseBehavior = false;
  48. }
  49. }
  50. }