From 97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 27 Jan 2021 16:15:06 +0800 Subject: +behaviour designer --- .../Conditionals/Reflection/CompareFieldValue.cs | 61 ++++++++++++++++++++++ .../Reflection/CompareFieldValue.cs.meta | 8 +++ .../Reflection/ComparePropertyValue.cs | 61 ++++++++++++++++++++++ .../Reflection/ComparePropertyValue.cs.meta | 8 +++ 4 files changed, 138 insertions(+) create mode 100644 Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs create mode 100644 Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs.meta create mode 100644 Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs create mode 100644 Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs.meta (limited to 'Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection') diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs new file mode 100644 index 00000000..d66bf706 --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs @@ -0,0 +1,61 @@ +using UnityEngine; +using System; +using System.Reflection; + +namespace BehaviorDesigner.Runtime.Tasks +{ + [TaskDescription("Compares the field value to the value specified. Returns success if the values are the same.")] + [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=151")] + [TaskCategory("Reflection")] + [TaskIcon("{SkinColor}ReflectionIcon.png")] + public class CompareFieldValue : Conditional + { + [Tooltip("The GameObject to compare the field on")] + public SharedGameObject targetGameObject; + [Tooltip("The component to compare the field on")] + public SharedString componentName; + [Tooltip("The name of the field")] + public SharedString fieldName; + [Tooltip("The value to compare to")] + public SharedVariable compareValue; + + public override TaskStatus OnUpdate() + { + if (compareValue == null) { + Debug.LogWarning("Unable to compare field - compare value is null"); + return TaskStatus.Failure; + } + + var type = TaskUtility.GetTypeWithinAssembly(componentName.Value); + if (type == null) { + Debug.LogWarning("Unable to compare field - type is null"); + return TaskStatus.Failure; + } + + var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type); + if (component == null) { + Debug.LogWarning("Unable to compare the field with component " + componentName.Value); + return TaskStatus.Failure; + } + + // If you are receiving a compiler error on the Windows Store platform see this topic: + // http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=46 + var field = component.GetType().GetField(fieldName.Value); + var fieldValue = field.GetValue(component); + + if (fieldValue == null && compareValue.GetValue() == null) { + return TaskStatus.Success; + } + + return fieldValue.Equals(compareValue.GetValue()) ? TaskStatus.Success : TaskStatus.Failure; + } + + public override void OnReset() + { + targetGameObject = null; + componentName = null; + fieldName = null; + compareValue = null; + } + } +} \ No newline at end of file diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs.meta b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs.meta new file mode 100644 index 00000000..42f0e6bb --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/CompareFieldValue.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cc6496fb6757b684c8cc0c4ac8929319 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs new file mode 100644 index 00000000..4f7fbdf1 --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs @@ -0,0 +1,61 @@ +using UnityEngine; +using System; +using System.Reflection; + +namespace BehaviorDesigner.Runtime.Tasks +{ + [TaskDescription("Compares the property value to the value specified. Returns success if the values are the same.")] + [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=152")] + [TaskCategory("Reflection")] + [TaskIcon("{SkinColor}ReflectionIcon.png")] + public class ComparePropertyValue : Conditional + { + [Tooltip("The GameObject to compare the property of")] + public SharedGameObject targetGameObject; + [Tooltip("The component to compare the property of")] + public SharedString componentName; + [Tooltip("The name of the property")] + public SharedString propertyName; + [Tooltip("The value to compare to")] + public SharedVariable compareValue; + + public override TaskStatus OnUpdate() + { + if (compareValue == null) { + Debug.LogWarning("Unable to compare field - compare value is null"); + return TaskStatus.Failure; + } + + var type = TaskUtility.GetTypeWithinAssembly(componentName.Value); + if (type == null) { + Debug.LogWarning("Unable to compare property - type is null"); + return TaskStatus.Failure; + } + + var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type); + if (component == null) { + Debug.LogWarning("Unable to compare the property with component " + componentName.Value); + return TaskStatus.Failure; + } + + // If you are receiving a compiler error on the Windows Store platform see this topic: + // http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=46 + var property = component.GetType().GetProperty(propertyName.Value); + var propertyValue = property.GetValue(component, null); + + if (propertyValue == null && compareValue.GetValue() == null) { + return TaskStatus.Success; + } + + return propertyValue.Equals(compareValue.GetValue()) ? TaskStatus.Success : TaskStatus.Failure; + } + + public override void OnReset() + { + targetGameObject = null; + componentName = null; + propertyName = null; + compareValue = null; + } + } +} \ No newline at end of file diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs.meta b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs.meta new file mode 100644 index 00000000..59fbef22 --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/Reflection/ComparePropertyValue.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f8b0fa403f826a74087ece4b81fec1c7 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: -- cgit v1.1-26-g67d0