RestartBehaviorTree.cs 1.9 KB

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