SendEvent.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks
  3. {
  4. [TaskDescription("Sends an event to the behavior tree, returns success after sending the event.")]
  5. [HelpURL("https://www.opsive.com/support/documentation/behavior-designer/events/")]
  6. [TaskIcon("{SkinColor}SendEventIcon.png")]
  7. public class SendEvent : Action
  8. {
  9. [Tooltip("The GameObject of the behavior tree that should have the event sent to it. If null use the current behavior")]
  10. public SharedGameObject targetGameObject;
  11. [Tooltip("The event to send")]
  12. public SharedString eventName;
  13. [Tooltip("The group of the behavior tree that the event should be sent to")]
  14. public SharedInt group;
  15. [Tooltip("Optionally specify a first argument to send")]
  16. [SharedRequired]
  17. public SharedVariable argument1;
  18. [Tooltip("Optionally specify a second argument to send")]
  19. [SharedRequired]
  20. public SharedVariable argument2;
  21. [Tooltip("Optionally specify a third argument to send")]
  22. [SharedRequired]
  23. public SharedVariable argument3;
  24. private BehaviorTree behaviorTree;
  25. public override void OnStart()
  26. {
  27. var behaviorTrees = GetDefaultGameObject(targetGameObject.Value).GetComponents<BehaviorTree>();
  28. if (behaviorTrees.Length == 1) {
  29. behaviorTree = behaviorTrees[0];
  30. } else if (behaviorTrees.Length > 1) {
  31. for (int i = 0; i < behaviorTrees.Length; ++i) {
  32. if (behaviorTrees[i].Group == group.Value) {
  33. behaviorTree = behaviorTrees[i];
  34. break;
  35. }
  36. }
  37. // If the group can't be found then use the first behavior tree
  38. if (behaviorTree == null) {
  39. behaviorTree = behaviorTrees[0];
  40. }
  41. }
  42. }
  43. public override TaskStatus OnUpdate()
  44. {
  45. // Send the event and return success
  46. if (argument1 == null || argument1.IsNone) {
  47. behaviorTree.SendEvent(eventName.Value);
  48. } else {
  49. if (argument2 == null || argument2.IsNone) {
  50. behaviorTree.SendEvent<object>(eventName.Value, argument1.GetValue());
  51. } else {
  52. if (argument3 == null || argument3.IsNone) {
  53. behaviorTree.SendEvent<object, object>(eventName.Value, argument1.GetValue(), argument2.GetValue());
  54. } else {
  55. behaviorTree.SendEvent<object, object, object>(eventName.Value, argument1.GetValue(), argument2.GetValue(), argument3.GetValue());
  56. }
  57. }
  58. }
  59. return TaskStatus.Success;
  60. }
  61. public override void OnReset()
  62. {
  63. // Reset the properties back to their original values
  64. targetGameObject = null;
  65. eventName = "";
  66. }
  67. }
  68. }