설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomEvent.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Unity.VisualScripting
  5. {
  6. /// <summary>
  7. /// A special named event with any amount of parameters called manually with the 'Trigger Custom Event' unit.
  8. /// </summary>
  9. [UnitCategory("Events")]
  10. [UnitOrder(0)]
  11. public sealed class CustomEvent : GameObjectEventUnit<CustomEventArgs>
  12. {
  13. public override Type MessageListenerType => null;
  14. protected override string hookName => EventHooks.Custom;
  15. [SerializeAs(nameof(argumentCount))]
  16. private int _argumentCount;
  17. [DoNotSerialize]
  18. [Inspectable, UnitHeaderInspectable("Arguments")]
  19. public int argumentCount
  20. {
  21. get => _argumentCount;
  22. set => _argumentCount = Mathf.Clamp(value, 0, 10);
  23. }
  24. /// <summary>
  25. /// The name of the event.
  26. /// </summary>
  27. [DoNotSerialize]
  28. [PortLabelHidden]
  29. public ValueInput name { get; private set; }
  30. [DoNotSerialize]
  31. public List<ValueOutput> argumentPorts { get; } = new List<ValueOutput>();
  32. protected override void Definition()
  33. {
  34. base.Definition();
  35. name = ValueInput(nameof(name), string.Empty);
  36. argumentPorts.Clear();
  37. for (var i = 0; i < argumentCount; i++)
  38. {
  39. argumentPorts.Add(ValueOutput<object>("argument_" + i));
  40. }
  41. }
  42. protected override bool ShouldTrigger(Flow flow, CustomEventArgs args)
  43. {
  44. return CompareNames(flow, name, args.name);
  45. }
  46. protected override void AssignArguments(Flow flow, CustomEventArgs args)
  47. {
  48. for (var i = 0; i < argumentCount; i++)
  49. {
  50. flow.SetValue(argumentPorts[i], args.arguments[i]);
  51. }
  52. }
  53. public static void Trigger(GameObject target, string name, params object[] args)
  54. {
  55. EventBus.Trigger(EventHooks.Custom, target, new CustomEventArgs(name, args));
  56. }
  57. }
  58. }