summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-27 16:15:06 +0800
committerchai <chaifix@163.com>2021-01-27 16:15:06 +0800
commit97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 (patch)
treed9b1db5908a3a030c529e230386fe01062923b09 /Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs
parent1fe4ffba72f56ccc6a89d1896142425c666887d4 (diff)
+behaviour designer
Diffstat (limited to 'Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs')
-rw-r--r--Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs58
1 files changed, 58 insertions, 0 deletions
diff --git a/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs b/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs
new file mode 100644
index 00000000..1a4631a1
--- /dev/null
+++ b/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/GetFieldValue.cs
@@ -0,0 +1,58 @@
+using UnityEngine;
+using System;
+using System.Reflection;
+
+namespace BehaviorDesigner.Runtime.Tasks
+{
+ [TaskDescription("Gets the value from the field specified. Returns success if the field was retrieved.")]
+ [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=147")]
+ [TaskCategory("Reflection")]
+ [TaskIcon("{SkinColor}ReflectionIcon.png")]
+ public class GetFieldValue : Action
+ {
+ [Tooltip("The GameObject to get the field on")]
+ public SharedGameObject targetGameObject;
+ [Tooltip("The component to get the field on")]
+ public SharedString componentName;
+ [Tooltip("The name of the field")]
+ public SharedString fieldName;
+ [Tooltip("The value of the field")]
+ [RequiredField]
+ public SharedVariable fieldValue;
+
+ public override TaskStatus OnUpdate()
+ {
+ if (fieldValue == null) {
+ //Debug.LogWarning("Unable to get field - field value is null");
+ return TaskStatus.Failure;
+ }
+
+ var type = TaskUtility.GetTypeWithinAssembly(componentName.Value);
+ if (type == null) {
+ //Debug.LogWarning("Unable to get field - type is null");
+ return TaskStatus.Failure;
+ }
+
+ var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type);
+ if (component == null) {
+ //Debug.LogWarning("Unable to get 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);
+ fieldValue.SetValue(field.GetValue(component));
+
+ return TaskStatus.Success;
+ }
+
+ public override void OnReset()
+ {
+ targetGameObject = null;
+ componentName = null;
+ fieldName = null;
+ fieldValue = null;
+ }
+ }
+} \ No newline at end of file