namespace Tilia.Interactions.Interactables.Interactables.Utility { using UnityEngine; using Zinnia.Data.Attribute; /// /// A helper class to easily create an Interactable Object via script. /// public class InteractableCreator : MonoBehaviour { #region Reference Settings [Header("Reference Settings")] [Tooltip("The interactable prefab.")] [SerializeField] [Restricted] private GameObject interactableObject; /// /// The interactable prefab. /// public GameObject InteractableObject { get { return interactableObject; } set { interactableObject = value; } } #endregion /// /// The factory for creating new Interactable Obejcts. /// protected InteractableFactory interactableFactory = new InteractableFactory(); /// /// Converts a given and wraps it into an . /// /// The GameObject to convert. /// The created Interactable Facade. public virtual InteractableFacade Convert(GameObject objectToConvert) { if (InteractableObject == null || !interactableFactory.CanConvert(objectToConvert)) { return null; } bool prefabState = interactableObject.activeSelf; interactableObject.SetActive(false); GameObject newInteractable = Instantiate(interactableObject); GameObject createdInteractable = interactableFactory.Create(newInteractable, objectToConvert); InteractableFacade interactableFacade = createdInteractable.GetComponent(); interactableFacade.Configuration.UpdatePrimaryAction(InteractableFactory.ActionType.None); interactableFacade.Configuration.UpdateSecondaryAction(InteractableFactory.ActionType.None); createdInteractable.SetActive(true); interactableObject.SetActive(prefabState); return interactableFacade; } /// /// Converts a given and wraps it into an . /// /// The GameObject to convert. public virtual void DoConvert(GameObject objectToConvert) { Convert(objectToConvert); } /// /// Embeds a created into the given . /// /// The GameObject to embed the Interactable within. /// The created Interactable Facade. public virtual InteractableFacade Embed(GameObject embedObject) { if (InteractableObject == null || !interactableFactory.CanConvert(embedObject)) { return null; } bool prefabState = interactableObject.activeSelf; interactableObject.SetActive(false); GameObject newInteractable = Instantiate(interactableObject); GameObject createdInteractable = interactableFactory.Embed(newInteractable, embedObject); InteractableFacade interactableFacade = createdInteractable.GetComponentInChildren(); interactableFacade.Configuration.UpdatePrimaryAction(InteractableFactory.ActionType.None); interactableFacade.Configuration.UpdateSecondaryAction(InteractableFactory.ActionType.None); createdInteractable.SetActive(true); interactableObject.SetActive(prefabState); return interactableFacade; } /// /// Embeds a created into the given . /// /// The GameObject to embed the Interactable within. public virtual void DoEmbed(GameObject embedObject) { Embed(embedObject); } } }