Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/BehaviorDesigner/Runtime/Tasks/SetFieldValue.cs
2026-02-21 16:45:37 +08:00

58 lines
1.6 KiB
C#

using System;
using System.Reflection;
using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks
{
[HelpURLAttribute("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=149")]
[TaskDescription("Sets the field to the value specified. Returns success if the field was set.")]
[TaskIcon("{SkinColor}ReflectionIcon.png")]
[TaskCategory("Reflection")]
public class SetFieldValue : Action
{
[Tooltip("The GameObject to set the field on")]
public SharedGameObject targetGameObject;
[Tooltip("The component to set the field on")]
public SharedString componentName;
[Tooltip("The name of the field")]
public SharedString fieldName;
[Tooltip("The value to set")]
public SharedVariable fieldValue;
public override TaskStatus OnUpdate()
{
if (fieldValue == null)
{
Debug.LogWarning("Unable to get field - field value is null");
return TaskStatus.Failure;
}
Type typeWithinAssembly = TaskUtility.GetTypeWithinAssembly(componentName.Value);
if (typeWithinAssembly == null)
{
Debug.LogWarning("Unable to set field - type is null");
return TaskStatus.Failure;
}
Component component = GetDefaultGameObject(targetGameObject.Value).GetComponent(typeWithinAssembly);
if (component == null)
{
Debug.LogWarning("Unable to set the field with component " + componentName.Value);
return TaskStatus.Failure;
}
FieldInfo field = component.GetType().GetField(fieldName.Value);
field.SetValue(component, fieldValue.GetValue());
return TaskStatus.Success;
}
public override void OnReset()
{
targetGameObject = null;
componentName = null;
fieldName = null;
fieldValue = null;
}
}
}