#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.Conversions { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Conversions")] [Opsive.Shared.Utility.Description("Gets the parent GameObject from a GameObject or Transform. Can traverse up multiple levels.")] public class GetParentGameObject : Action { [Tooltip("The GameObject to get the parent from. If null, uses Transform.")] [SerializeField] protected SharedVariable m_SourceGameObject; [Tooltip("The Transform to get the parent from. Only used if GameObject is null.")] [SerializeField] protected SharedVariable m_SourceTransform; [Tooltip("The number of levels up to traverse (0 = direct parent, 1 = grandparent, etc.).")] [SerializeField] protected SharedVariable m_LevelsUp = 0; [Tooltip("The resulting parent GameObject.")] [SerializeField] [RequireShared] protected SharedVariable m_ParentGameObject; /// /// Gets the parent GameObject. /// /// The status of the action. public override TaskStatus OnUpdate() { Transform sourceTransform = null; if (m_SourceGameObject.Value != null) { sourceTransform = m_SourceGameObject.Value.transform; } else if (m_SourceTransform.Value != null) { sourceTransform = m_SourceTransform.Value; } if (sourceTransform == null) { m_ParentGameObject.Value = null; return TaskStatus.Success; } var parent = sourceTransform.parent; for (int i = 0; i < m_LevelsUp.Value && parent != null; ++i) { parent = parent.parent; } m_ParentGameObject.Value = parent != null ? parent.gameObject : null; return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_SourceGameObject = null; m_SourceTransform = null; m_LevelsUp = 0; m_ParentGameObject = null; } } } #endif