namespace Zinnia.Data.Operation { using System; using UnityEngine; using UnityEngine.Events; using Zinnia.Extension; /// /// Duplicates a by cloning it. /// public class GameObjectCloner : MonoBehaviour { /// /// Defines the event with the . /// [Serializable] public class UnityEvent : UnityEvent { } [Tooltip("The object to clone.")] [SerializeField] private GameObject source; /// /// The object to clone. /// public GameObject Source { get { return source; } set { source = value; } } [Tooltip("An optional object to parent the cloned objects to.")] [SerializeField] private GameObject parent; /// /// An optional object to parent the cloned objects to. /// public GameObject Parent { get { return parent; } set { parent = value; } } [Tooltip("An optional string to name the cloned object.")] [SerializeField] private string clonedName; /// /// An optional to name the cloned object. /// public string ClonedName { get { return clonedName; } set { clonedName = value; } } /// /// Emitted when an object has been cloned. /// public UnityEvent Cloned = new UnityEvent(); /// /// Clears . /// public virtual void ClearSource() { if (!this.IsValidState()) { return; } Source = default; } /// /// Clears . /// public virtual void ClearParent() { if (!this.IsValidState()) { return; } Parent = default; } /// /// Duplicates by cloning it and optionally parents the cloned object to . /// /// The cloned object, or if no clone has been created. public virtual GameObject Clone() { if (!this.IsValidState()) { return null; } return Clone(Source); } /// /// Duplicates a by cloning it and optionally parents the cloned object to . /// /// The object to clone. /// The cloned object, or if no clone has been created. public virtual GameObject Clone(GameObject source) { if (!this.IsValidState() || source == null) { return null; } GameObject clone = Instantiate(source, Parent == null ? null : Parent.transform); if (!string.IsNullOrEmpty(ClonedName)) { clone.name = ClonedName; } Cloned?.Invoke(clone); return clone; } /// /// Duplicates by cloning it and parents the cloned object to . /// public virtual void DoClone() { Clone(); } /// /// Duplicates by cloning it and parents the cloned object to . /// /// The object to clone. public virtual void DoClone(GameObject source) { Clone(source); } } }