InvokeMethod.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace BehaviorDesigner.Runtime.Tasks
  5. {
  6. [TaskDescription("Invokes the specified method with the specified parameters. Can optionally store the return value. Returns success if the method was invoked.")]
  7. [TaskCategory("Reflection")]
  8. [TaskIcon("{SkinColor}ReflectionIcon.png")]
  9. public class InvokeMethod : Action
  10. {
  11. [Tooltip("The GameObject to invoke the method on")]
  12. public SharedGameObject targetGameObject;
  13. [Tooltip("The component to invoke the method on")]
  14. public SharedString componentName;
  15. [Tooltip("The name of the method")]
  16. public SharedString methodName;
  17. [Tooltip("The first parameter of the method")]
  18. public SharedVariable parameter1;
  19. [Tooltip("The second parameter of the method")]
  20. public SharedVariable parameter2;
  21. [Tooltip("The third parameter of the method")]
  22. public SharedVariable parameter3;
  23. [Tooltip("The fourth parameter of the method")]
  24. public SharedVariable parameter4;
  25. [Tooltip("Store the result of the invoke call")]
  26. public SharedVariable storeResult;
  27. public override TaskStatus OnUpdate()
  28. {
  29. var type = TaskUtility.GetTypeWithinAssembly(componentName.Value);
  30. if (type == null) {
  31. Debug.LogWarning("Unable to invoke - type is null");
  32. return TaskStatus.Failure;
  33. }
  34. var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type);
  35. if (component == null) {
  36. Debug.LogWarning("Unable to invoke method with component " + componentName.Value);
  37. return TaskStatus.Failure;
  38. }
  39. var parameterList = new List<object>();
  40. var parameterTypeList = new List<Type>();
  41. SharedVariable sharedVariable = null;
  42. for (int i = 0; i < 4; ++i) {
  43. var parameterField = GetType().GetField("parameter" + (i + 1));
  44. if ((sharedVariable = parameterField.GetValue(this) as SharedVariable) != null) {
  45. parameterList.Add(sharedVariable.GetValue());
  46. parameterTypeList.Add(sharedVariable.GetType().GetProperty("Value").PropertyType);
  47. } else {
  48. break;
  49. }
  50. }
  51. // If you are receiving a compiler error on the Windows Store platform see this topic:
  52. // https://www.opsive.com/support/documentation/behavior-designer/installation/
  53. var methodInfo = component.GetType().GetMethod(methodName.Value, parameterTypeList.ToArray());
  54. if (methodInfo == null) {
  55. Debug.LogWarning("Unable to invoke method " + methodName.Value + " on component " + componentName.Value);
  56. return TaskStatus.Failure;
  57. }
  58. var result = methodInfo.Invoke(component, parameterList.ToArray());
  59. if (storeResult != null) {
  60. storeResult.SetValue(result);
  61. }
  62. return TaskStatus.Success;
  63. }
  64. public override void OnReset()
  65. {
  66. targetGameObject = null;
  67. componentName = null;
  68. methodName = null;
  69. parameter1 = null;
  70. parameter2 = null;
  71. parameter3 = null;
  72. parameter4 = null;
  73. storeResult = null;
  74. }
  75. }
  76. }