ComparePropertyValue.cs 2.3 KB

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