GetPropertyValue.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System;
  3. using System.Reflection;
  4. namespace BehaviorDesigner.Runtime.Tasks
  5. {
  6. [TaskDescription("Gets the value from the property specified. Returns success if the property was retrieved.")]
  7. [TaskCategory("Reflection")]
  8. [TaskIcon("{SkinColor}ReflectionIcon.png")]
  9. public class GetPropertyValue : Action
  10. {
  11. [Tooltip("The GameObject to get the property of")]
  12. public SharedGameObject targetGameObject;
  13. [Tooltip("The component to get the property of")]
  14. public SharedString componentName;
  15. [Tooltip("The name of the property")]
  16. public SharedString propertyName;
  17. [Tooltip("The value of the property")]
  18. [RequiredField]
  19. public SharedVariable propertyValue;
  20. public override TaskStatus OnUpdate()
  21. {
  22. if (propertyValue == null) {
  23. Debug.LogWarning("Unable to get property - property value is null");
  24. return TaskStatus.Failure;
  25. }
  26. var type = TaskUtility.GetTypeWithinAssembly(componentName.Value);
  27. if (type == null) {
  28. Debug.LogWarning("Unable to get property - type is null");
  29. return TaskStatus.Failure;
  30. }
  31. var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type);
  32. if (component == null) {
  33. Debug.LogWarning("Unable to get the property with component " + componentName.Value);
  34. return TaskStatus.Failure;
  35. }
  36. // If you are receiving a compiler error on the Windows Store platform see this topic:
  37. // https://www.opsive.com/support/documentation/behavior-designer/installation/
  38. var property = component.GetType().GetProperty(propertyName.Value);
  39. propertyValue.SetValue(property.GetValue(component, null));
  40. return TaskStatus.Success;
  41. }
  42. public override void OnReset()
  43. {
  44. targetGameObject = null;
  45. componentName = null;
  46. propertyName = null;
  47. propertyValue = null;
  48. }
  49. }
  50. }